summaryrefslogtreecommitdiff
path: root/src/description/mod.rs
blob: a87c1cfe90490a06ffd01172db45b772f8cf400b (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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<String>,
    parts: Vec<Part>,
}

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<String>
    {
        &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<Self, Self::Error>
    {
        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::<Result<Vec<_>, _>>()?;

        Ok(Description {
            for_function,
            parts,
        })
    }
}

/// [`Description`] error.
#[derive(Debug, thiserror::Error)]
pub enum Error
{
    /// Invalid description part.
    #[error("Invalid description part")]
    InvalidPart(#[from] PartError),
}