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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
use serde::{Deserialize, Serialize};
use std::{
    collections::BTreeMap,
    fmt::{self, Display, Formatter},
};

use crate::{value::Value, Error, Result, Table, TOOL_LIST};

/// A collection dust variables comprised of key-value pairs.
///
/// The inner value is a BTreeMap in order to allow VariableMap instances to be sorted and compared
/// to one another.
#[derive(Clone, Debug, PartialEq, PartialOrd, Ord, Eq, Serialize, Deserialize)]
pub struct VariableMap {
    variables: BTreeMap<String, Value>,
}

impl VariableMap {
    /// Creates a new instace.
    pub fn new() -> Self {
        VariableMap {
            variables: BTreeMap::new(),
        }
    }

    /// Invokes built-in tools or user-defined functions based on the identifier and passes the     
    /// argument. Returns an error a tool is called with the wrong inputs or if the identifier does
    /// not match any tools or functions.
    pub fn call_function(&self, identifier: &str, argument: &Value) -> Result<Value> {
        for macro_item in TOOL_LIST {
            let valid_input_types = macro_item.info().inputs;

            if identifier == macro_item.info().identifier {
                let input_type = argument.value_type();

                if valid_input_types.contains(&input_type) {
                    return macro_item.run(argument);
                } else {
                    return Err(Error::MacroArgumentType {
                        macro_info: macro_item.info(),
                        actual: argument.clone(),
                    });
                }
            }
        }

        for (key, value) in &self.variables {
            if identifier == key {
                if let Ok(function) = value.as_function() {
                    let mut context = self.clone();

                    context.set_value("input", argument.clone())?;

                    return function.run_with_context(&mut context);
                }
            }
        }

        Err(Error::FunctionIdentifierNotFound(identifier.to_string()))
    }

    /// Returns a Value assigned to the identifer, allowing dot notation to retrieve Values that are     /// nested in Lists or Maps. Returns None if there is no variable with a key matching the            /// identifier. Returns an error if a Map or List is indexed incorrectly.
    pub fn get_value(&self, identifier: &str) -> Result<Option<Value>> {
        let split = identifier.rsplit_once('.');
        let (found_value, next_identifier) = if let Some((identifier, next_identifier)) = split {
            if identifier.contains('.') {
                (self.get_value(identifier)?, next_identifier)
            } else {
                (self.variables.get(identifier).cloned(), next_identifier)
            }
        } else {
            return Ok(self.variables.get(identifier).cloned());
        };

        if let Some(value) = found_value {
            if let Value::List(list) = value {
                let index = if let Ok(index) = next_identifier.parse::<usize>() {
                    index
                } else {
                    return Err(Error::expected_int(Value::String(
                        next_identifier.to_string(),
                    )));
                };

                Ok(list.get(index).cloned())
            } else if let Value::Map(map) = value {
                map.get_value(next_identifier)
            } else {
                Ok(Some(value))
            }
        } else {
            Ok(None)
        }
    }

    /// Assigns a variable with a Value and the identifier as its key, allowing dot notation to
    /// assign nested lists and maps. Returns an error if a List or Map is indexed incorrectly.
    pub fn set_value(&mut self, identifier: &str, value: Value) -> Result<()> {
        let split = identifier.split_once('.');

        if let Some((identifier, next_identifier)) = split {
            let get_value = self.variables.get_mut(identifier);

            if let Some(found_value) = get_value {
                if let Value::List(list) = found_value {
                    let index = if let Ok(index) = next_identifier.parse::<usize>() {
                        index
                    } else {
                        return Err(Error::expected_int(Value::String(
                            next_identifier.to_string(),
                        )));
                    };

                    let mut missing_elements = index.saturating_sub(list.len()) + 1;

                    while missing_elements > 0 {
                        list.push(value.clone());

                        missing_elements -= 1;
                    }

                    Ok(())
                } else if let Value::Map(map) = found_value {
                    map.set_value(next_identifier, value)
                } else {
                    Err(Error::ExpectedMap {
                        actual: found_value.clone(),
                    })
                }
            } else {
                let mut new_map = VariableMap::new();

                new_map.set_value(next_identifier, value)?;

                self.variables
                    .insert(identifier.to_string(), Value::Map(new_map));

                Ok(())
            }
        } else {
            self.variables.insert(identifier.to_string(), value);

            Ok(())
        }
    }

    /// Returns a reference to the inner BTreeMap.
    pub fn inner(&self) -> &BTreeMap<String, Value> {
        &self.variables
    }

    /// Returns the number of stored variables.
    pub fn len(&self) -> usize {
        self.variables.len()
    }

    /// Returns true if the length is zero.
    pub fn is_empty(&self) -> bool {
        self.variables.is_empty()
    }
}

impl Default for VariableMap {
    fn default() -> Self {
        Self::new()
    }
}

impl Display for VariableMap {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        Table::from(self).fmt(f)
    }
}

impl From<&Table> for VariableMap {
    fn from(value: &Table) -> Self {
        let mut map = VariableMap::new();

        for (row_index, row) in value.rows().iter().enumerate() {
            map.set_value(&row_index.to_string(), Value::List(row.clone()))
                .unwrap();
        }

        map
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn get_and_set_simple_value() {
        let mut map = VariableMap::new();

        map.set_value("x", Value::Integer(1)).unwrap();

        assert_eq!(Value::Integer(1), map.get_value("x").unwrap().unwrap());
    }

    #[test]
    fn get_and_set_nested_maps() {
        let mut map = VariableMap::new();

        map.set_value("x", Value::Map(VariableMap::new())).unwrap();
        map.set_value("x.x", Value::Map(VariableMap::new()))
            .unwrap();
        map.set_value("x.x.x", Value::Map(VariableMap::new()))
            .unwrap();
        map.set_value("x.x.x.x", Value::Map(VariableMap::new()))
            .unwrap();

        assert_eq!(
            Value::Map(VariableMap::new()),
            map.get_value("x.x.x.x").unwrap().unwrap()
        );
    }
}