wz_reader 0.0.19

A wz file reader to resolve wz file with thread safe
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
use crate::{
    directory, file, ms, property, util, util::node_util, wz_image, MsFile,
    SharedWzStringDecryptor, WzFile, WzImage, WzNodeCast, WzNodeName, WzObjectType,
};
use hashbrown::HashMap;
use std::path::Path;
use std::sync::{Arc, RwLock, Weak};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error("Node has been using")]
    NodeHasBeenUsing,

    #[error("Error parsing WzDirectory: {0}")]
    WzDirectoryParseError(#[from] directory::Error),

    #[error("Error parsing WzFile: {0}")]
    WzFileParseError(#[from] file::Error),

    #[error("Error parsing MsFile: {0}")]
    MsFileParseError(#[from] ms::file::Error),

    #[error("Error parsing WzImage: {0}")]
    WzImageParseError(#[from] wz_image::Error),

    #[error("Node not found")]
    NodeNotFound,
}

/// A basic unit of wz_reader
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug)]
pub struct WzNode {
    pub name: WzNodeName,
    #[cfg_attr(feature = "serde", serde(flatten))]
    pub object_type: WzObjectType,
    #[cfg_attr(feature = "serde", serde(skip))]
    pub parent: Weak<RwLock<WzNode>>,
    #[cfg_attr(feature = "serde", serde(with = "arc_node_serde"))]
    pub children: HashMap<WzNodeName, Arc<RwLock<WzNode>>>,
}

pub type WzNodeArc = Arc<RwLock<WzNode>>;
pub type WzNodeArcVec = Vec<(WzNodeName, WzNodeArc)>;

impl From<WzNode> for WzNodeArc {
    fn from(node: WzNode) -> Self {
        node.into_lock()
    }
}

impl WzNode {
    pub fn new(
        name: &WzNodeName,
        object_type: impl Into<WzObjectType>,
        parent: Option<&WzNodeArc>,
    ) -> Self {
        Self {
            name: name.clone(),
            object_type: object_type.into(),
            parent: parent.map(Arc::downgrade).unwrap_or_default(),
            children: HashMap::new(),
        }
    }

    pub fn empty() -> Self {
        Self {
            name: WzNodeName::default(),
            object_type: WzObjectType::Value(property::WzValue::Null),
            parent: Weak::new(),
            children: HashMap::new(),
        }
    }

    /// Create a `WzNode` use &str as name.
    pub fn from_str(
        name: &str,
        object_type: impl Into<WzObjectType>,
        parent: Option<&WzNodeArc>,
    ) -> Self {
        Self::new(&name.into(), object_type, parent)
    }

    /// Create a `WzNode` from a any `.wz` file. If version is not provided, it will try to detect the version.
    ///
    /// # Errors
    /// When not provid version and unable to detect it. Or it not valid WzFile(not contain valid header).
    pub fn from_wz_file_full<P>(
        path: P,
        version: Option<util::version::WzMapleVersion>,
        patch_version: Option<i32>,
        parent: Option<&WzNodeArc>,
        existing_key: Option<&SharedWzStringDecryptor>,
    ) -> Result<Self, Error>
    where
        P: AsRef<Path>,
    {
        let name = path.as_ref().file_stem().unwrap().to_str().unwrap();
        let wz_file = WzFile::from_file(
            &path,
            version.map(util::version::get_iv_by_maple_version),
            patch_version,
            existing_key,
        )?;
        Ok(WzNode::new(&name.into(), wz_file, parent))
    }
    /// from_wz_file_full with less argements.
    ///
    /// # Errors
    /// When unable to detect iv and patch version. Or it not valid WzFile(not contain valid header).
    pub fn from_wz_file<P>(path: P, parent: Option<&WzNodeArc>) -> Result<Self, Error>
    where
        P: AsRef<Path>,
    {
        Self::from_wz_file_full(path, None, None, parent, None)
    }

    /// Create a `WzNode` from a any `.ms` file.
    ///
    /// # Errors
    /// When it not valid MsFile(not contain valid header).
    pub fn from_ms_file<P>(path: P, parent: Option<&WzNodeArc>) -> Result<Self, Error>
    where
        P: AsRef<Path>,
    {
        let name = path.as_ref().file_stem().unwrap().to_str().unwrap();
        let ms_file = MsFile::from_file(&path)?;
        Ok(WzNode::new(&name.into(), ms_file, parent))
    }

    /// Create a `WzNode` from a any `.img` file. If version is not provided, it will try to detect the version.
    ///
    /// # Errors
    /// When provided version is incorrect or unable to detect version.
    pub fn from_img_file<P>(
        path: P,
        version: Option<util::version::WzMapleVersion>,
        parent: Option<&WzNodeArc>,
    ) -> Result<Self, Error>
    where
        P: AsRef<Path>,
    {
        let wz_image =
            WzImage::from_file(&path, version.map(util::version::get_iv_by_maple_version))?;
        Ok(WzNode::new(&wz_image.name.clone(), wz_image, parent))
    }

    /// Create a `WzNode` from a any `.img` file with custom wz iv([u8; 4])
    ///
    /// # Errors
    /// When provided iv is incorrect or unable to detect version.
    pub fn from_img_file_with_iv<P>(
        path: P,
        iv: [u8; 4],
        parent: Option<&WzNodeArc>,
    ) -> Result<Self, Error>
    where
        P: AsRef<Path>,
    {
        let wz_image = WzImage::from_file(&path, Some(iv))?;
        Ok(WzNode::new(&wz_image.name.clone(), wz_image, parent))
    }

    /// A quicker way to turn `WzNode` to `WzNodeArc`.
    #[inline]
    pub fn into_lock(self) -> WzNodeArc {
        Arc::new(RwLock::new(self))
    }

    /// Parse the node base on the object type.
    pub fn parse(&mut self, parent: &WzNodeArc) -> Result<(), Error> {
        let (childs, uol_nodes): (WzNodeArcVec, Vec<WzNodeArc>) = match self.object_type {
            WzObjectType::Directory(ref mut directory) => {
                if directory.is_parsed {
                    return Ok(());
                }
                let childs = directory.resolve_children(parent)?;
                directory.is_parsed = true;
                (childs, vec![])
            }
            WzObjectType::File(ref mut file) => {
                if file.is_parsed {
                    return Ok(());
                }
                let childs = file.parse(parent, None)?;
                file.is_parsed = true;
                (childs, vec![])
            }
            WzObjectType::MsFile(ref mut file) => {
                if file.is_parsed {
                    return Ok(());
                }
                let childs = file.parse(parent)?;
                file.is_parsed = true;
                (childs, vec![])
            }
            WzObjectType::Image(ref mut image) => {
                if image.is_parsed {
                    return Ok(());
                }
                let result = image.resolve_children(Some(parent))?;
                image.is_parsed = true;
                result
            }
            WzObjectType::MsImage(ref mut image) => {
                let mut image = image.to_wz_image();
                let result = image.resolve_children(Some(parent))?;
                image.is_parsed = true;
                self.object_type = image.into();
                result
            }
            _ => return Ok(()),
        };

        self.children.reserve(childs.len());

        for (name, child) in childs {
            self.children.insert(name, child);
        }

        for node in uol_nodes {
            node_util::resolve_uol(&node, Some(self));
        }

        Ok(())
    }

    /// Clear the node childrens and set the node to unparsed.
    #[inline]
    pub fn unparse(&mut self) {
        match &mut self.object_type {
            WzObjectType::Directory(directory) => {
                directory.is_parsed = false;
            }
            WzObjectType::File(file) => {
                file.is_parsed = false;
            }
            WzObjectType::Image(image) => {
                image.is_parsed = false;
            }
            _ => return,
        }

        self.children.clear();
    }

    /// Add a child to the node. It just shorten the `node.write().unwrap().children.insert(name, child)`.
    /// If you have a lot node need to add, consider mauanlly `let mut = node.write().unwrap()`.
    #[inline]
    pub fn add(&mut self, node: &WzNodeArc) {
        self.children
            .insert(node.read().unwrap().name.clone(), Arc::clone(node));
    }

    /// Returns the full path of the WzNode.
    ///
    /// # Examples
    ///
    /// ```
    /// # use wz_reader::{WzObjectType, WzNode};
    /// # use wz_reader::property::WzValue;
    /// let root = WzNode::from_str("root", 1, None).into_lock();
    /// let child = WzNode::from_str("1", 1, Some(&root)).into_lock();
    /// let grandchild = WzNode::from_str("2", 1, Some(&child)).into_lock();
    ///
    /// assert_eq!(grandchild.read().unwrap().get_full_path(), "root/1/2");
    /// ```
    pub fn get_full_path(&self) -> String {
        let mut path = self.name.to_string();
        let mut parent = self.parent.upgrade();
        while let Some(parent_inner) = parent {
            let read = parent_inner.read().unwrap();
            path = format!("{}/{}", &read.name, path);
            parent = read.parent.upgrade();
        }
        path
    }

    /// Returns the path of the WzNode but start from root. It useful when you need this path later to find this node from root.
    ///
    /// # Examples
    ///
    /// ```
    /// # use wz_reader::{WzObjectType, WzNode};
    /// # use wz_reader::property::WzValue;
    /// let root = WzNode::from_str("root", 1, None).into_lock();
    /// let child = WzNode::from_str("1", 1, Some(&root)).into_lock();
    /// let grandchild = WzNode::from_str("2", 1, Some(&child)).into_lock();
    ///
    /// assert_eq!(grandchild.read().unwrap().get_path_from_root(), "1/2");
    /// assert!(root.read().unwrap().get_path_from_root().is_empty());
    /// ```
    pub fn get_path_from_root(&self) -> String {
        let mut parent = self.parent.upgrade();

        if parent.is_none() {
            return String::new();
        }

        let mut path = self.name.to_string();

        while let Some(parent_inner) = parent {
            let read = parent_inner.read().unwrap();
            parent = read.parent.upgrade();
            if parent.is_some() {
                path = format!("{}/{}", &read.name, path);
            }
        }
        path
    }

    /// Returns the path of the WzNode but start from WzImage.
    ///
    /// # Examples
    ///
    /// ```
    /// # use wz_reader::{WzObjectType, WzNode, WzImage};
    /// # use wz_reader::property::WzValue;
    /// let root = WzNode::from_str("root", 1, None).into_lock();
    /// let child = WzNode::from_str("1", WzImage::default(), Some(&root)).into_lock();
    /// let grandchild = WzNode::from_str("2", 1, Some(&child)).into_lock();
    ///
    /// assert_eq!(grandchild.read().unwrap().get_path_from_image(), "2");
    /// assert!(root.read().unwrap().get_path_from_image().is_empty());
    /// ```
    pub fn get_path_from_image(&self) -> String {
        let mut parent = self.parent.upgrade();

        if parent.is_none() {
            return String::new();
        }

        let mut path = self.name.to_string();

        while let Some(parent_inner) = parent {
            let read = parent_inner.read().unwrap();

            if read.try_as_image().is_some() {
                return path;
            }

            parent = read.parent.upgrade();

            if parent.is_none() {
                return String::new();
            } else {
                path = format!("{}/{}", &read.name, path);
            }
        }

        String::new()
    }

    /// A alias to get child.
    ///
    /// # Examples
    ///
    /// ```
    /// # use wz_reader::{WzObjectType, WzNode};
    /// # use wz_reader::property::WzValue;
    /// let root = WzNode::from_str("root", 1, None).into_lock();
    /// let child1 = WzNode::from_str("1", 1, Some(&root)).into_lock();
    /// let child2 = WzNode::from_str("2", 1, Some(&root)).into_lock();
    ///
    /// let mut root =  root.write().unwrap();
    /// root.add(&child1);
    /// root.add(&child2);
    ///
    /// assert!(root.at("1").is_some());
    /// assert!(root.at("3").is_none());
    /// ```
    #[inline]
    pub fn at(&self, name: &str) -> Option<WzNodeArc> {
        self.children.get(name).cloned()
    }

    /// A relative path version of `at` able to use `..` to get parent node.
    ///
    /// # Examples
    ///
    /// ```
    /// # use wz_reader::{WzObjectType, WzNode};
    /// # use wz_reader::property::WzValue;
    /// # use std::sync::Arc;
    /// let root = WzNode::from_str("root", 1, None).into_lock();
    /// let child1 = WzNode::from_str("1", 1, Some(&root)).into_lock();
    /// let child2 = WzNode::from_str("2", 1, Some(&root)).into_lock();
    ///
    /// let mut root =  root.write().unwrap();
    /// root.add(&child1);
    /// root.add(&child2);
    ///
    /// assert!(child1.read().unwrap().at_relative("..").is_some());
    /// assert!(root.at_relative("..").is_none());
    /// ```
    #[inline]
    pub fn at_relative(&self, path: &str) -> Option<WzNodeArc> {
        if path == ".." {
            self.parent.upgrade()
        } else {
            self.at(path)
        }
    }
    /// Get node by path like `a/b/c`.
    ///
    /// # Examples
    ///
    /// ```
    /// # use wz_reader::{WzObjectType, WzNode};
    /// # use wz_reader::property::WzValue;
    /// let root = WzNode::from_str("root", 1, None).into_lock();
    /// let child1 = WzNode::from_str("1", 1, Some(&root)).into_lock();
    /// let child2 = WzNode::from_str("2", 1, Some(&child1)).into_lock();
    ///
    /// root.write().unwrap().add(&child1);
    /// child1.write().unwrap().add(&child2);
    ///
    /// assert!(root.read().unwrap().at_path("1/2").is_some());
    /// assert!(root.read().unwrap().at_path("1/3").is_none());
    /// ```
    pub fn at_path(&self, path: &str) -> Option<WzNodeArc> {
        let mut pathes = path.split('/');
        let first = self.at(pathes.next().unwrap());
        if let Some(first) = first {
            pathes.try_fold(first, |node, name| node.read().unwrap().at(name))
        } else {
            None
        }
    }
    /// Get node by path like `a/b/c` and parse all nodes in the path.
    pub fn at_path_parsed(&self, path: &str) -> Result<WzNodeArc, Error> {
        let mut pathes = path.split('/');

        let first = self.at(pathes.next().unwrap());
        if let Some(first) = first {
            pathes.try_fold(first, |node, name| {
                let mut write = node.write().unwrap();
                write.parse(&node)?;
                write.at(name).ok_or(Error::NodeNotFound)
            })
        } else {
            Err(Error::NodeNotFound)
        }
    }
    /// Get node by path that include relative path like `../../b/c`.
    ///
    /// # Examples
    ///
    /// ```
    /// # use wz_reader::{WzObjectType, WzNode};
    /// # use wz_reader::property::WzValue;
    /// let root = WzNode::from_str("root", 1, None).into_lock();
    /// let child1 = WzNode::from_str("1", 1, Some(&root)).into_lock();
    /// let child2 = WzNode::from_str("2", 1, Some(&child1)).into_lock();
    ///
    /// root.write().unwrap().add(&child1);
    /// child1.write().unwrap().add(&child2);
    ///
    /// assert!(child2.read().unwrap().at_path_relative("../..").is_some());
    /// assert!(child2.read().unwrap().at_path_relative("../3").is_none());
    /// ```
    pub fn at_path_relative(&self, path: &str) -> Option<WzNodeArc> {
        let mut pathes = path.split('/');
        let first = self.at_relative(pathes.next().unwrap());
        if let Some(first) = first {
            pathes.try_fold(first, |node, name| node.read().unwrap().at_relative(name))
        } else {
            None
        }
    }

    /// Get parent node by filter.
    ///
    /// # Examples
    ///
    /// ```
    /// # use wz_reader::{WzObjectType, WzNode};
    /// # use wz_reader::property::WzValue;
    /// let root = WzNode::from_str("root", 1, None).into_lock();
    /// let child1 = WzNode::from_str("1", 1, Some(&root)).into_lock();
    /// let child2 = WzNode::from_str("2", 1, Some(&child1)).into_lock();
    ///
    /// root.write().unwrap().add(&child1);
    /// child1.write().unwrap().add(&child2);
    ///
    /// let target = child2.read().unwrap().filter_parent(|node| node.name.as_str() == "root");
    /// assert!(target.is_some());
    /// ```
    pub fn filter_parent<F>(&self, cb: F) -> Option<WzNodeArc>
    where
        F: Fn(&WzNode) -> bool,
    {
        let mut parent = self.parent.upgrade();
        loop {
            if let Some(parent_inner) = parent {
                let read = parent_inner.read().unwrap();
                if cb(&read) {
                    break Some(Arc::clone(&parent_inner));
                } else {
                    parent = read.parent.upgrade();
                }
            } else {
                break None;
            }
        }
    }

    #[inline]
    pub fn get_parent_wz_image(&self) -> Option<WzNodeArc> {
        self.filter_parent(|node| matches!(node.object_type, WzObjectType::Image(_)))
    }

    #[inline]
    pub fn get_base_wz_file(&self) -> Option<WzNodeArc> {
        self.filter_parent(|node| {
            matches!(node.object_type, WzObjectType::File(_)) && node.name.as_str() == "Base"
        })
    }

    /// Transfer all children to another node. It will merge the children instead of replace to new one.
    pub fn transfer_childs(&mut self, to: &WzNodeArc) {
        let mut write = to.write().unwrap();
        write.children.reserve(self.children.len());
        for (name, child) in self.children.drain() {
            if let Some(old) = write.children.get(&name) {
                child.write().unwrap().transfer_childs(old);
            } else {
                child.write().unwrap().parent = Arc::downgrade(to);
                write.children.insert(name, child);
            }
        }
    }

    /// Generate full json that can deserialize back to `WzNode`.
    #[cfg(feature = "json")]
    #[inline]
    pub fn to_json(&self) -> Result<serde_json::Value, serde_json::Error> {
        serde_json::to_value(self)
    }

    /// Generate simple json only name and value.
    #[cfg(feature = "json")]
    pub fn to_simple_json(&self) -> Result<serde_json::Value, serde_json::Error> {
        use crate::property::WzSubProperty;
        use serde_json::{to_value, Map, Value};

        if self.children.is_empty() {
            match &self.object_type {
                WzObjectType::Value(value_type) => return Ok(value_type.clone().into()),
                WzObjectType::Property(WzSubProperty::PNG(inner)) => return to_value(inner),
                WzObjectType::Property(WzSubProperty::Sound(inner)) => return to_value(inner),
                _ => return Ok(Value::Null),
            }
        }

        let mut json = Map::new();

        match &self.object_type {
            WzObjectType::Property(WzSubProperty::PNG(inner)) => {
                let dict = to_value(inner)?;

                if let Value::Object(dict) = dict {
                    for (name, value) in dict {
                        json.insert(name, value);
                    }
                }
            }
            WzObjectType::Property(WzSubProperty::Sound(inner)) => {
                let dict = to_value(inner)?;

                if let Value::Object(dict) = dict {
                    for (name, value) in dict {
                        json.insert(name, value);
                    }
                }
            }
            _ => {}
        }

        for (name, value) in self.children.iter() {
            let child = value.read().unwrap();
            json.insert(name.to_string(), child.to_simple_json()?);
        }

        Ok(Value::Object(json))
    }
}

#[cfg(feature = "serde")]
mod arc_node_serde {
    use crate::WzNodeName;
    use hashbrown::HashMap;
    use serde::de::Deserializer;
    use serde::ser::{SerializeMap, Serializer};
    use serde::{Deserialize, Serialize};
    use std::sync::{Arc, RwLock};

    pub fn serialize<S, T>(
        val: &HashMap<WzNodeName, Arc<RwLock<T>>>,
        s: S,
    ) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
        T: Serialize,
    {
        let mut map = s.serialize_map(Some(val.len()))?;
        for (k, v) in val {
            map.serialize_entry(k, &*v.read().unwrap())?;
        }
        map.end()
    }

    pub fn deserialize<'de, D, T>(d: D) -> Result<HashMap<WzNodeName, Arc<RwLock<T>>>, D::Error>
    where
        D: Deserializer<'de>,
        T: Deserialize<'de>,
    {
        let map = HashMap::<WzNodeName, T>::deserialize(d)?;
        Ok(map
            .into_iter()
            .map(|(k, v)| (k, Arc::new(RwLock::new(v))))
            .collect())
    }
}

#[cfg(feature = "serde")]
#[cfg(test)]
mod test {
    use super::*;
    use crate::util::node_util;

    #[cfg(feature = "serde")]
    use serde_json::json;

    #[test]
    fn test_serialize_wz_node() {
        let root = WzNode::from_str("root", 1, None).into_lock();
        let child1 = WzNode::from_str("1", 1, Some(&root)).into_lock();
        let child2 = WzNode::from_str("2", 1, Some(&root)).into_lock();
        root.write().unwrap().add(&child1);
        root.write().unwrap().add(&child2);
        let child11 = WzNode::from_str("1-1", 1, Some(&child1)).into_lock();
        let child12 = WzNode::from_str("1-2", 1, Some(&child1)).into_lock();
        child1.write().unwrap().add(&child11);
        child1.write().unwrap().add(&child12);
        let child111 = WzNode::from_str("1-1-1", 1, Some(&child11)).into_lock();
        child11.write().unwrap().add(&child111);

        let json = serde_json::to_value(&*root.read().unwrap()).unwrap();

        let result = json!({
            "name": "root",
            "type": "Int",
            "data": 1,
            "children": {
                "1": {
                    "name": "1",
                    "type": "Int",
                    "data": 1,
                    "children": {
                        "1-1": {
                            "name": "1-1",
                            "type": "Int",
                            "data": 1,
                            "children": {
                                "1-1-1": {
                                    "name": "1-1-1",
                                    "type": "Int",
                                    "data": 1,
                                    "children": {}
                                }
                            }
                        },
                        "1-2": {
                            "name": "1-2",
                            "type": "Int",
                            "data": 1,
                            "children": {}
                        }
                    }
                },
                "2": {
                    "name": "2",
                    "type": "Int",
                    "data": 1,
                    "children": {}
                }
            }
        });

        assert_eq!(json, result);
    }

    #[test]
    fn test_deserialize_wz_node() {
        let source = json!({
            "name": "root",
            "type": "Int",
            "data": 1,
            "children": {
                "1": {
                    "name": "1",
                    "type": "Int",
                    "data": 1,
                    "children": {
                        "1-1": {
                            "name": "1-1",
                            "type": "Int",
                            "data": 1,
                            "children": {
                                "1-1-1": {
                                    "name": "1-1-1",
                                    "type": "Int",
                                    "data": 1,
                                    "children": {}
                                }
                            }
                        },
                        "1-2": {
                            "name": "1-2",
                            "type": "Int",
                            "data": 1,
                            "children": {}
                        }
                    }
                },
                "2": {
                    "name": "2",
                    "type": "Int",
                    "data": 1,
                    "children": {}
                }
            }
        });

        let root = serde_json::from_value::<WzNode>(source)
            .unwrap()
            .into_lock();

        let node111 = root.read().unwrap().at_path("1/1-1/1-1-1");

        assert!(node111.is_some());

        let node111 = node111.unwrap();

        assert_eq!(node111.read().unwrap().name.as_str(), "1-1-1");
        // should not be able to resolve parent
        assert!(node111.read().unwrap().parent.upgrade().is_none());

        node_util::resolve_childs_parent(&root);

        // should able to get parent after resolved
        let node111_parent = node111.read().unwrap().parent.upgrade();
        assert!(node111_parent.is_some());

        let node111_parent = node111_parent.unwrap();

        assert_eq!(node111_parent.read().unwrap().name.as_str(), "1-1");
    }

    #[cfg(feature = "json")]
    #[test]
    fn test_node_to_simple_json() {
        use crate::property::{WzPng, WzSound};

        let root = WzNode::from_str("root", 1, None).into_lock();
        let child1 = WzNode::from_str("1", 1, Some(&root)).into_lock();
        let child2 = WzNode::from_str("2", 1, Some(&root)).into_lock();
        root.write().unwrap().add(&child1);
        root.write().unwrap().add(&child2);
        let child11 = WzNode::from_str("1-1", 1, Some(&child1)).into_lock();
        let child12 = WzNode::from_str("1-2", 1, Some(&child1)).into_lock();
        child1.write().unwrap().add(&child11);
        child1.write().unwrap().add(&child12);
        let child111 = WzNode::from_str("1-1-1", 1, Some(&child11)).into_lock();
        let child112 = WzNode::from_str("1-1-2", 2, Some(&child11)).into_lock();
        let child11png = WzNode::from_str("1-1-png", WzPng::default(), Some(&child11)).into_lock();
        let child11sound =
            WzNode::from_str("1-1-sound", WzSound::default(), Some(&child11)).into_lock();
        child11.write().unwrap().add(&child111);
        child11.write().unwrap().add(&child112);
        child11.write().unwrap().add(&child11png);
        child11.write().unwrap().add(&child11sound);
        let child11pngz = WzNode::from_str("1-1-png-z", 2, Some(&child11png)).into_lock();
        child11png.write().unwrap().add(&child11pngz);

        let json = root.read().unwrap().to_simple_json().unwrap();

        let result = json!({
            "1": {
                "1-1": {
                    "1-1-1": 1,
                    "1-1-2": 2,
                    "1-1-png": {
                        "width": 0,
                        "height": 0,
                        "1-1-png-z": 2
                    },
                    "1-1-sound": {
                        "duration": 0,
                        "sound_type": "Binary"
                    }
                },
                "1-2": 1
            },
            "2": 1
        });

        assert_eq!(json, result);
    }
}