Skip to main content

mln_expression/
type_.rs

1// Ports include/mln/style/expression/type.hpp.
2//
3// The module is `type_` because `type` is a Rust keyword. The C++ constants
4// (`type::Number`, ...) are the enum variants (`Type::Number`, ...).
5
6use std::fmt;
7
8/// `type::Type`, same variant order as the C++ variant.
9#[derive(Clone, Debug, PartialEq, Eq)]
10pub enum Type {
11    Null,
12    Number,
13    Boolean,
14    String,
15    Color,
16    Padding,
17    VariableAnchorOffsetCollection,
18    Object,
19    Value,
20    Array(Box<Array>),
21    Collator,
22    Formatted,
23    Error,
24    Image,
25}
26
27/// `type::Array`.
28#[derive(Clone, Debug, PartialEq, Eq)]
29pub struct Array {
30    pub item_type: Type,
31    pub n: Option<usize>,
32}
33
34impl Array {
35    pub fn new(item_type: Type) -> Self {
36        Array { item_type, n: None }
37    }
38
39    pub fn with_n(item_type: Type, n: Option<usize>) -> Self {
40        Array { item_type, n }
41    }
42
43    pub fn get_name(&self) -> String {
44        if let Some(n) = self.n {
45            format!("array<{}, {}>", to_string(&self.item_type), n)
46        } else if self.item_type == Type::Value {
47            "array".to_owned()
48        } else {
49            format!("array<{}>", to_string(&self.item_type))
50        }
51    }
52}
53
54impl From<Array> for Type {
55    fn from(a: Array) -> Self {
56        Type::Array(Box::new(a))
57    }
58}
59
60impl Type {
61    /// `type::Array(itemType)`.
62    pub fn array(item_type: Type) -> Type {
63        Array::new(item_type).into()
64    }
65
66    /// `type::Array(itemType, N)`.
67    pub fn array_n(item_type: Type, n: usize) -> Type {
68        Array::with_n(item_type, Some(n)).into()
69    }
70
71    pub fn is_array(&self) -> bool {
72        matches!(self, Type::Array(_))
73    }
74
75    pub fn as_array(&self) -> Option<&Array> {
76        match self {
77            Type::Array(a) => Some(a),
78            _ => None,
79        }
80    }
81
82    pub fn get_name(&self) -> String {
83        match self {
84            Type::Null => "null".to_owned(),
85            Type::Number => "number".to_owned(),
86            Type::Boolean => "boolean".to_owned(),
87            Type::String => "string".to_owned(),
88            Type::Color => "color".to_owned(),
89            Type::Padding => "padding".to_owned(),
90            Type::VariableAnchorOffsetCollection => "variableAnchorOffsetCollection".to_owned(),
91            Type::Object => "object".to_owned(),
92            Type::Value => "value".to_owned(),
93            Type::Array(a) => a.get_name(),
94            Type::Collator => "collator".to_owned(),
95            Type::Formatted => "formatted".to_owned(),
96            Type::Error => "error".to_owned(),
97            Type::Image => "resolvedImage".to_owned(),
98        }
99    }
100}
101
102/// `type::toString`.
103pub fn to_string(t: &Type) -> String {
104    t.get_name()
105}
106
107impl fmt::Display for Type {
108    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
109        f.write_str(&self.get_name())
110    }
111}
112
113#[cfg(test)]
114mod tests {
115    use super::*;
116
117    #[test]
118    fn names() {
119        assert_eq!(to_string(&Type::array(Type::Value)), "array");
120        assert_eq!(to_string(&Type::array(Type::Number)), "array<number>");
121        assert_eq!(to_string(&Type::array_n(Type::Value, 2)), "array<value, 2>");
122        assert_eq!(to_string(&Type::Image), "resolvedImage");
123    }
124}