xmlem/
key.rs

1use slotmap::new_key_type;
2
3use crate::{element::Element, Document};
4
5new_key_type! {
6    pub(crate) struct DocKey;
7}
8
9#[derive(Debug, PartialEq, Eq, Copy, Clone)]
10pub struct Text(pub(crate) DocKey);
11
12impl Text {
13    pub fn as_str<'d>(&self, doc: &'d Document) -> &'d str {
14        let node_value = &doc.nodes[self.0];
15        node_value.as_str().unwrap()
16    }
17}
18
19#[derive(Debug, PartialEq, Eq, Copy, Clone)]
20pub struct CDataSection(pub(crate) DocKey);
21
22impl CDataSection {
23    pub fn as_str<'d>(&self, doc: &'d Document) -> &'d str {
24        let node_value = &doc.nodes[self.0];
25        node_value.as_str().unwrap()
26    }
27}
28
29#[derive(Debug, PartialEq, Eq, Copy, Clone)]
30pub struct ProcessingInstruction(pub(crate) DocKey);
31
32impl ProcessingInstruction {
33    pub fn as_str<'d>(&self, doc: &'d Document) -> &'d str {
34        let node_value = &doc.nodes[self.0];
35        node_value.as_str().unwrap()
36    }
37}
38
39#[derive(Debug, PartialEq, Eq, Copy, Clone)]
40pub struct Comment(pub(crate) DocKey);
41
42impl Comment {
43    pub fn as_str<'d>(&self, doc: &'d Document) -> &'d str {
44        let node_value = &doc.nodes[self.0];
45        node_value.as_str().unwrap()
46    }
47}
48
49#[derive(Debug, PartialEq, Eq, Copy, Clone)]
50pub struct DocumentType(pub(crate) DocKey);
51
52#[derive(Debug, Copy, PartialEq, Eq, Clone)]
53pub enum Node {
54    Element(Element),
55    Text(Text),
56    CDataSection(CDataSection),
57    ProcessingInstruction(ProcessingInstruction),
58    Comment(Comment),
59    DocumentType(DocumentType),
60}
61
62impl Node {
63    pub(crate) fn as_key(self) -> DocKey {
64        match self {
65            Node::Element(e) => e.0,
66            Node::Text(e) => e.0,
67            Node::CDataSection(e) => e.0,
68            Node::ProcessingInstruction(e) => e.0,
69            Node::Comment(e) => e.0,
70            Node::DocumentType(e) => e.0,
71        }
72    }
73
74    pub fn as_text(self) -> Option<Text> {
75        match self {
76            Node::Text(e) => Some(e),
77            _ => None,
78        }
79    }
80
81    pub fn as_element(self) -> Option<Element> {
82        match self {
83            Node::Element(e) => Some(e),
84            _ => None,
85        }
86    }
87
88    pub fn as_document_type(self) -> Option<DocumentType> {
89        match self {
90            Node::DocumentType(e) => Some(e),
91            _ => None,
92        }
93    }
94
95    pub fn as_cdata(self) -> Option<CDataSection> {
96        match self {
97            Node::CDataSection(e) => Some(e),
98            _ => None,
99        }
100    }
101
102    pub fn as_comment(self) -> Option<Comment> {
103        match self {
104            Node::Comment(e) => Some(e),
105            _ => None,
106        }
107    }
108
109    pub fn as_processing_instruction(self) -> Option<ProcessingInstruction> {
110        match self {
111            Node::ProcessingInstruction(e) => Some(e),
112            _ => None,
113        }
114    }
115
116    pub fn to_ordinal(&self) -> u8 {
117        match self {
118            Node::Element(_) => 0,
119            Node::Text(_) => 2,
120            Node::CDataSection(_) => 3,
121            Node::ProcessingInstruction(_) => 4,
122            Node::Comment(_) => 2,
123            Node::DocumentType(_) => 5,
124        }
125    }
126}