From cc30a537284871d668911353bd121e38d0353eb0 Mon Sep 17 00:00:00 2001 From: HampusM Date: Tue, 16 May 2023 21:59:46 +0200 Subject: refactor: reorganize structs --- src/description/mod.rs | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 src/description/mod.rs (limited to 'src/description/mod.rs') 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, + parts: Vec, +} + +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 + { + &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 + { + 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::, _>>()?; + + Ok(Description { + for_function, + parts, + }) + } +} + +/// [`Description`] error. +#[derive(Debug, thiserror::Error)] +pub enum Error +{ + /// Invalid description part. + #[error("Invalid description part")] + InvalidPart(#[from] PartError), +} -- cgit v1.2.3-18-g5258