diff options
Diffstat (limited to 'src/paragraph/mod.rs')
-rw-r--r-- | src/paragraph/mod.rs | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/paragraph/mod.rs b/src/paragraph/mod.rs new file mode 100644 index 0000000..32ab429 --- /dev/null +++ b/src/paragraph/mod.rs @@ -0,0 +1,61 @@ +//! Paragraph. +use crate::xml::element::{Element, Elements, FromElements}; + +mod part; + +pub use part::{Error as PartError, Part}; + +/// A paragraph. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Paragraph +{ + parts: Vec<Part>, +} + +impl Paragraph +{ + /// Returns a new `Paragraph`. + pub fn new(parts: impl IntoIterator<Item = Part>) -> Self + { + Self { + parts: parts.into_iter().collect(), + } + } + + /// Returns the parts of the paragraph. + #[must_use] + pub fn parts(&self) -> &[Part] + { + &self.parts + } +} + +impl FromElements for Paragraph +{ + type Error = Error; + + fn from_elements(elements: &Elements) -> Result<Self, Self::Error> + { + let parts = elements + .into_iter() + .filter_map(|element| { + if matches!(element, Element::Comment(_)) { + return None; + } + + Some(Part::from_elements(&Elements::from([element.clone()]))) + }) + .collect::<Result<Vec<_>, _>>()?; + + Ok(Self { parts }) + } +} + +/// [`Paragraph`] error. +#[derive(Debug, thiserror::Error)] +pub enum Error +{ + /// Invalid reference description part. + #[error("Invalid part")] + InvalidPart(#[from] PartError), +} |