Skip to main content

xml_dom/level2/
convert.rs

1/*!
2Provides safe `RefNode` conversion functions.
3
4Note that all of the `as_{name}` functions work as follows.
5
6* If the `node_type` corresponds to the correct type, it returns OK.
7* If the `node_type` does not correspond to the correct type, it returns `Error::InvalidState`.
8* If the `node_type` is not implemented it returns `Error::NotSupported`.
9
10 */
11
12use crate::level2::node_impl::*;
13use crate::level2::traits::*;
14use crate::shared::error::{Error, Result, MSG_INVALID_NODE_TYPE};
15use crate::{make_is_as_functions, make_ref_type};
16use tracing::warn;
17
18// ------------------------------------------------------------------------------------------------
19// Public Functions
20// ------------------------------------------------------------------------------------------------
21
22make_ref_type!(RefAttribute, MutRefAttribute, Attribute);
23
24make_ref_type!(RefCDataSection, MutRefCDataSection, CDataSection);
25
26make_ref_type!(RefCharacterData, MutRefCharacterData, CharacterData);
27
28make_ref_type!(RefComment, MutRefComment, Comment);
29
30make_ref_type!(RefDocument, MutRefDocument, Document);
31
32make_ref_type!(
33    RefDocumentFragment,
34    MutRefDocumentFragment,
35    DocumentFragment
36);
37
38make_ref_type!(RefDocumentType, MutRefDocumentType, DocumentType);
39
40make_ref_type!(RefElement, MutRefElement, Element);
41
42make_ref_type!(RefEntity, MutRefEntity, Entity);
43
44make_ref_type!(RefEntityReference, MutRefEntityReference, EntityReference);
45
46make_ref_type!(RefNotation, MutRefNotation, Notation);
47
48make_ref_type!(
49    RefProcessingInstruction,
50    MutRefProcessingInstruction,
51    ProcessingInstruction
52);
53
54make_ref_type!(RefText, MutRefText, Text);
55
56// ------------------------------------------------------------------------------------------------
57// Public Functions
58// ------------------------------------------------------------------------------------------------
59
60make_is_as_functions!(
61    is_attribute,
62    NodeType::Attribute,
63    as_attribute,
64    RefAttribute,
65    as_attribute_mut,
66    MutRefAttribute
67);
68
69make_is_as_functions!(
70    is_element,
71    NodeType::Element,
72    as_element,
73    RefElement,
74    as_element_mut,
75    MutRefElement
76);
77
78///
79/// Determines if the specified node is a type of `CharacterData`.
80///
81#[inline]
82pub fn is_character_data(ref_node: &RefNode) -> bool {
83    matches!(
84        ref_node.borrow().i_node_type,
85        NodeType::CData | NodeType::Comment | NodeType::Text
86    )
87}
88
89///
90/// Safely _cast_ the specified `RefNode` into a  `Text`.
91///
92#[inline]
93pub fn as_character_data(ref_node: &RefNode) -> Result<RefCharacterData<'_>> {
94    match ref_node.borrow().i_node_type {
95        NodeType::CData | NodeType::Comment | NodeType::Text => {
96            Ok(ref_node as RefCharacterData<'_>)
97        }
98        _ => {
99            warn!("{}", MSG_INVALID_NODE_TYPE);
100            Err(Error::InvalidState)
101        }
102    }
103}
104
105///
106/// Safely _cast_ the specified `RefNode` into a mutable `Text`.
107///
108#[inline]
109pub fn as_character_data_mut(ref_node: &mut RefNode) -> Result<MutRefCharacterData<'_>> {
110    let node_type = { &ref_node.borrow().i_node_type.clone() };
111    match node_type {
112        NodeType::CData | NodeType::Comment | NodeType::Text => {
113            Ok(ref_node as MutRefCharacterData<'_>)
114        }
115        _ => {
116            warn!("{}", MSG_INVALID_NODE_TYPE);
117            Err(Error::InvalidState)
118        }
119    }
120}
121
122make_is_as_functions!(
123    is_text,
124    NodeType::Text,
125    as_text,
126    RefText,
127    as_text_mut,
128    MutRefText
129);
130
131make_is_as_functions!(
132    is_cdata_section,
133    NodeType::CData,
134    as_cdata_section,
135    RefCDataSection,
136    as_cdata_section_mut,
137    MutRefCDataSection
138);
139
140make_is_as_functions!(
141    is_entity_reference,
142    NodeType::EntityReference,
143    as_entity_reference,
144    RefEntityReference,
145    as_entity_reference_mut,
146    MutRefEntityReference
147);
148
149make_is_as_functions!(
150    is_entity_,
151    NodeType::Entity,
152    as_entity,
153    RefEntity,
154    as_entity_mut,
155    MutRefEntity
156);
157
158make_is_as_functions!(
159    is_processing_instruction,
160    NodeType::ProcessingInstruction,
161    as_processing_instruction,
162    RefProcessingInstruction,
163    as_processing_instruction_mut,
164    MutRefProcessingInstruction
165);
166
167make_is_as_functions!(
168    is_comment,
169    NodeType::Comment,
170    as_comment,
171    RefComment,
172    as_comment_mut,
173    MutRefComment
174);
175
176make_is_as_functions!(
177    is_document,
178    NodeType::Document,
179    as_document,
180    RefDocument,
181    as_document_mut,
182    MutRefDocument
183);
184
185make_is_as_functions!(
186    is_document_type,
187    NodeType::DocumentType,
188    as_document_type,
189    RefDocumentType,
190    as_document_type_mut,
191    MutRefDocumentType
192);
193
194make_is_as_functions!(
195    is_document_fragment,
196    NodeType::DocumentFragment,
197    as_document_fragment,
198    RefDocumentFragment,
199    as_document_fragment_mut,
200    MutRefDocumentFragment
201);
202
203make_is_as_functions!(
204    is_notation,
205    NodeType::Notation,
206    as_notation,
207    RefNotation,
208    as_notation_mut,
209    MutRefNotation
210);