swamp_script_core_extra/
cnv.rs1use crate::value::Value;
7use crate::value::ValueRef;
8use swamp_script_types::NamedStructType;
9
10pub fn overwrite_value(target: &ValueRef, source: Value) {
11 if let Value::NamedStruct(ref mut target_struct_type_ref, ref mut target_fields) =
12 *target.borrow_mut()
13 {
14 if let Value::NamedStruct(source_struct_type_ref, source_fields) = source {
15 overwrite_struct(
16 target_struct_type_ref.clone(),
17 target_fields,
18 source_struct_type_ref,
19 source_fields,
20 );
21 }
22 }
23}
24
25pub fn overwrite_struct(
28 target_struct_type_ref: NamedStructType,
29 target_values: &mut Vec<ValueRef>,
30 source_struct: NamedStructType,
31 source_values: Vec<ValueRef>,
32) {
33 let borrowed_source_struct_type = source_struct.clone();
34 let source_anon_type = &borrowed_source_struct_type.anon_struct_type;
35
36 for ((field_name, target_field_type), target_field_value) in target_struct_type_ref
37 .anon_struct_type
38 .field_name_sorted_fields
39 .iter()
40 .zip(target_values)
41 {
42 if let Some(source_field_type) = source_anon_type.field_name_sorted_fields.get(field_name) {
43 if source_field_type
44 .field_type
45 .compatible_with(&target_field_type.field_type)
46 {
47 let index = source_anon_type
48 .field_name_sorted_fields
49 .get_index(field_name)
50 .expect("should work, we checked get()");
51 *target_field_value.borrow_mut() = source_values[index].borrow().clone();
52 } else {
53 panic!("overwrite_struct, wrong type");
54 }
55 }
56 }
57}