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
use std::sync::Arc;

use super::super::Props;
use super::{Children, Component, ViewKind};

#[derive(Debug, Clone, PartialEq)]
pub enum View {
    Text(String),
    Data {
        kind: ViewKind,
        key: Option<String>,
        props: Props,
        children: Children,
    },
}

unsafe impl Sync for View {}
unsafe impl Send for View {}

impl View {
    #[inline]
    pub fn new(kind: ViewKind, mut props: Props, children: Children) -> Self {
        let key = props.remove("key").map(|p| match p.take_string() {
            Ok(string) => string,
            Err(p) => p.to_string(),
        });

        View::Data {
            kind: kind,
            key: key,
            props: props,
            children: children,
        }
    }

    #[inline]
    pub fn new_empty() -> Self {
        View::Text(String::new())
    }
    #[inline]
    pub fn new_text(text: &str) -> Self {
        View::Text(text.into())
    }
    #[inline]
    pub fn new_data(kind: &str) -> Self {
        View::Data {
            kind: kind.into(),
            key: None,
            props: Props::new(),
            children: Children::new(),
        }
    }
    #[inline]
    pub fn new_component<T>(component: T) -> Self
    where
        T: Component,
    {
        View::Data {
            kind: component.into(),
            key: None,
            props: Props::new(),
            children: Children::new(),
        }
    }

    #[inline]
    pub fn is_text(&self) -> bool {
        match self {
            &View::Text(_) => true,
            &View::Data { .. } => false,
        }
    }
    #[inline]
    pub fn is_data(&self) -> bool {
        match self {
            &View::Text(_) => false,
            &View::Data { .. } => true,
        }
    }

    #[inline]
    pub fn kind(&self) -> Option<&ViewKind> {
        match self {
            &View::Text(_) => None,
            &View::Data { ref kind, .. } => Some(kind),
        }
    }
    #[inline]
    pub fn tag(&self) -> Option<&String> {
        match self.kind() {
            Some(&ViewKind::String(ref string)) => Some(string),
            _ => None,
        }
    }
    #[inline]
    pub fn component(&self) -> Option<&Arc<Component>> {
        match self.kind() {
            Some(&ViewKind::Component(ref component)) => Some(component),
            _ => None,
        }
    }

    #[inline]
    pub fn has_key(&self) -> bool {
        match self {
            &View::Text(_) => false,
            &View::Data { ref key, .. } => key.is_some(),
        }
    }
    #[inline]
    pub fn clone_key(&self) -> Option<String> {
        match self {
            &View::Text(_) => None,
            &View::Data { ref key, .. } => key.clone(),
        }
    }
    #[inline]
    pub fn key(&self) -> Option<&String> {
        match self {
            &View::Text(_) => None,
            &View::Data { ref key, .. } => key.as_ref(),
        }
    }
    #[inline]
    pub fn set_key(&mut self, new_key: String) {
        self.set_key_option(Some(new_key));
    }
    #[inline]
    pub fn set_key_option(&mut self, new_key: Option<String>) {
        match self {
            &mut View::Text(_) => (),
            &mut View::Data { ref mut key, .. } => *key = new_key,
        }
    }

    #[inline]
    pub fn props(&self) -> Option<&Props> {
        match self {
            &View::Text(_) => None,
            &View::Data { ref props, .. } => Some(props),
        }
    }
    #[inline]
    pub fn props_mut(&mut self) -> Option<&mut Props> {
        match self {
            &mut View::Text(_) => None,
            &mut View::Data { ref mut props, .. } => Some(props),
        }
    }

    #[inline]
    pub fn children(&self) -> Option<&Children> {
        match self {
            &View::Text(_) => None,
            &View::Data { ref children, .. } => Some(children),
        }
    }
    #[inline]
    pub fn children_mut(&mut self) -> Option<&mut Children> {
        match self {
            &mut View::Text(_) => None,
            &mut View::Data {
                ref mut children, ..
            } => Some(children),
        }
    }
    #[inline]
    pub fn clone_no_children(&self) -> Self {
        match self {
            &View::Text(ref string) => View::Text(string.clone()),
            &View::Data {
                ref kind,
                ref key,
                ref props,
                ..
            } => View::Data {
                kind: kind.clone(),
                key: key.clone(),
                props: props.clone(),
                children: Children::new(),
            },
        }
    }
}

impl<'a> From<&'a View> for View {
    #[inline]
    fn from(view: &'a View) -> Self {
        view.clone()
    }
}

impl<T> From<T> for View
where
    T: ToString,
{
    #[inline]
    fn from(value: T) -> Self {
        View::Text(value.to_string())
    }
}