muffy_document/document/
element.rs1use super::node::Node;
2use alloc::sync::Arc;
3use core::ops::Deref;
4
5#[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 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 pub fn name(&self) -> &str {
31 &self.name
32 }
33
34 pub fn namespace(&self) -> Option<&str> {
36 self.namespace.as_deref()
37 }
38
39 pub fn set_namespace(mut self, namespace: Option<String>) -> Self {
41 self.namespace = namespace;
42 self
43 }
44
45 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 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}