diff options
author | HampusM <hampus@hampusmat.com> | 2023-05-16 21:59:46 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2023-05-16 21:59:46 +0200 |
commit | cc30a537284871d668911353bd121e38d0353eb0 (patch) | |
tree | 121976a44346f5979447d9d9052372246d4ff66c /src/paragraph/mod.rs | |
parent | b336066d6067a0eb9ff9fc34c5aa062b86e56c62 (diff) |
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), +} |