Skip to main content

muffy_document/document/
element.rs

1use super::node::Node;
2use alloc::sync::Arc;
3use core::ops::Deref;
4
5/// An element.
6#[derive(Debug, Eq, PartialEq)]
7pub struct Element {
8    name: String,
9    namespace: Option<String>,
10    attributes: Vec<(String, String)>,
11    children: Vec<Arc<Node>>,
12}
13
14impl Element {
15    /// Creates an element.
16    pub const fn new(
17        name: String,
18        attributes: Vec<(String, String)>,
19        children: Vec<Arc<Node>>,
20    ) -> Self {
21        Self {
22            name,
23            namespace: None,
24            attributes,
25            children,
26        }
27    }
28
29    /// Returns a name.
30    pub fn name(&self) -> &str {
31        &self.name
32    }
33
34    /// Returns a namespace.
35    pub fn namespace(&self) -> Option<&str> {
36        self.namespace.as_deref()
37    }
38
39    /// Sets a namespace.
40    pub fn set_namespace(mut self, namespace: Option<String>) -> Self {
41        self.namespace = namespace;
42        self
43    }
44
45    /// Returns attributes.
46    pub fn attributes(&self) -> impl Iterator<Item = (&str, &str)> {
47        self.attributes
48            .iter()
49            .map(|(key, value)| (key.as_str(), value.as_str()))
50    }
51
52    /// Returns children.
53    pub fn children(&self) -> impl Iterator<Item = &Node> {
54        self.children.iter().map(Deref::deref)
55    }
56}
57
58impl From<Element> for Node {
59    fn from(element: Element) -> Self {
60        Self::Element(element)
61    }
62}