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/paragraph/mod.rs | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/paragraph/mod.rs (limited to 'src/paragraph/mod.rs') 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, +} + +impl Paragraph +{ + /// Returns a new `Paragraph`. + pub fn new(parts: impl IntoIterator) -> 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 + { + let parts = elements + .into_iter() + .filter_map(|element| { + if matches!(element, Element::Comment(_)) { + return None; + } + + Some(Part::from_elements(&Elements::from([element.clone()]))) + }) + .collect::, _>>()?; + + Ok(Self { parts }) + } +} + +/// [`Paragraph`] error. +#[derive(Debug, thiserror::Error)] +pub enum Error +{ + /// Invalid reference description part. + #[error("Invalid part")] + InvalidPart(#[from] PartError), +} -- cgit v1.2.3-18-g5258