swamp_script_core_extra/qck_des.rs
1/*
2 * Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/swamp/script
3 * Licensed under the MIT License. See LICENSE in the project root for license information.
4 */
5
6use crate::extra::SparseValueId;
7use crate::prelude::Value;
8use crate::value::{QuickDeserialize, RustType};
9use fixed32::Fp;
10use std::cell::RefCell;
11use std::rc::Rc;
12use swamp_script_types::{EnumVariantType, ExternalType, Type};
13
14/// # Panics
15///
16#[inline]
17#[allow(clippy::too_many_lines)]
18#[must_use]
19pub fn quick_deserialize(resolved_type: &Type, buf: &[u8], depth: usize) -> (Value, usize) {
20 let (val, octet_size) = match resolved_type {
21 Type::Int => {
22 let i = i32::from_le_bytes(buf[0..4].try_into().expect("REASON"));
23 (Value::Int(i), 4)
24 }
25
26 Type::Float => {
27 let i = i32::from_le_bytes(buf[0..4].try_into().expect("couldn't convert to Fp"));
28 (Value::Float(Fp::from_raw(i)), 4)
29 }
30 Type::String => {
31 let octet_len =
32 u16::from_le_bytes(buf[0..2].try_into().expect("could not convert strlen"));
33 let str =
34 String::from_utf8(buf[2..2 + octet_len as usize].to_owned()).expect("utf8 error");
35 (Value::String(str), (octet_len + 2) as usize)
36 }
37 Type::Bool => (Value::Bool(buf[0] != 0), 1),
38 Type::Unit => (Value::Unit, 0),
39 Type::Never => panic!("can not deserialize never type"),
40 Type::Slice(value_type) => {
41 todo!()
42 }
43 Type::SlicePair(key_type, value_type) => {
44 todo!()
45 }
46 Type::MutableReference(_) => todo!(),
47 /*
48 Type::Vec(element_type) => {
49 let mut offset = 0;
50 let count = u16::from_le_bytes(
51 buf[offset..offset + 2]
52 .try_into()
53 .expect("should work with u16"),
54 );
55 offset += 2;
56
57 let item_ref = &element_type;
58
59 let mut values = Vec::new();
60 for _index in 0..count {
61 let (value, item_octet_size) =
62 quick_deserialize(item_ref, &buf[offset..], depth + 1);
63
64 offset += item_octet_size;
65
66 values.push(Rc::new(RefCell::new(value)));
67 }
68
69 (Value::Vec(*element_type.clone(), values), offset)
70 }
71
72 */
73 Type::Tuple(tuple_types) => {
74 let mut offset = 0;
75 let mut values = Vec::new();
76 for tuple_item_type in tuple_types {
77 let (value, item_octet_size) =
78 quick_deserialize(tuple_item_type, &buf[offset..], depth + 1);
79 values.push(Rc::new(RefCell::new(value)));
80 offset += item_octet_size;
81 }
82 (Value::Tuple(tuple_types.clone(), values), offset)
83 }
84 Type::NamedStruct(struct_type_ref) => {
85 let mut values = Vec::new();
86 let mut offset = 0;
87 for struct_field_type in struct_type_ref
88 .anon_struct_type
89 .field_name_sorted_fields
90 .values()
91 {
92 let (value, octet_size) =
93 quick_deserialize(&struct_field_type.field_type, &buf[offset..], depth + 1);
94 values.push(Rc::new(RefCell::new(value)));
95 offset += octet_size;
96 }
97 (Value::NamedStruct(struct_type_ref.clone(), values), offset)
98 }
99 Type::AnonymousStruct(_anon_struct_type) => {
100 todo!()
101 }
102 /*
103 Type::Map(key_type, value_type) => {
104 let mut offset = 0;
105 let count = u16::from_le_bytes(
106 buf[offset..offset + 2]
107 .try_into()
108 .expect("should work with u16"),
109 );
110 offset += 2;
111
112 let mut seq_map = SeqMap::new(); //SeqMap<Value, ValueRef>
113 for _map_index in 0..count {
114 let (key_val, key_octet_size) =
115 quick_deserialize(key_type, &buf[offset..], depth + 1);
116 offset += key_octet_size;
117
118 let (value_val, value_octet_size) =
119 quick_deserialize(value_type, &buf[offset..], depth + 1);
120 offset += value_octet_size;
121
122 let value_ref = Rc::new(RefCell::new(value_val));
123
124 seq_map
125 .insert(key_val, value_ref)
126 .expect("should work to insert");
127 }
128 (
129 Value::Map(*key_type.clone(), *value_type.clone(), seq_map),
130 offset,
131 )
132 }
133
134 */
135 Type::Enum(enum_type) => {
136 let mut offset = 0;
137 let enum_lookup_index = buf[offset];
138 offset += 1;
139 assert!(enum_lookup_index < 8);
140
141 let borrowed_enum = enum_type.clone();
142
143 let variant_type = borrowed_enum
144 .get_variant_from_index(enum_lookup_index as usize)
145 .expect("should be able to find variant");
146
147 let val = match &*variant_type {
148 EnumVariantType::Struct(_) => {
149 todo!("struct containers not done yet")
150 }
151 EnumVariantType::Tuple(tuple_type_ref) => {
152 let mut vals_in_order = Vec::new();
153 for tuple_type in &tuple_type_ref.fields_in_order {
154 let (tuple_value, tuple_octet_size) =
155 quick_deserialize(tuple_type, &buf[offset..], depth + 1);
156 vals_in_order.push(Rc::new(RefCell::new(tuple_value)));
157 offset += tuple_octet_size;
158 }
159 Value::EnumVariantTuple(
160 enum_type.clone(),
161 tuple_type_ref.clone(),
162 vals_in_order,
163 )
164 }
165 EnumVariantType::Nothing(x) => {
166 offset += 0;
167 Value::EnumVariantSimple(enum_type.clone(), x.clone())
168 }
169 };
170
171 (val, offset)
172 }
173 /*
174 Type::Sparse(value_type) => {
175 let sparse_type_id_rust_type = Rc::new(ExternalType {
176 type_name: "SparseId".to_string(),
177 number: SPARSE_ID_TYPE_ID, // TODO: FIX hardcoded number
178 });
179
180 let (internal_map, sparse_value_map_octet_size) = SparseValueMap::quick_deserialize(
181 sparse_type_id_rust_type,
182 *value_type.clone(),
183 buf,
184 );
185
186 let sparse_collection_rust_type = Rc::new(ExternalType {
187 type_name: "Sparse".to_string(),
188 number: SPARSE_TYPE_ID, // TODO: FIX hardcoded number
189 });
190
191 (
192 Value::Sparse(
193 swamp_script_types::Type::External(sparse_collection_rust_type),
194 internal_map,
195 ),
196 sparse_value_map_octet_size,
197 )
198 }
199 Type::Grid(type_parameter) => {
200 todo!()
201 }
202
203 */
204 Type::Function(_) => {
205 panic!("can not serialize function")
206 }
207 Type::Optional(optional_type_ref) => {
208 let mut offset = 0;
209 let has_some = buf[0] != 0;
210 offset += 1;
211 if has_some {
212 let (v, octet_size) = quick_deserialize(optional_type_ref, &buf[1..], depth + 1);
213 offset += octet_size;
214 (Value::Option(Some(Rc::new(RefCell::new(v)))), offset)
215 } else {
216 (Value::Option(None), offset)
217 }
218 }
219 Type::External(rust_type_ref) => {
220 match rust_type_ref.number {
221 SPARSE_ID_TYPE_ID => {
222 let sparse_id_rust_type = ExternalType {
223 type_name: "SparseId".to_string(),
224 number: SPARSE_ID_TYPE_ID, // TODO: FIX hardcoded number
225 };
226
227 let (sparse_value_id, octet_size) = SparseValueId::quick_deserialize(buf);
228 (
229 {
230 let boxed_sparse_value_id: Rc<RefCell<Box<dyn RustType>>> =
231 Rc::new(RefCell::new(Box::new(sparse_value_id)));
232 Value::RustValue(sparse_id_rust_type, boxed_sparse_value_id)
233 },
234 octet_size,
235 )
236 }
237 _ => panic!("can not deserialize rust types {}", rust_type_ref.type_name),
238 }
239 }
240 &swamp_script_types::Type::Variable(_) => todo!(),
241 &swamp_script_types::Type::Generic(_, _) => todo!(),
242 &swamp_script_types::Type::Blueprint(_) => todo!(),
243 };
244
245 (val, octet_size)
246}