dummy_xml 0.1.6

Fast Non-validating XML DOM parser.
Documentation
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
use std::ptr;
use std::borrow::{Borrow, BorrowMut, Cow};

#[derive(Debug)]
pub struct Attribute {
    name: String,
    value: String,
    next: Option<Box<Attribute>>,
    prev: *mut Attribute,
}

#[derive(Debug)]
pub enum NodeType {
    Element,
    PcData,                //<node> text1 <child/> text2 </node>
    CData,                 //<node> <![CDATA[text1]]> <child/> <![CDATA[text2]]> </node> -> done
    Comment,               //<!-- comment text -->
    ProcessingInstruction, //?name value?>
    Declaration,           //<?xml version="1.0"?>
    Doctype,               //<!DOCTYPE greeting [ <!ELEMENT greeting (#PCDATA)> ]>
}

#[derive(Debug)]
pub struct Node<'a> {
    name: Cow<'a, str>,
    value: Cow<'a, str>,
    node_type: NodeType,
    next: Option<Box<Node<'a>>>,
    parent: *mut Node<'a>,
    prev: *mut Node<'a>,
    first_child: Option<Box<Node<'a>>>,
    last_child: *mut Node<'a>,
    first_attr: Option<Box<Attribute>>,
    last_attr: *mut Attribute,
}

impl Attribute {
    #[inline]
    pub fn new<S: Into<String>>(name: S, value: S) -> Box<Self> {
        Box::new(Attribute {
            name: name.into(),
            value: value.into(),
            next: None,
            prev: ptr::null_mut(),
        })
    }

    #[inline]
    pub fn name(&self) -> &str {
        &self.name
    }

    #[inline]
    pub fn value(&self) -> &str {
        &self.value
    }

    #[inline]
    pub fn set_name<S: Into<String>>(&mut self, name: S) -> &mut Self {
        self.name = name.into();
        self
    }

    #[inline]
    pub fn set_value<S: Into<String>>(&mut self, value: S) -> &mut Self {
        self.value = value.into();
        self
    }

    #[inline]
    pub fn next_attribute(&self) -> Option<&Self> {
        self.next.as_ref().map(|attr| attr.borrow())
    }

    #[inline]
    pub fn next_attribute_mut(&mut self) -> Option<&mut Self> {
        self.next.as_mut().map(|attr| attr.borrow_mut())
    }

    #[inline]
    pub fn previous_attribute(&self) -> Option<&Self> {
        unsafe { self.prev.as_ref().map(|attr| attr.borrow()) }
    }

    #[inline]
    pub fn previous_attribute_mut(&mut self) -> Option<&mut Self> {
        unsafe { self.prev.as_mut().map(|attr| attr.borrow_mut()) }
    }
}

impl PartialEq for Attribute {
    #[inline]
    fn eq(&self, other: &Attribute) -> bool {
        self.name == other.name && self.value == other.value
    }
}

const EMPTY_STRING: Cow<str> = Cow::Borrowed("");

impl<'a> Node<'a> {
    #[inline]
    pub fn new<S: Into<String>>(name: S) -> Box<Self> {
        Box::new(Node {
            name: Cow::Owned(name.into()),
            node_type: NodeType::Element,
            value: EMPTY_STRING,
            next: None,
            prev: ptr::null_mut(),
            parent: ptr::null_mut(),
            first_child: None,
            last_child: ptr::null_mut(),
            first_attr: None,
            last_attr: ptr::null_mut(),
        })
    }

    #[inline]
    pub fn new_by_type(node_type: NodeType) -> Box<Self> {
        Box::new(Node {
            name: EMPTY_STRING,
            node_type: node_type,
            value: EMPTY_STRING,
            next: None,
            prev: ptr::null_mut(),
            parent: ptr::null_mut(),
            first_child: None,
            last_child: ptr::null_mut(),
            first_attr: None,
            last_attr: ptr::null_mut(),
        })
    }

    #[inline]
    pub fn name(&self) -> &str {
        &self.name
    }

    #[inline]
    pub fn set_name<S: Into<String>>(&mut self, name: S) -> &mut Self {
        self.name = Cow::Owned(name.into());
        self
    }

    #[inline]
    pub fn value(&self) -> &str {
        &self.value
    }

    #[inline]
    pub fn set_value<S: Into<String>>(&mut self, value: S) -> &mut Self {
        self.value = Cow::Owned(value.into());
        self
    }

    #[inline]
    pub fn node_type(&self) -> &NodeType {
        &self.node_type
    }

    #[inline]
    pub fn set_node_type(&mut self, node_type: NodeType) -> &mut Self {
        self.node_type = node_type;
        self
    }

    #[inline]
    pub fn next_sibling(&self) -> Option<&Self> {
        self.next.as_ref().map(|node| node.borrow())
    }

    #[inline]
    pub fn next_sibling_mut(&mut self) -> Option<&mut Self> {
        self.next.as_mut().map(|node| node.borrow_mut())
    }

    #[inline]
    pub fn previous_sibling(&self) -> Option<&Self> {
        unsafe { self.prev.as_ref().map(|node| node.borrow()) }
    }

    #[inline]
    pub fn previous_sibling_mut(&mut self) -> Option<&mut Self> {
        unsafe { self.prev.as_mut().map(|node| node.borrow_mut()) }
    }

    #[inline]
    pub fn parent(&self) -> Option<&Self> {
        unsafe { self.parent.as_ref().map(|node| node.borrow()) }
    }

    #[inline]
    pub fn parent_mut(&mut self) -> Option<&mut Self> {
        unsafe { self.parent.as_mut().map(|node| node.borrow_mut()) }
    }

    #[inline]
    pub fn first_child(&self) -> Option<&Self> {
        self.first_child.as_ref().map(|node| node.borrow())
    }

    #[inline]
    pub fn first_child_mut(&mut self) -> Option<&mut Self> {
        self.first_child.as_mut().map(|node| node.borrow_mut())
    }

    #[inline]
    pub fn last_child(&self) -> Option<&Self> {
        unsafe { self.last_child.as_ref().map(|node| node.borrow()) }
    }

    #[inline]
    pub fn last_child_mut(&mut self) -> Option<&mut Self> {
        unsafe { self.last_child.as_mut().map(|node| node.borrow_mut()) }
    }

    #[inline]
    pub fn first_attribute(&self) -> Option<&Attribute> {
        self.first_attr.as_ref().map(|node| node.borrow())
    }

    #[inline]
    pub fn first_attribute_mut(&mut self) -> Option<&mut Attribute> {
        self.first_attr.as_mut().map(|node| node.borrow_mut())
    }

    #[inline]
    pub fn last_attribute(&self) -> Option<&Attribute> {
        unsafe { self.last_attr.as_ref().map(|node| node.borrow()) }
    }

    #[inline]
    pub fn last_attribute_mut(&mut self) -> Option<&mut Attribute> {
        unsafe { self.last_attr.as_mut().map(|node| node.borrow_mut()) }
    }

    #[inline]
    pub fn append_child<S: Into<String>>(&mut self, name: S) -> &mut Self {
        let mut node = Node::new(name.into());
        let raw_ptr: *mut _ = &mut *node;
        node.parent = self;
        match unsafe { self.last_child.as_mut() } {
            Some(last_child) => {
                node.prev = last_child;
                last_child.next = Some(node);
                self.last_child = raw_ptr;
            }
            None => {
                self.first_child = Some(node);
                self.last_child = raw_ptr;
            }
        }
        unsafe { &mut *raw_ptr }
    }

    #[inline]
    pub fn prepend_child<S: Into<String>>(&mut self, name: S) -> &mut Self {
        let mut node = Node::new(name.into());
        let raw_ptr: *mut _ = &mut *node;
        node.parent = self;
        match self.first_child.take() {
            Some(mut first_child) => {
                first_child.prev = raw_ptr;
                node.next = Some(first_child);
                self.first_child = Some(node);
            }
            None => {
                self.first_child = Some(node);
                self.last_child = raw_ptr;
            }
        }
        unsafe { &mut *raw_ptr }
    }

    #[inline]
    pub fn append_child_by_type(&mut self, node_type: NodeType) -> &mut Self {
        let mut node = Node::new_by_type(node_type);
        let raw_ptr: *mut _ = &mut *node;
        node.parent = self;
        match unsafe { self.last_child.as_mut() } {
            Some(last_child) => {
                node.prev = last_child;
                last_child.next = Some(node);
                self.last_child = raw_ptr;
            }
            None => {
                self.first_child = Some(node);
                self.last_child = raw_ptr;
            }
        }
        unsafe { &mut *raw_ptr }
    }

    #[inline]
    pub fn prepend_child_by_type(&mut self, node_type: NodeType) -> &mut Self {
        let mut node = Node::new_by_type(node_type);
        let raw_ptr: *mut _ = &mut *node;
        node.parent = self;
        match self.first_child.take() {
            Some(mut first_child) => {
                first_child.prev = raw_ptr;
                node.next = Some(first_child);
                self.first_child = Some(node);
            }
            None => {
                self.first_child = Some(node);
                self.last_child = raw_ptr;
            }
        }
        unsafe { &mut *raw_ptr }
    }

    #[inline]
    pub fn insert_child_after<S: Into<String>>(&mut self, name: S, child: &mut Self) -> &mut Self {
        let mut node = Node::new(name.into());
        let raw_ptr: *mut _ = &mut *node;
        node.parent = self;

        node.prev = child;
        node.next = child.next.take();
        node.next.as_mut().map(|n| n.prev = raw_ptr);
        child.next = Some(node);

        unsafe { &mut *raw_ptr }
    }

    #[inline]
    pub fn insert_child_before<S: Into<String>>(&mut self, name: S, child: &mut Self) -> &mut Self {
        let mut node = Node::new(name.into());
        let raw_ptr: *mut _ = &mut *node;
        node.parent = self;

        let prev_child = child.prev;

        node.prev = child.prev;
        child.prev = raw_ptr;
        unsafe {
            node.next = (*prev_child).next.take();
            (*prev_child).next = Some(node);
        }

        unsafe { &mut *raw_ptr }
    }

    #[inline]
    pub fn append_attribute<S: Into<String>>(&mut self, name: S, value: S) -> &mut Attribute {
        let mut attr = Attribute::new(name, value);
        let raw_ptr: *mut _ = &mut *attr;
        match unsafe { self.last_attr.as_mut() } {
            Some(last_attr) => {
                attr.prev = last_attr;
                last_attr.next = Some(attr);
                self.last_attr = raw_ptr;
            }
            None => {
                self.first_attr = Some(attr);
                self.last_attr = raw_ptr;
            }
        }
        unsafe { &mut *raw_ptr }
    }

    #[inline]
    pub fn prepend_attribute<S: Into<String>>(&mut self, name: S, value: S) -> &mut Attribute {
        let mut attr = Attribute::new(name, value);
        let raw_ptr: *mut _ = &mut *attr;
        match self.first_attr.take() {
            Some(mut first_attr) => {
                first_attr.prev = raw_ptr;
                attr.next = Some(first_attr);
                self.first_attr = Some(attr);
            }
            None => {
                self.first_attr = Some(attr);
                self.last_attr = raw_ptr;
            }
        }
        unsafe { &mut *raw_ptr }
    }

    // xml_attribute xml_node::insert_attribute_after(const char_t* name, const xml_attribute& attr);
    // xml_attribute xml_node::insert_attribute_before(const char_t* name, const xml_attribute& attr);

    // typedef xml_node_iterator iterator;
    // iterator begin() const;
    // iterator end() const;
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn new_node_test() {
        let mut node = Node::new("mpd");
        assert_eq!(node.name(), "mpd");
        node.append_child("child1");
        node.append_child("child2");

        {
            let c1 = node.first_child().unwrap();
            assert_eq!(c1.name(), "child1");

            let c2 = c1.next_sibling().unwrap();
            assert_eq!(c2.name(), "child2");

            let node = c2.parent().unwrap();
            assert_eq!(node.name(), "mpd");
        }

        node.prepend_child("child0");

        {
            let c1 = node.first_child().unwrap();
            assert_eq!(c1.name(), "child0");
        }

        node.append_child_by_type(NodeType::PcData)
            .set_value("text1");

        {
            let txt = node.last_child().unwrap();
            assert_eq!(txt.value(), "text1");
        }
    }

    #[test]
    fn function_chain_test() {
        let mut node = Node::new("mpd");
        assert_eq!(node.name(), "mpd");
        node.append_child("child1").set_value("value1");

        let c1 = node.first_child().unwrap();
        assert_eq!(c1.name(), "child1");
        assert_eq!(c1.value(), "value1");
    }

    #[test]
    fn new_attribute_test() {
        let mut attr = Attribute::new("class", "test");

        //borrow_mut
        {
            assert_eq!(attr.name(), "class");
            assert_eq!(attr.value(), "test");

            attr.set_name("id");
            attr.set_value("main");
        }
    }
}