1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
pub use crate::prelude::*;

pub type DefaultValue = Arc<str>;
pub type KeyName = Arc<str>;

#[derive(Debug)]
pub struct Variable {
    key: KeyName,
    required: bool,
    default: Option<DefaultValue>,
}

#[derive(Debug, Default)]
pub struct VariableMap {
    data: BTreeMap<KeyName, Variable>,
}

impl Variable {
    pub fn new<S>(key: S) -> Self
    where
        S: Into<KeyName>,
    {
        Self {
            key: key.into(),
            required: false,
            default: None,
        }
    }

    pub fn required(self) -> Self {
        Self {
            required: true,
            ..self
        }
    }

    pub fn default<S>(self, value: S) -> Self
    where
        S: Into<DefaultValue>,
    {
        Self {
            default: Some(value.into()),
            ..self
        }
    }

    pub fn is_required(&self) -> bool {
        self.required
    }

    pub fn key(&self) -> &str {
        self.key.as_ref()
    }

    pub fn default_value(&self) -> Option<MetaValue> {
        self.default
            .as_ref()
            .map(ToString::to_string)
            .map(MetaValue::String)
    }
}

impl VariableMap {
    pub fn insert(&mut self, var: Variable) {
        self.data.insert(var.key.clone(), var);
    }

    pub fn get<K>(&self, key: &K) -> Option<&Variable>
    where
        K: AsRef<str>,
    {
        self.data.get(key.as_ref())
    }

    pub fn iter(&self) -> impl Iterator<Item = &Variable> {
        self.data.values()
    }

    pub fn is_empty(&self) -> bool {
        self.data.is_empty()
    }
}