summaryrefslogtreecommitdiff
path: root/src/paragraph/mod.rs
blob: 32ab4293437fac585a1475bbb1204e5ada7a679e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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),
}