summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2023-02-25 14:30:11 +0100
committerHampusM <hampus@hampusmat.com>2023-02-25 14:30:11 +0100
commit0f7696236b6b4d1011cefc860da332ffe11b1b1f (patch)
tree7f23ccf1e774df73fd0b93027874c0d3474267a3
parentb3fdb097b43b8fe3f02a6153a17f50274e009acb (diff)
refactor: improve errors
-rw-r--r--src/description.rs4
-rw-r--r--src/lib.rs48
2 files changed, 40 insertions, 12 deletions
diff --git a/src/description.rs b/src/description.rs
index ba37abc..d57dbc7 100644
--- a/src/description.rs
+++ b/src/description.rs
@@ -197,8 +197,8 @@ pub enum ParagraphPartError
#[error("Input element is a comment")]
InputIsComment,
- /// A input element is a unknown reference description part.
- #[error("Input element with name '{0}' is a unknown reference description part")]
+ /// A input element is a unknown description part.
+ #[error("Input element with name '{0}' is a unknown description part")]
UnknownPart(String),
/// No text was found in tagged input element.
diff --git a/src/lib.rs b/src/lib.rs
index af9ef72..f4d465f 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -4,6 +4,7 @@
#![cfg_attr(doc_cfg, feature(doc_cfg))]
#![deny(clippy::all, clippy::pedantic, missing_docs)]
+use std::ffi::OsStr;
use std::os::unix::prelude::OsStrExt;
use include_dir::{include_dir, Dir};
@@ -84,7 +85,17 @@ impl ReferenceEntry
let root_elements = parser.parse()?;
- ReferenceEntry::from_elements(&root_elements)
+ ReferenceEntry::from_elements(&root_elements).map_err(|err| {
+ ReferenceEntryError::Invalid {
+ source: err,
+ entry_name: function_file
+ .path()
+ .file_stem()
+ .unwrap_or_else(|| OsStr::new("(unknown)"))
+ .to_string_lossy()
+ .to_string(),
+ }
+ })
}
/// Returns the reference entry purpose.
@@ -104,23 +115,23 @@ impl ReferenceEntry
impl FromElements for ReferenceEntry
{
- type Error = ReferenceEntryError;
+ type Error = InvalidReferenceEntryError;
fn from_elements(elements: &Elements) -> Result<Self, Self::Error>
{
let refentry_element = elements
.get_first_tagged_with_name("refentry")
- .ok_or(ReferenceEntryError::MissingRefEntry)?;
+ .ok_or(Self::Error::MissingRefEntry)?;
let refnamediv_element = refentry_element
.child_elements()
.get_first_tagged_with_name("refnamediv")
- .ok_or(ReferenceEntryError::MissingRefNameDiv)?;
+ .ok_or(Self::Error::MissingRefNameDiv)?;
let refpurpose_element = refnamediv_element
.child_elements()
.get_first_tagged_with_name("refpurpose")
- .ok_or(ReferenceEntryError::MissingRefPurpose)?;
+ .ok_or(Self::Error::MissingRefPurpose)?;
let purpose = refpurpose_element
.child_elements()
@@ -137,7 +148,7 @@ impl FromElements for ReferenceEntry
value: b"description".to_vec(),
},
)
- .ok_or(ReferenceEntryError::MissingDescriptionRefSect)?;
+ .ok_or(Self::Error::MissingDescriptionRefSect)?;
let description =
Description::from_elements(description_refsect.child_elements())?;
@@ -157,6 +168,27 @@ pub enum ReferenceEntryError
#[error("No reference entry file was found for '{0}'")]
NoFileFound(String),
+ /// The data of the reference entry is invalid.
+ #[error("Invalid reference entry '{entry_name}'")]
+ Invalid
+ {
+ /// Source error.
+ #[source]
+ source: InvalidReferenceEntryError,
+
+ /// Name of the invalid reference entry.
+ entry_name: String,
+ },
+
+ /// Parsing failed.
+ #[error("Parsing failed")]
+ ParsingFailed(#[from] ParserError),
+}
+
+/// Error for when a reference entry is invalid.
+#[derive(Debug, thiserror::Error)]
+pub enum InvalidReferenceEntryError
+{
/// No 'refentry' element was found.
#[error("No 'refentry' element was found")]
MissingRefEntry,
@@ -176,8 +208,4 @@ pub enum ReferenceEntryError
/// Invalid description.
#[error("Invalid description")]
InvalidDescription(#[from] DescriptionError),
-
- /// Parsing failed.
- #[error("Parsing failed")]
- ParsingFailed(#[from] ParserError),
}