selene_lib/standard_library/
v1_upgrade.rs

1use std::collections::BTreeMap;
2
3use super::{v1, *};
4
5impl From<v1::StandardLibrary> for StandardLibrary {
6    fn from(mut v1: v1::StandardLibrary) -> Self {
7        let mut standard_library = StandardLibrary::default();
8
9        if let Some(meta) = v1.meta.take() {
10            standard_library.base = meta.base;
11            standard_library.name = meta.name;
12
13            if let Some(v1_structs) = meta.structs {
14                for (name, children) in v1_structs {
15                    let mut struct_fields = BTreeMap::new();
16
17                    for (name, v1_struct_field) in children {
18                        struct_fields.extend(unpack_v1_field(name, v1_struct_field));
19                    }
20
21                    standard_library.structs.insert(name, struct_fields);
22                }
23            }
24        }
25
26        let mut globals = BTreeMap::new();
27
28        for (name, v1_global) in v1.globals {
29            globals.extend(unpack_v1_field(name, v1_global));
30        }
31
32        standard_library.globals = globals;
33
34        standard_library
35    }
36}
37
38impl From<v1::Argument> for Argument {
39    fn from(v1_argument: v1::Argument) -> Self {
40        Argument {
41            required: v1_argument.required.into(),
42            argument_type: v1_argument.argument_type.into(),
43            observes: Observes::ReadWrite,
44            deprecated: None,
45        }
46    }
47}
48
49impl From<v1::ArgumentType> for ArgumentType {
50    fn from(v1_argument_type: v1::ArgumentType) -> Self {
51        match v1_argument_type {
52            v1::ArgumentType::Any => ArgumentType::Any,
53            v1::ArgumentType::Bool => ArgumentType::Bool,
54            v1::ArgumentType::Constant(constants) => ArgumentType::Constant(constants),
55            v1::ArgumentType::Display(message) => ArgumentType::Display(message),
56            v1::ArgumentType::Function => ArgumentType::Function,
57            v1::ArgumentType::Nil => ArgumentType::Nil,
58            v1::ArgumentType::Number => ArgumentType::Number,
59            v1::ArgumentType::String => ArgumentType::String,
60            v1::ArgumentType::Table => ArgumentType::Table,
61            v1::ArgumentType::Vararg => ArgumentType::Vararg,
62        }
63    }
64}
65
66fn unpack_v1_field(name: String, v1_field: v1::Field) -> BTreeMap<String, Field> {
67    let mut upgraded_fields = BTreeMap::new();
68    let mut v1_fields = vec![(name, v1_field)];
69
70    while let Some((name, field)) = v1_fields.pop() {
71        match field {
72            v1::Field::Any => {
73                upgraded_fields.insert(name, Field::from_field_kind(FieldKind::Any));
74            }
75
76            v1::Field::Complex { function, table } => {
77                for (child_name, child_field) in table {
78                    v1_fields.push((format!("{name}.{child_name}").to_owned(), child_field));
79                }
80
81                if let Some(function) = function {
82                    upgraded_fields.insert(
83                        name,
84                        Field::from_field_kind(FieldKind::Function(FunctionBehavior {
85                            arguments: function.arguments.into_iter().map(Into::into).collect(),
86                            method: function.method,
87                            must_use: false,
88                        })),
89                    );
90                }
91            }
92
93            v1::Field::Property { writable } => {
94                upgraded_fields.insert(
95                    name,
96                    Field::from_field_kind(FieldKind::Property(writable.into())),
97                );
98            }
99
100            v1::Field::Struct(struct_name) => {
101                upgraded_fields
102                    .insert(name, Field::from_field_kind(FieldKind::Struct(struct_name)));
103            }
104
105            v1::Field::Removed => {
106                upgraded_fields.insert(name, Field::from_field_kind(FieldKind::Removed));
107            }
108        }
109    }
110
111    upgraded_fields
112}
113
114impl From<Option<v1::Writable>> for PropertyWritability {
115    fn from(v1_writable: Option<v1::Writable>) -> Self {
116        match v1_writable {
117            Some(v1::Writable::Full) => PropertyWritability::FullWrite,
118            Some(v1::Writable::NewFields) => PropertyWritability::NewFields,
119            Some(v1::Writable::Overridden) => PropertyWritability::OverrideFields,
120            None => PropertyWritability::ReadOnly,
121        }
122    }
123}
124
125impl From<v1::Required> for Required {
126    fn from(v1_required: v1::Required) -> Self {
127        match v1_required {
128            v1::Required::NotRequired => Required::NotRequired,
129            v1::Required::Required(message) => Required::Required(message),
130        }
131    }
132}