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
use serde_json::{Map, Value};

use super::super::{props_to_json, View};

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub enum RawView {
    Text(String),
    Data {
        kind: String,
        key: Option<String>,
        props: Map<String, Value>,
        children: Vec<RawView>,
    },
}

unsafe impl Sync for RawView {}
unsafe impl Send for RawView {}

impl<'a> From<&'a View> for RawView {
    #[inline]
    fn from(view: &'a View) -> Self {
        match view {
            &View::Text(ref text) => RawView::Text(text.clone()),
            &View::Data {
                ref kind,
                ref key,
                ref props,
                ref children,
                ..
            } => RawView::Data {
                kind: kind.to_string(),
                key: key.clone(),
                props: props_to_json(props),
                children: children.iter().map(|child| RawView::from(child)).collect(),
            },
        }
    }
}

impl From<View> for RawView {
    #[inline]
    fn from(view: View) -> Self {
        match view {
            View::Text(text) => RawView::Text(text),
            View::Data {
                kind,
                key,
                props,
                children,
                ..
            } => RawView::Data {
                kind: kind.take_string(),
                key: key,
                props: props_to_json(&props),
                children: children
                    .into_iter()
                    .map(|child| RawView::from(child))
                    .collect(),
            },
        }
    }
}

impl RawView {
    #[inline]
    pub fn kind(&self) -> Option<&String> {
        match self {
            &RawView::Text(_) => None,
            &RawView::Data { ref kind, .. } => Some(kind),
        }
    }
    #[inline]
    pub fn key(&self) -> Option<&String> {
        match self {
            &RawView::Text(_) => None,
            &RawView::Data { ref key, .. } => key.as_ref(),
        }
    }
    #[inline]
    pub fn props(&self) -> Option<&Map<String, Value>> {
        match self {
            &RawView::Text(_) => None,
            &RawView::Data { ref props, .. } => Some(props),
        }
    }
    #[inline]
    pub fn children(&self) -> Option<&Vec<RawView>> {
        match self {
            &RawView::Text(_) => None,
            &RawView::Data { ref children, .. } => Some(children),
        }
    }
}