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
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*!
Provides safe `RefNode` conversion functions.

Note that all of the `as_{name}` functions work as follows.

* If the `node_type` corresponds to the correct type, it returns OK.
* If the `node_type` does not correspond to the correct type, it returns `Error::InvalidState`.
* If the `node_type` is not implemented it returns `Error::NotSupported`.

*/
use crate::level2::node_impl::*;
use crate::level2::traits::*;
use crate::shared::error::{Error, Result, MSG_INVALID_NODE_TYPE};

use crate::{make_is_as_functions, make_ref_type};

// ------------------------------------------------------------------------------------------------
// Public Functions
// ------------------------------------------------------------------------------------------------

make_ref_type!(RefAttribute, MutRefAttribute, Attribute);

make_ref_type!(RefCDataSection, MutRefCDataSection, CDataSection);

make_ref_type!(RefCharacterData, MutRefCharacterData, CharacterData);

make_ref_type!(RefComment, MutRefComment, Comment);

make_ref_type!(RefDocument, MutRefDocument, Document);

make_ref_type!(
    RefDocumentFragment,
    MutRefDocumentFragment,
    DocumentFragment
);

make_ref_type!(RefDocumentType, MutRefDocumentType, DocumentType);

make_ref_type!(RefElement, MutRefElement, Element);

make_ref_type!(RefEntity, MutRefEntity, Entity);

make_ref_type!(RefEntityReference, MutRefEntityReference, EntityReference);

make_ref_type!(RefNotation, MutRefNotation, Notation);

make_ref_type!(
    RefProcessingInstruction,
    MutRefProcessingInstruction,
    ProcessingInstruction
);

make_ref_type!(RefText, MutRefText, Text);

// ------------------------------------------------------------------------------------------------
// Public Functions
// ------------------------------------------------------------------------------------------------

make_is_as_functions!(
    is_attribute,
    NodeType::Attribute,
    as_attribute,
    RefAttribute,
    as_attribute_mut,
    MutRefAttribute
);

make_is_as_functions!(
    is_element,
    NodeType::Element,
    as_element,
    RefElement,
    as_element_mut,
    MutRefElement
);

///
/// Determines if the specified node is a type of `CharacterData`.
///
#[inline]
pub fn is_character_data(ref_node: &RefNode) -> bool {
    match ref_node.borrow().i_node_type {
        NodeType::CData | NodeType::Comment | NodeType::Text => true,
        _ => false,
    }
}

///
/// Safely _cast_ the specified `RefNode` into a  `Text`.
///
#[inline]
pub fn as_character_data(ref_node: &RefNode) -> Result<RefCharacterData<'_>> {
    match ref_node.borrow().i_node_type {
        NodeType::CData | NodeType::Comment | NodeType::Text => {
            Ok(ref_node as RefCharacterData<'_>)
        }
        _ => {
            warn!("{}", MSG_INVALID_NODE_TYPE);
            Err(Error::InvalidState)
        }
    }
}

///
/// Safely _cast_ the specified `RefNode` into a mutable `Text`.
///
#[inline]
pub fn as_character_data_mut(ref_node: &mut RefNode) -> Result<MutRefCharacterData<'_>> {
    let node_type = { &ref_node.borrow().i_node_type.clone() };
    match node_type {
        NodeType::CData | NodeType::Comment | NodeType::Text => {
            Ok(ref_node as MutRefCharacterData<'_>)
        }
        _ => {
            warn!("{}", MSG_INVALID_NODE_TYPE);
            Err(Error::InvalidState)
        }
    }
}

make_is_as_functions!(
    is_text,
    NodeType::Text,
    as_text,
    RefText,
    as_text_mut,
    MutRefText
);

make_is_as_functions!(
    is_cdata_section,
    NodeType::CData,
    as_cdata_section,
    RefCDataSection,
    as_cdata_section_mut,
    MutRefCDataSection
);

make_is_as_functions!(
    is_entity_reference,
    NodeType::EntityReference,
    as_entity_reference,
    RefEntityReference,
    as_entity_reference_mut,
    MutRefEntityReference
);

make_is_as_functions!(
    is_entity_,
    NodeType::Entity,
    as_entity,
    RefEntity,
    as_entity_mut,
    MutRefEntity
);

make_is_as_functions!(
    is_processing_instruction,
    NodeType::ProcessingInstruction,
    as_processing_instruction,
    RefProcessingInstruction,
    as_processing_instruction_mut,
    MutRefProcessingInstruction
);

make_is_as_functions!(
    is_comment,
    NodeType::Comment,
    as_comment,
    RefComment,
    as_comment_mut,
    MutRefComment
);

make_is_as_functions!(
    is_document,
    NodeType::Document,
    as_document,
    RefDocument,
    as_document_mut,
    MutRefDocument
);

make_is_as_functions!(
    is_document_type,
    NodeType::DocumentType,
    as_document_type,
    RefDocumentType,
    as_document_type_mut,
    MutRefDocumentType
);

make_is_as_functions!(
    is_document_fragment,
    NodeType::DocumentFragment,
    as_document_fragment,
    RefDocumentFragment,
    as_document_fragment_mut,
    MutRefDocumentFragment
);

make_is_as_functions!(
    is_notation,
    NodeType::Notation,
    as_notation,
    RefNotation,
    as_notation_mut,
    MutRefNotation
);