xcell_types/typing/
parser.rs

1use super::*;
2
3impl XCellTyped {
4    pub fn parse(input: &str, info: &TypeMetaInfo) -> Self {
5        let normed = Self::norm_typing(input);
6        match normed.as_str() {
7            "bool" | "boolean" => info.boolean.clone().into(),
8            // int
9            "byte" | "i8" => IntegerDescription::range(i8::MIN, i8::MAX, IntegerKind::Integer8).into(),
10            "short" | "i16" => IntegerDescription::range(i16::MIN, i16::MAX, IntegerKind::Integer16).into(),
11            "int" | "i32" => IntegerDescription::range(i32::MIN, i32::MAX, IntegerKind::Integer32).into(),
12            "long" | "i64" => IntegerDescription::range(i64::MIN, i64::MAX, IntegerKind::Integer64).into(),
13            // unsigned
14            "sbyte" | "u8" => IntegerDescription::range(u8::MIN, u8::MAX, IntegerKind::Unsigned8).into(),
15            "ushort" | "u16" => IntegerDescription::range(u16::MIN, u16::MAX, IntegerKind::Unsigned16).into(),
16            "uint" | "u32" => IntegerDescription::range(u32::MIN, u32::MAX, IntegerKind::Unsigned32).into(),
17            "ulong" | "u64" => IntegerDescription::range(u64::MIN, u64::MAX, IntegerKind::Unsigned64).into(),
18            // float
19            "float" | "f32" => Self::Decimal(Default::default()),
20            "double" | "f64" => Self::Decimal(Default::default()),
21            "decimal" | "d128" | "f128" => Self::Decimal(Default::default()),
22            // other
23            "color" | "colour" => Self::Color(Default::default()),
24            // "c4" | "color32" | "color4" => Self::Custom(ArrayDescription::new(s)),
25            // "date" | "time" | "datetime" => Self::Time(Default::default()),
26            // array
27            "v2" | "vec2" => ArrayDescription::new(ArrayKind::Vector2).into(),
28            "v3" | "vec3" => ArrayDescription::new(ArrayKind::Vector3).into(),
29            "v4" | "vec4" => ArrayDescription::new(ArrayKind::Vector4).into(),
30            "q4" | "quaternion" => ArrayDescription::new(ArrayKind::Quaternion4).into(),
31            // slow path
32            _ => XCellTyped::parse_complex(input, &normed, info),
33        }
34    }
35    fn parse_complex(raw: &str, normed: &str, info: &TypeMetaInfo) -> Self {
36        if info.matches_string(normed) {
37            return info.string.clone().into();
38        }
39        if let Some(s) = info.matches_vector(raw) {
40            let typing = XCellTyped::parse(s, info);
41            return info.vector.clone().with_type(typing).into();
42        }
43        EnumerateDescription::new(raw).into()
44    }
45    fn norm_typing(input: &str) -> String {
46        let mut out = String::with_capacity(input.len());
47        for c in input.chars() {
48            if c.is_ascii_uppercase() {
49                out.push(c.to_ascii_lowercase())
50            }
51            else if c.is_ascii_whitespace() {
52            }
53            else {
54                out.push(c)
55            }
56        }
57        out
58    }
59}
60
61impl TypeMetaInfo {
62    fn matches_string(&self, s: &str) -> bool {
63        self.string.matches_type(s)
64    }
65    fn matches_vector<'i>(&self, raw: &'i str) -> Option<&'i str> {
66        return self.vector.matches_rest(raw);
67    }
68}