makepad_live_compiler/
live_node_cbor.rs

1use {
2    std::sync::Arc,
3    crate::{
4        makepad_live_tokenizer::LiveId,
5        live_node::*,
6    }
7};
8
9pub trait LiveNodeSliceToCbor {
10    fn to_cbor(&self, parent_index: usize) -> Result<Vec<u8>, String>;
11}
12
13pub trait LiveNodeVecFromCbor {
14    fn from_cbor(&mut self, buf: &[u8]) -> Result<(), LiveNodeFromCborError>;
15}
16
17//0x00..0x17	unsigned integer 0x00..0x17 (0..23)
18const CBOR_UINT_START: u8 = 0x00;
19const CBOR_UINT_END: u8 = 0x17;
20const CBOR_U8: u8 = 0x18; //	unsigned integer (one-byte uint8_t follows)
21const CBOR_U16: u8 = 0x19; // unsigned integer (two - byte uint16_t follows)
22const CBOR_U32: u8 = 0x1a; //	unsigned integer (four-byte uint32_t follows)
23const CBOR_U64: u8 = 0x1b; //	unsigned integer (eight-byte uint64_t follows)
24const CBOR_NUINT_START: u8 = 0x20; //..0x37	negative integer -1-0x00..-1-0x17 (-1..-24)
25const CBOR_NUINT_END: u8 = 0x37; //..0x37	negative integer -1-0x00..-1-0x17 (-1..-24)
26const CBOR_NU8: u8 = 0x38; //	negative integer -1-n (one-byte uint8_t for n follows)
27const CBOR_NU16: u8 = 0x39; //	negative integer -1-n (two-byte uint16_t for n follows)
28const CBOR_NU32: u8 = 0x3a; //	negative integer -1-n (four-byte uint32_t for n follows)
29const CBOR_NU64: u8 = 0x3b; //	negative integer -1-n (eight-byte uint64_t for n follows)
30const CBOR_BSTR_START: u8 = 0x40; //..0x57	byte string (0x00..0x17 bytes follow)
31const CBOR_BSTR_END: u8 = 0x57; //..0x57	byte string (0x00..0x17 bytes follow)
32const CBOR_BSTR_8: u8 = 0x58; //	byte string (one-byte uint8_t for n, and then n bytes follow)
33const CBOR_BSTR_16: u8 = 0x59; //	byte string (two-byte uint16_t for n, and then n bytes follow)
34const CBOR_BSTR_32: u8 = 0x5a; //	byte string (four-byte uint32_t for n, and then n bytes follow)
35const CBOR_BSTR_64: u8 = 0x5b; //	byte string (eight-byte uint64_t for n, and then n bytes follow)
36const CBOR_BSTR_BRK: u8 = 0x5f; //	byte string, byte strings follow, terminated by "break"
37const CBOR_UTF8_START: u8 = 0x60; //..0x77	UTF-8 string (0x00..0x17 bytes follow)
38const CBOR_UTF8_END: u8 = 0x77; //..0x77	UTF-8 string (0x00..0x17 bytes follow)
39const CBOR_UTF8_8: u8 = 0x78; //	UTF-8 string (one-byte uint8_t for n, and then n bytes follow)
40const CBOR_UTF8_16: u8 = 0x79; //	UTF-8 string (two-byte uint16_t for n, and then n bytes follow)
41const CBOR_UTF8_32: u8 = 0x7a; //	UTF-8 string (four-byte uint32_t for n, and then n bytes follow)
42const CBOR_UTF8_64: u8 = 0x7b; //	UTF-8 string (eight-byte uint64_t for n, and then n bytes follow)
43const CBOR_UTF8_BRK: u8 = 0x7f; //	UTF-8 string, UTF-8 strings follow, terminated by "break"
44const CBOR_ARRAY_START: u8 = 0x80; //..0x97	array (0x00..0x17 data items follow)
45const CBOR_ARRAY_END: u8 = 0x97; //..0x97	array (0x00..0x17 data items follow)
46const CBOR_ARRAY_8: u8 = 0x98; //	array (one-byte uint8_t for n, and then n data items follow)
47const CBOR_ARRAY_16: u8 = 0x99; //	array (two-byte uint16_t for n, and then n data items follow)
48const CBOR_ARRAY_32: u8 = 0x9a; //	array (four-byte uint32_t for n, and then n data items follow)
49const CBOR_ARRAY_64: u8 = 0x9b; //	array (eight-byte uint64_t for n, and then n data items follow)
50const CBOR_ARRAY_BRK: u8 = 0x9f; //	array, data items follow, terminated by "break"
51const CBOR_MAP_START: u8 = 0xa0; //..0xb7	map (0x00..0x17 pairs of data items follow)
52const CBOR_MAP_END: u8 = 0xb7; //..0xb7	map (0x00..0x17 pairs of data items follow)
53const CBOR_MAP_8: u8 = 0xb8; //	map (one-byte uint8_t for n, and then n pairs of data items follow)
54const CBOR_MAP_16: u8 = 0xb9; //	map (two-byte uint16_t for n, and then n pairs of data items follow)
55const CBOR_MAP_32: u8 = 0xba; //	map (four-byte uint32_t for n, and then n pairs of data items follow)
56const CBOR_MAP_64: u8 = 0xbb; //	map (eight-byte uint64_t for n, and then n pairs of data items follow)
57const CBOR_MAP_BRK: u8 = 0xbf; //	map, pairs of data items follow, terminated by "break"
58const CBOR_TIME_TEXT: u8 = 0xc0; //	text-based date/time (data item follows; see Section 3.4.1)
59const CBOR_TIME_EPOCH: u8 = 0xc1; //	epoch-based date/time (data item follows; see Section 3.4.2)
60const CBOR_UBIGNUM: u8 = 0xc2; //	unsigned bignum (data item "byte string" follows)
61const CBOR_NBIGNUM: u8 = 0xc3; //	negative bignum (data item "byte string" follows)
62const CBOR_DECFRACT: u8 = 0xc4; //	decimal Fraction (data item "array" follows; see Section 3.4.4)
63const CBOR_BIGFLOAT: u8 = 0xc5; //	bigfloat (data item "array" follows; see Section 3.4.4)
64const CBOR_TAG_START: u8 = 0xc6; //..0xd4	(tag)
65const CBOR_TAG_END: u8 = 0xd4; //..0xd4	(tag)
66const CBOR_CONV_START: u8 = 0xd5; //..0xd7	expected conversion (data item follows; see Section 3.4.5.2)
67const CBOR_CONV_END: u8 = 0xd7; //..0xd7	expected conversion (data item follows; see Section 3.4.5.2)
68const CBOR_MTAG_START: u8 = 0xd8; //..0xdb	(more tags; 1/2/4/8 bytes of tag number and then a data item follow)
69const CBOR_MTAG_END: u8 = 0xdb; //..0xdb	(more tags; 1/2/4/8 bytes of tag number and then a data item follow)
70const CBOR_SIMPLE_START: u8 = 0xe0; //..0xf3	(simple value)
71const CBOR_SIMPLE_END: u8 = 0xf3; //..0xf3	(simple value)
72const CBOR_FALSE: u8 = 0xf4; //	false
73const CBOR_TRUE: u8 = 0xf5; //	true
74const CBOR_NULL: u8 = 0xf6; //	null
75const CBOR_UNDEIFNED: u8 = 0xf7; //	undefined
76const CBOR_SIMPLE_8: u8 = 0xf8; //	(simple value, one byte follows)
77const CBOR_FLOAT16: u8 = 0xf9; //	half-precision float (two-byte IEEE 754)
78const CBOR_FLOAT32: u8 = 0xfa; //	single-precision float (four-byte IEEE 754)
79const CBOR_FLOAT64: u8 = 0xfb; //	double-precision float (eight-byte IEEE 754)
80const CBOR_BREAK: u8 = 0xff; //	"break" stop code
81
82/* some Rust keyword abuse here 
83key:{move:{}} // clone
84key:{dyn:{}} // dyn created
85key:{ref:{}} // template
86key:{in:[v1,v2]} // vec
87key:{as:u32} // color
88key {enum:"String"} // enum
89*/
90
91impl<T> LiveNodeSliceToCbor for T where T: AsRef<[LiveNode]> {
92    fn to_cbor(&self, parent_index: usize) -> Result<Vec<u8>, String> {
93        let mut out = Vec::new();
94        let nodes = self.as_ref();
95        let mut index = parent_index;
96        
97        struct StackItem {index: usize, count: usize, has_keys: bool}
98        
99        let mut stack = vec![StackItem {index: 0, count: 0, has_keys: false}];
100        
101        while index < nodes.len() {
102            let node = &nodes[index];
103            
104            if node.value.is_close() {
105                if stack.is_empty() {
106                    return Err("Unmatched closed".into())
107                }
108                let item = stack.pop().unwrap();
109                if item.count > std::u16::MAX as usize {
110                    out[item.index] = if item.has_keys {CBOR_MAP_32}else {CBOR_ARRAY_32};
111                    let bytes = (item.count as u32).to_be_bytes();
112                    out.splice(item.index + 1..item.index + 1, bytes.iter().cloned());
113                }
114                else if item.count > std::u8::MAX as usize {
115                    out[item.index] = if item.has_keys {CBOR_MAP_16}else {CBOR_ARRAY_16};
116                    let bytes = (item.count as u16).to_be_bytes();
117                    out.splice(item.index + 1..item.index + 1, bytes.iter().cloned());
118                }
119                else if item.count >= 32 {
120                    out[item.index] = if item.has_keys {CBOR_MAP_8}else {CBOR_ARRAY_8};
121                    let bytes = (item.count as u8).to_be_bytes();
122                    out.splice(item.index + 1..item.index + 1, bytes.iter().cloned());
123                }
124                else {
125                    out[item.index] += item.count as u8
126                }
127                index += 1;
128                continue;
129            }
130            
131            let item = stack.last_mut().unwrap();
132            item.count += 1;
133            
134            if item.has_keys {
135                encode_id(node.id, &mut out);
136            }
137            
138            fn encode_u32(v: u32, out: &mut Vec<u8>) {
139                if v <= CBOR_UINT_END as u32 {
140                    out.push(v as u8)
141                }
142                else if v <= std::u8::MAX as u32 {
143                    out.push(CBOR_U8);
144                    out.push(v as u8)
145                }
146                else if v <= std::u16::MAX as u32 {
147                    out.push(CBOR_U16);
148                    out.extend_from_slice(&(v as u16).to_be_bytes());
149                }
150                else {
151                    out.push(CBOR_U32);
152                    out.extend_from_slice(&v.to_be_bytes());
153                }
154            }
155            
156            fn encode_u64(v: u64, out: &mut Vec<u8>) {
157                if v <= std::u32::MAX as u64 {
158                    encode_u32(v as u32, out);
159                }
160                else {
161                    out.push(CBOR_U64);
162                    out.extend_from_slice(&v.to_be_bytes());
163                }
164            }
165            
166            fn encode_i64(v: i64, out: &mut Vec<u8>) {
167                if v < 0 {
168                    let v = ((-v) - 1) as u64;
169                    if v <= (CBOR_NUINT_END - CBOR_NUINT_START) as u64 {
170                        out.push(CBOR_NUINT_START + v as u8);
171                    }
172                    else if v <= std::u8::MAX as u64 {
173                        out.push(CBOR_NU8);
174                        out.extend_from_slice(&(v as u8).to_be_bytes());
175                    }
176                    else if v <= std::u16::MAX as u64 {
177                        out.push(CBOR_NU16);
178                        out.extend_from_slice(&(v as u16).to_be_bytes());
179                    }
180                    else if v <= std::u32::MAX as u64 {
181                        out.push(CBOR_NU32);
182                        out.extend_from_slice(&(v as u32).to_be_bytes());
183                    }
184                    else {
185                        out.push(CBOR_NU64);
186                        out.extend_from_slice(&v.to_be_bytes());
187                    }
188                }
189                else {
190                    encode_u64(v as u64, out);
191                }
192            }
193            
194            fn encode_f32(v: f32, out: &mut Vec<u8>) {
195                if v.fract() == 0.0 {
196                    encode_i64(v as i64, out)
197                }
198                else {
199                    out.push(CBOR_FLOAT32);
200                    out.extend_from_slice(&v.to_be_bytes());
201                }
202            }
203            
204            fn encode_f64(v: f64, out: &mut Vec<u8>) {
205                if v.fract() == 0.0 {
206                    encode_i64(v as i64, out)
207                }
208                else {
209                    out.push(CBOR_FLOAT64);
210                    out.extend_from_slice(&v.to_be_bytes());
211                }
212            }
213            
214            fn encode_id(id: LiveId, out: &mut Vec<u8>) {
215                if id.0 & 0x8000_0000_0000_0000 == 0 {
216                    encode_u64(id.0, out);
217                }
218                else {
219                    id.as_string( | v | {
220                        if let Some(v) = v {
221                            encode_str(v, out);
222                        }
223                        else {
224                            encode_u64(id.0, out);
225                        }
226                    });
227                }
228            }
229            
230            let prop_type = node.origin.prop_type();
231            if prop_type != LivePropType::Field && prop_type != LivePropType::Nameless {
232                return Err("Non field types not implemented".into())
233            }
234            
235            fn encode_str(s: &str, out: &mut Vec<u8>) {
236                let len = s.len();
237                if len <= (CBOR_UTF8_END - CBOR_UTF8_START) as usize {
238                    out.push(len as u8 + CBOR_UTF8_START);
239                    out.extend_from_slice(s.as_bytes());
240                }
241                else if len <= std::u8::MAX as usize {
242                    out.push(CBOR_UTF8_8);
243                    out.push(len as u8);
244                    out.extend_from_slice(s.as_bytes());
245                }
246                else if len <= std::u16::MAX as usize {
247                    out.push(CBOR_UTF8_16);
248                    out.extend_from_slice(&(len as u16).to_be_bytes());
249                    out.extend_from_slice(s.as_bytes());
250                }
251                else if len <= std::u32::MAX as usize {
252                    out.push(CBOR_UTF8_32);
253                    out.extend_from_slice(&(len as u32).to_be_bytes());
254                    out.extend_from_slice(s.as_bytes());
255                }
256                else {
257                    out.push(CBOR_UTF8_64);
258                    out.extend_from_slice(&(len as u64).to_be_bytes());
259                    out.extend_from_slice(s.as_bytes());
260                }
261            }
262            //log!("SAVING {:?} {}", node.value, out.len());
263            match &node.value {
264                LiveValue::None => {
265                    out.push(CBOR_NULL);
266                },
267                LiveValue::Str(s) => {
268                    encode_str(s, &mut out);
269                },
270                LiveValue::InlineString(s) => {
271                    encode_str(s.as_str(), &mut out);
272                },
273                LiveValue::String(s) => {
274                    encode_str(s.as_str(), &mut out);
275                },
276                LiveValue::Bool(v) => {
277                    out.push(if *v {CBOR_TRUE} else {CBOR_FALSE});
278                }
279                LiveValue::Int64(v) => {
280                    encode_i64(*v, &mut out);
281                }
282                LiveValue::Uint64(v) => {
283                    encode_u64(*v, &mut out);
284                }
285                LiveValue::Float32(v) => {
286                    encode_f32(*v, &mut out);
287                },
288                LiveValue::Float64(v) => {
289                    encode_f64(*v, &mut out);
290                },
291                LiveValue::Color(v) => {
292                    out.push(1 + CBOR_MAP_START);
293                    encode_str("as", &mut out);
294                    encode_u32(*v, &mut out);
295                },
296                LiveValue::Vec2(v) => {
297                    out.push(1 + CBOR_MAP_START);
298                    encode_str("in", &mut out);
299                    out.push(2 + CBOR_ARRAY_START);
300                    encode_f32(v.x, &mut out);
301                    encode_f32(v.x, &mut out);
302                },
303                LiveValue::Vec3(v) => {
304                    out.push(1 + CBOR_MAP_START);
305                    encode_str("in", &mut out);
306                    out.push(3 + CBOR_ARRAY_START);
307                    encode_f32(v.x, &mut out);
308                    encode_f32(v.x, &mut out);
309                    encode_f32(v.z, &mut out);
310                },
311                LiveValue::Vec4(v) => {
312                    out.push(1 + CBOR_MAP_START);
313                    encode_str("in", &mut out);
314                    out.push(4 + CBOR_ARRAY_START);
315                    encode_f32(v.x, &mut out);
316                    encode_f32(v.x, &mut out);
317                    encode_f32(v.z, &mut out);
318                    encode_f32(v.w, &mut out);
319                },
320                LiveValue::BareEnum(variant) => {
321                    out.push(1 + CBOR_MAP_START);
322                    encode_str("if", &mut out);
323                    encode_id(*variant, &mut out);
324                },
325                LiveValue::Array => {
326                    stack.push(StackItem {index: out.len(), count: 0, has_keys: false});
327                    out.push(CBOR_ARRAY_START);
328                },
329                LiveValue::TupleEnum(variant) => {
330                    out.push(1 + CBOR_MAP_START);
331                    encode_str("enum", &mut out);
332                    out.push(2 + CBOR_ARRAY_START);
333                    encode_id(*variant, &mut out);
334                    stack.push(StackItem {index: out.len(), count: 0, has_keys: false});
335                    out.push(CBOR_ARRAY_START);
336                },
337                LiveValue::NamedEnum(variant) => {
338                    out.push(1 + CBOR_MAP_START);
339                    encode_str("enum", &mut out);
340                    out.push(2 + CBOR_ARRAY_START);
341                    encode_id(*variant, &mut out);
342                    stack.push(StackItem {index: out.len(), count: 0, has_keys: true});
343                    out.push(CBOR_MAP_START);
344                },
345                LiveValue::Object => {
346                    stack.push(StackItem {index: out.len(), count: 0, has_keys: true});
347                    out.push(CBOR_MAP_START);
348                }, // subnodes including this one
349                LiveValue::Close => {},
350                // TODO ITEMS
351                LiveValue::Id(_) => {
352                    return Err("Cannot serialise LiveValue::Id".into())
353                },
354                LiveValue::Clone{..} => {
355                    return Err("Cannot serialise LiveValue::Clone".into())
356                }, // subnodes including this one
357                LiveValue::ExprBinOp(_) => {
358                    return Err("Cannot serialise LiveValue::ExprBinOp".into())
359                },
360                LiveValue::ExprUnOp(_) => {
361                    return Err("Cannot serialise LiveValue::ExprUnOp".into())
362                },
363                LiveValue::ExprMember(_) => {
364                    return Err("Cannot serialise LiveValue::ExprMember".into())
365                },
366                LiveValue::Expr {..} => {
367                    return Err("Cannot serialise LiveValue::Expr".into())
368                },
369                LiveValue::ExprCall {..} => {
370                    return Err("Cannot serialise LiveValue::ExprCall".into())
371                },
372                LiveValue::Dependency {..} => {
373                    return Err("Cannot serialise LiveValue::Dependency".into())
374                },
375                LiveValue::Class {..} => {
376                    return Err("Cannot serialise LiveValue::Class".into())
377                }, // subnodes including this one
378                LiveValue::Deref {..} => {
379                    return Err("Cannot serialise LiveValue::Deref".into())
380                }, // subnodes including this one
381                LiveValue::DSL {..} => {
382                    return Err("Cannot serialise LiveValue::DSL".into())
383                },
384                LiveValue::Import(..) => {
385                    return Err("Cannot serialise LiveValue::Import".into())
386                }
387                LiveValue::Root{..} => {
388                    return Err("Cannot serialise LiveValue::Registry".into())
389                }
390                LiveValue::IdPath(..) => {
391                    return Err("Cannot serialise LiveValue::IdPath".into())
392                }
393                LiveValue::Font {..} => {
394                    return Err("Cannot serialise LiveValue::Font".into())
395                },
396            }
397            index += 1;
398        }
399        if stack.len() > 1 {
400            return Err("Uneven stack, not enough closes".into())
401        }
402        Ok(out)
403    }
404}
405
406// todo: pack these in somehow
407/*
408const BIN_EXPR_BIN_OP: u8 = 27;
409const BIN_EXPR_UN_OP: u8 = 28;
410const BIN_EXPR_MEMBER: u8 = 29;
411const BIN_EXPR: u8 = 30;
412const BIN_EXPR_CALL: u8 = 31;
413*/
414
415// compressed number values
416
417#[derive(Debug)]
418pub enum LiveNodeFromCborError {
419    OutOfBounds,
420    UnexpectedVariant,
421    LiveIdCollision,
422    ExpectedId,
423    NotImpl,
424    UnexpectedValue,
425    ExpectedBareEnumString,
426    StackNotClosed,
427    UTF8Error
428}
429
430impl LiveNodeVecFromCbor for Vec<LiveNode> {
431    
432    fn from_cbor(&mut self, data: &[u8]) -> Result<(), LiveNodeFromCborError> {
433        // alright lets decode msgpack livenodes
434        
435        fn assert_len(o: usize, len: usize, data: &[u8]) -> Result<(), LiveNodeFromCborError> {
436            if o + len > data.len() {panic!()} //return Err(LiveNodeFromBinaryError::OutOfBounds);}
437            Ok(())
438        }
439        
440        fn read_u8(data: &[u8], o: &mut usize) -> Result<u8, LiveNodeFromCborError> {
441            assert_len(*o, 1, data) ?;
442            let d = data[*o];
443            *o += 1;
444            Ok(d)
445        }
446        
447        fn read_u16(data: &[u8], o: &mut usize) -> Result<u16, LiveNodeFromCborError> {
448            assert_len(*o, 2, data) ?;
449            let d = u16::from_be_bytes(data[*o..*o + 2].try_into().unwrap());
450            *o += 2;
451            Ok(d)
452        }
453        
454        fn read_u32(data: &[u8], o: &mut usize) -> Result<u32, LiveNodeFromCborError> {
455            assert_len(*o, 4, data) ?;
456            let d = u32::from_be_bytes(data[*o..*o + 4].try_into().unwrap());
457            *o += 4;
458            Ok(d)
459        }
460        
461        fn read_u64(data: &[u8], o: &mut usize) -> Result<u64, LiveNodeFromCborError> {
462            assert_len(*o, 8, data) ?;
463            let d = u64::from_be_bytes(data[*o..*o + 8].try_into().unwrap());
464            *o += 8;
465            Ok(d)
466        }
467        
468        fn read_i8(data: &[u8], o: &mut usize) -> Result<i8, LiveNodeFromCborError> {
469            assert_len(*o, 1, data) ?;
470            let d = i8::from_be_bytes(data[*o..*o + 1].try_into().unwrap());
471            *o += 1;
472            Ok(d)
473        }
474        
475        fn read_i16(data: &[u8], o: &mut usize) -> Result<i16, LiveNodeFromCborError> {
476            assert_len(*o, 2, data) ?;
477            let d = i16::from_be_bytes(data[*o..*o + 2].try_into().unwrap());
478            *o += 2;
479            Ok(d)
480        }
481        
482        fn read_i32(data: &[u8], o: &mut usize) -> Result<i32, LiveNodeFromCborError> {
483            assert_len(*o, 4, data) ?;
484            let d = i32::from_be_bytes(data[*o..*o + 4].try_into().unwrap());
485            *o += 4;
486            Ok(d)
487        }
488        
489        fn read_i64(data: &[u8], o: &mut usize) -> Result<i64, LiveNodeFromCborError> {
490            assert_len(*o, 8, data) ?;
491            let d = i64::from_be_bytes(data[*o..*o + 8].try_into().unwrap());
492            *o += 8;
493            Ok(d)
494        }
495        
496        fn read_f32(data: &[u8], o: &mut usize) -> Result<f32, LiveNodeFromCborError> {
497            assert_len(*o, 4, data) ?;
498            let d = f32::from_be_bytes(data[*o..*o + 4].try_into().unwrap());
499            *o += 4;
500            Ok(d)
501        }
502        
503        fn read_f64(data: &[u8], o: &mut usize) -> Result<f64, LiveNodeFromCborError> {
504            assert_len(*o, 8, data) ?;
505            let d = f64::from_be_bytes(data[*o..*o + 8].try_into().unwrap());
506            *o += 8;
507            Ok(d)
508        }
509        
510        fn decode_str<'a>(data: &'a [u8], o: &mut usize) -> Result<Option<&'a str>,
511        LiveNodeFromCborError> {
512            assert_len(*o, 1, data) ?;
513            let len = if data[*o] >= CBOR_UTF8_START && data[*o] <= CBOR_UTF8_END {
514                let r = (data[*o] - CBOR_UTF8_START) as usize;
515                *o += 1;
516                r
517            }
518            else {
519                match data[*o] {
520                    CBOR_UTF8_8 => {
521                        *o += 1;
522                        read_u8(data, o) ? as usize
523                    }
524                    CBOR_UTF8_16 => {
525                        *o += 1;
526                        read_u16(data, o) ? as usize
527                    }
528                    CBOR_UTF8_32 => {
529                        *o += 1;
530                        read_u32(data, o) ? as usize
531                    }
532                    CBOR_UTF8_64 => {
533                        *o += 1;
534                        read_u64(data, o) ? as usize
535                    }
536                    _ => return Ok(None)
537                }
538            };
539            assert_len(*o, len, data) ?;
540            if let Ok(val) = std::str::from_utf8(&data[*o..*o + len]) {
541                *o += len;
542                Ok(Some(val))
543            } else {
544                Err(LiveNodeFromCborError::UTF8Error)
545            }
546        }
547        
548        fn decode_u64(data: &[u8], o: &mut usize) -> Result<Option<u64>, LiveNodeFromCborError> {
549            assert_len(*o, 1, data) ?;
550            let v = if data[*o] <= CBOR_UINT_END {
551                let r = Some(data[*o] as u64);
552                *o += 1;
553                r
554            }
555            else {
556                match data[*o] {
557                    CBOR_U8 => {
558                        *o += 1;
559                        Some(read_u8(data, o) ? as u64)
560                    }
561                    CBOR_U16 => {
562                        *o += 1;
563                        Some(read_u16(data, o) ? as u64)
564                    }
565                    CBOR_U32 => {
566                        *o += 1;
567                        Some(read_u32(data, o) ? as u64)
568                    }
569                    CBOR_U64 => {
570                        *o += 1;
571                        Some(read_u64(data, o)?)
572                    }
573                    _ => return Ok(None)
574                }
575            };
576            Ok(v)
577        }
578        
579        fn decode_i64(data: &[u8], o: &mut usize) -> Result<Option<i64>, LiveNodeFromCborError> {
580            assert_len(*o, 1, data) ?;
581            let v = if data[*o] >= CBOR_NUINT_START && data[*o] <= CBOR_NUINT_END {
582                let r = Some(-((data[*o] - CBOR_NUINT_START + 1) as i64));
583                *o += 1;
584                r
585            }
586            else {
587                match data[*o] {
588                    CBOR_NU8 => {
589                        *o += 1;
590                        Some(-(read_i8(data, o) ? as i64 + 1))
591                    }
592                    CBOR_NU16 => {
593                        *o += 1;
594                        Some(-(read_i16(data, o) ? as i64 + 1))
595                    }
596                    CBOR_NU32 => {
597                        *o += 1;
598                        Some(-(read_i32(data, o) ? as i64 + 1))
599                    }
600                    CBOR_NU64 => {
601                        *o += 1;
602                        Some(-(read_i64(data, o)? + 1))
603                    }
604                    _ => decode_u64(data, o)?.map(|data| data as i64)
605                }
606            };
607            Ok(v)
608        }
609        
610        fn decode_array_len(data: &[u8], o: &mut usize) -> Result<Option<usize>, LiveNodeFromCborError> {
611            assert_len(*o, 1, data) ?;
612            let v = if data[*o] >= CBOR_ARRAY_START && data[*o] <= CBOR_ARRAY_END {
613                let r = Some((data[*o] - CBOR_ARRAY_START) as usize);
614                *o += 1;
615                r
616            }
617            else {
618                match data[*o] {
619                    CBOR_ARRAY_8 => {
620                        *o += 1;
621                        Some(read_u8(data, o) ? as usize)
622                    }
623                    CBOR_ARRAY_16 => {
624                        *o += 1;
625                        Some(read_u16(data, o) ? as usize)
626                    }
627                    CBOR_ARRAY_32 => {
628                        *o += 1;
629                        Some(read_u32(data, o) ? as usize)
630                    }
631                    CBOR_ARRAY_64 => {
632                        *o += 1;
633                        Some(read_u64(data, o) ? as usize)
634                    }
635                    _ => return Ok(None)
636                }
637            };
638            Ok(v)
639        }
640        
641        fn decode_map_len(data: &[u8], o: &mut usize) -> Result<Option<usize>, LiveNodeFromCborError> {
642            assert_len(*o, 1, data) ?;
643            let v = if data[*o] >= CBOR_MAP_START && data[*o] <= CBOR_MAP_END {
644                let r = Some((data[*o] - CBOR_MAP_START) as usize);
645                *o += 1;
646                r
647            }
648            else {
649                match data[*o] {
650                    CBOR_MAP_8 => {
651                        *o += 1;
652                        Some(read_u8(data, o) ? as usize)
653                    }
654                    CBOR_MAP_16 => {
655                        *o += 1;
656                        Some(read_u16(data, o) ? as usize)
657                    }
658                    CBOR_MAP_32 => {
659                        *o += 1;
660                        Some(read_u32(data, o) ? as usize)
661                    }
662                    CBOR_MAP_64 => {
663                        *o += 1;
664                        Some(read_u32(data, o) ? as usize)
665                    }
666                    _ => return Ok(None)
667                }
668            };
669            Ok(v)
670        }
671        
672        fn decode_id(data: &[u8], o: &mut usize) -> Result<Option<LiveId>, LiveNodeFromCborError> {
673            // we expect a string OR a u64
674            if let Some(val) = decode_str(data, o) ? {
675                if let Ok(id) = LiveId::from_str_with_lut(val) {
676                    return Ok(Some(id))
677                }
678                else {
679                    return Err(LiveNodeFromCborError::LiveIdCollision)
680                }
681            }
682            else if let Some(v) = decode_u64(data, o) ? {
683                return Ok(Some(LiveId(v)))
684            }
685            Ok(None)
686        }
687        
688        struct StackItem {len: usize, count: usize, has_keys: bool}
689        
690        let mut stack = vec![StackItem {count: 0, len: 1, has_keys: false}];
691        let origin = LiveNodeOrigin::field();
692        let mut o = 0;
693        while o < data.len() {
694            
695            if stack.last().unwrap().count == stack.last().unwrap().len {
696                self.push(LiveNode {id: LiveId(0), origin, value: LiveValue::Close});
697                stack.pop();
698            }
699            
700            // ok lets read
701            let stack_item = stack.last_mut().unwrap();
702            let id = if stack_item.has_keys {
703                let id = decode_id(data, &mut o) ?;
704                if id.is_none() {return Err(LiveNodeFromCborError::ExpectedId)}
705                id.unwrap()
706            }
707            else {
708                LiveId(0)
709            };
710            
711            stack_item.count += 1;
712            
713            assert_len(o, 1, data) ?;
714            
715            if let Some(v) = decode_i64(data, &mut o) ? {
716                self.push(LiveNode {id, origin, value: LiveValue::Int64(v)});
717            }
718            else if let Some(v) = decode_str(data, &mut o) ? {
719                let value = if let Some(inline_str) = InlineString::from_str(v) {
720                    LiveValue::InlineString(inline_str)
721                }
722                else {
723                    LiveValue::String(Arc::new(v.to_string()))
724                };
725                self.push(LiveNode {id, origin, value});
726            }
727            else if let Some(len) = decode_array_len(data, &mut o) ? {
728                stack.push(StackItem {count: 0, len, has_keys: false});
729                self.push(LiveNode {id, origin, value: LiveValue::Array});
730            }
731            else if let Some(len) = decode_map_len(data, &mut o) ? {
732                // this COULD be a special type.
733                if len == 1 {
734                    let mut o1 = o;
735                    if let Some(s) = decode_str(data, &mut o1) ? {
736                        match s {
737                            "in" => { // its a vec
738                                return Err(LiveNodeFromCborError::NotImpl)
739                            }
740                            "as" => { // its a color
741                                return Err(LiveNodeFromCborError::NotImpl)
742                            }
743                            "if" => { // bare enum
744                                if let Some(variant) = decode_id(data, &mut o1) ? {
745                                    self.push(LiveNode {
746                                        id,
747                                        origin,
748                                        value: LiveValue::BareEnum(variant)
749                                    });
750                                    o = o1;
751                                    continue;
752                                }
753                                else {
754                                    return Err(LiveNodeFromCborError::ExpectedBareEnumString)
755                                }
756                            }
757                            "enum" => { // other enum
758                                return Err(LiveNodeFromCborError::NotImpl)
759                            }
760                            _ => ()
761                        }
762                    }
763                }
764                stack.push(StackItem {count: 0, len, has_keys: true});
765                self.push(LiveNode {id, origin, value: LiveValue::Object});
766            }
767            else {
768                match data[o] {
769                    CBOR_TRUE => {
770                        o += 1;
771                        self.push(LiveNode {id, origin, value: LiveValue::Bool(true)});
772                    }
773                    CBOR_FALSE => {
774                        o += 1;
775                        self.push(LiveNode {id, origin, value: LiveValue::Bool(false)});
776                    }
777                    CBOR_FLOAT32 => {
778                        o += 1;
779                        let value = LiveValue::Float32(read_f32(data, &mut o) ?);
780                        self.push(LiveNode {id, origin, value});
781                    }
782                    CBOR_FLOAT64 => {
783                        o += 1;
784                        let value = LiveValue::Float64(read_f64(data, &mut o) ?);
785                        self.push(LiveNode {id, origin, value});
786                    }
787                    CBOR_NULL => {
788                        o += 1;
789                        self.push(LiveNode {id, origin, value: LiveValue::None});
790                    },
791                    _ => {
792                        return Err(LiveNodeFromCborError::UnexpectedValue)
793                    }
794                }
795            };
796        }
797        if stack.last().unwrap().count == stack.last().unwrap().len {
798            self.push(LiveNode {id: LiveId(0), origin, value: LiveValue::Close});
799            stack.pop();
800        }
801        // lets unwind the stack
802        while let Some(item) = stack.pop() {
803            if item.count != item.len {
804                return Err(LiveNodeFromCborError::StackNotClosed)
805            }
806        }
807        self.push(LiveNode {id: LiveId(0), origin, value: LiveValue::Close});
808        Ok(())
809    }
810}