summaryrefslogtreecommitdiff
path: root/src/description/mod.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2023-05-16 21:59:46 +0200
committerHampusM <hampus@hampusmat.com>2023-05-16 21:59:46 +0200
commitcc30a537284871d668911353bd121e38d0353eb0 (patch)
tree121976a44346f5979447d9d9052372246d4ff66c /src/description/mod.rs
parentb336066d6067a0eb9ff9fc34c5aa062b86e56c62 (diff)
refactor: reorganize structsHEADmaster
Diffstat (limited to 'src/description/mod.rs')
-rw-r--r--src/description/mod.rs98
1 files changed, 98 insertions, 0 deletions
diff --git a/src/description/mod.rs b/src/description/mod.rs
new file mode 100644
index 0000000..a87c1cf
--- /dev/null
+++ b/src/description/mod.rs
@@ -0,0 +1,98 @@
+//! Reference entry description.
+use crate::xml::element::{Elements, FromElements};
+
+mod part;
+
+pub use part::{Error as PartError, Part};
+
+/// Reference entry description.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct Description
+{
+ for_function: Option<String>,
+ parts: Vec<Part>,
+}
+
+impl Description
+{
+ /// Returns a new empty `Description`.
+ #[must_use]
+ pub fn new() -> Self
+ {
+ Self {
+ for_function: None,
+ parts: Vec::new(),
+ }
+ }
+
+ /// Returns what function this description is specific for.
+ #[must_use]
+ pub fn for_function(&self) -> &Option<String>
+ {
+ &self.for_function
+ }
+
+ /// Returns the description's parts.
+ #[must_use]
+ pub fn parts(&self) -> &[Part]
+ {
+ &self.parts
+ }
+}
+
+impl Default for Description
+{
+ fn default() -> Self
+ {
+ Self::new()
+ }
+}
+
+impl FromElements for Description
+{
+ type Error = Error;
+
+ fn from_elements(elements: &Elements) -> Result<Self, Self::Error>
+ {
+ let for_function =
+ elements
+ .get_first_tagged_with_name("title")
+ .and_then(|title_element| {
+ let title_text =
+ title_element.child_elements().get_first_text_element()?;
+
+ if title_text != "Description for " {
+ return None;
+ }
+
+ let function_element = title_element
+ .child_elements()
+ .get_first_tagged_with_name("function")?;
+
+ function_element
+ .child_elements()
+ .get_first_text_element()
+ .cloned()
+ });
+
+ let parts = elements
+ .get_all_tagged_elements()
+ .into_iter()
+ .map(Part::from_tagged_element)
+ .collect::<Result<Vec<_>, _>>()?;
+
+ Ok(Description {
+ for_function,
+ parts,
+ })
+ }
+}
+
+/// [`Description`] error.
+#[derive(Debug, thiserror::Error)]
+pub enum Error
+{
+ /// Invalid description part.
+ #[error("Invalid description part")]
+ InvalidPart(#[from] PartError),
+}