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
use alloc::string::{ToString, String}; use alloc::vec::Vec; use core::fmt::{self, Debug}; use serde_json::{Value, Map}; use super::{Events, value_to_string_take}; pub enum View { Text(String), Data { kind: String, key: Option<String>, props: Map<String, Value>, events: Events, children: Vec<View>, } } impl Debug for View { #[inline] fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { &View::Text(ref string) => write!(f, "Text({})", string), &View::Data {ref kind, ref key, ref props, ref events, ref children} => { f.debug_struct("Data") .field("kind", kind) .field("key", key) .field("props", props) .field("events", events) .field("children", children) .finish() }, } } } impl Clone for View { #[inline] fn clone(&self) -> Self { match self { &View::Text(ref string) => View::Text(string.clone()), &View::Data {ref kind, ref key, ref props, ref children, ..} => View::Data { kind: kind.clone(), key: key.clone(), props: props.clone(), events: Events::new(), children: children.clone(), }, } } } impl PartialEq for View { #[inline] fn eq(&self, other: &View) -> bool { match self { &View::Text(ref lhs_string) => match other { &View::Text(ref rhs_string) => lhs_string == rhs_string, &View::Data { .. } => false, }, &View::Data { kind: ref lhs_kind, key: ref lhs_key, props: ref lhs_props, events: ref lhs_events, children: ref lhs_children, .. } => match self { &View::Text(_) => false, &View::Data { kind: ref rhs_kind, key: ref rhs_key, props: ref rhs_props, events: ref rhs_events, children: ref rhs_children, } => { lhs_kind == rhs_kind && lhs_key == rhs_key && lhs_props == rhs_props && lhs_events == rhs_events && lhs_children == rhs_children }, }, } } } impl View { #[inline] pub fn new(kind: &str, mut props: Map<String, Value>, events: Events, children: Vec<View>) -> Self { let key = props.remove("key").map(value_to_string_take); View::Data { kind: kind.into(), key: key, props: props, events: events, children: children, } } #[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: Map::new(), events: Events::new(), children: Vec::new(), } } #[inline] pub fn kind(&self) -> Option<&String> { match self { &View::Text(_) => None, &View::Data { ref kind, .. } => Some(kind), } } #[inline] pub fn key(&self) -> Option<&String> { match self { &View::Text(_) => None, &View::Data { ref key, .. } => key.as_ref(), } } #[inline] pub fn key_mut(&mut self) -> Option<&mut String> { match self { &mut View::Text(_) => None, &mut View::Data { ref mut key, .. } => key.as_mut(), } } #[inline] pub fn set_key(&mut self, new_key: String) { match self { &mut View::Text(_) => (), &mut View::Data { ref mut key, .. } => *key = Some(new_key), } } #[inline] pub fn props(&self) -> Option<&Map<String, Value>> { match self { &View::Text(_) => None, &View::Data { ref props, .. } => Some(props), } } #[inline] pub fn props_mut(&mut self) -> Option<&mut Map<String, Value>> { match self { &mut View::Text(_) => None, &mut View::Data { ref mut props, .. } => Some(props), } } #[inline] pub fn events(&self) -> Option<&Events> { match self { &View::Text(_) => None, &View::Data { ref events, .. } => Some(events), } } #[inline] pub fn events_mut(&mut self) -> Option<&mut Events> { match self { &mut View::Text(_) => None, &mut View::Data { ref mut events, .. } => Some(events), } } #[inline] pub fn children(&self) -> Option<&Vec<View>> { match self { &View::Text(_) => None, &View::Data { ref children, .. } => Some(children), } } #[inline] pub fn children_mut(&mut self) -> Option<&mut Vec<View>> { match self { &mut View::Text(_) => None, &mut View::Data { ref mut children, .. } => Some(children), } } } impl<T> From<T> for View where T: ToString, { #[inline] fn from(value: T) -> Self { View::Text(value.to_string()) } }