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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
use std::vec::Vec;
use std::ops::{Deref, DerefMut};
use serde::ser::{Serialize, Serializer};
use serde::de::{Deserialize, DeserializeOwned, Deserializer};
use serde_json;

use crate::node::Node;
use crate::node_runner::NodeRunner;
use crate::node_token::NodeToken;

#[derive(Clone, Default)]
pub struct ContextVec<T> {
    context: Vec<usize>,
    vector:  Vec<T>,
}

/// The purpose of a ContextVec is to provide a way for commands to easily access relevant values.
/// If we have a ContextVec named foo, the command `foo[?] get` will display the values in foo that the context points to.
///
/// # Contents
///
/// *   `vector: Vec<T>`
/// *   `context: Vec<usize>`
///
/// # Invariants
///
/// *   the values in context will point to a valid value in vector
/// *   the values in context will continue to point to the same value in vector (even after operations like insert and remove)

impl<T> ContextVec<T> {
    /// Create a new empty ContextVec
    pub fn new() -> ContextVec<T> {
        ContextVec {
            context: vec!(),
            vector:  vec!(),
        }
    }

    /// Create a new ContextVec from a Vec
    pub fn from_vec(vector: Vec<T>) -> ContextVec<T> {
        ContextVec {
            context: vec!(),
            vector: vector,
        }
    }

    /// Get the value currently pointed to by context
    pub fn selection_first(&self) -> Option<&T> {
        match self.context.first() {
            Some (value) => {
                self.vector.get(*value)
            }
            None => None
        }
    }

    /// Mutably get the value currently pointed to by context
    pub fn selection_first_mut(&mut self) -> Option<&mut T> {
        match self.context.first() {
            Some (value) => {
                self.vector.get_mut(*value)
            }
            None => None
        }
    }

    /// Get the values currently pointed to by context
    pub fn selection(&self) -> Vec<&T> {
        let mut result: Vec<&T> = vec!();
        for i in &self.context {
            result.push(&self.vector[*i]);
        }
        result
    }

    /// Get a slice of the context
    pub fn get_context(&self) -> &[usize] {
        &self.context
    }

    /// Consume the ContextVec into the context
    pub fn into_context(self) -> Vec<usize> {
        self.context
    }

    /// Consume the ContextVec into the vector
    pub fn into_vector(self) -> Vec<T> {
        self.vector
    }

    /// Consume the ContextVec into a tuple of the context and the vector
    pub fn into_tuple(self) -> (Vec<usize>, Vec<T>) {
        (self.context, self.vector)
    }

    /// Clears the context
    pub fn clear_context(&mut self) {
        self.context.clear();
    }

    /// Sets a new context
    pub fn set_context(&mut self, value: usize) {
        let length = self.vector.len();
        if value >= length {
            panic!(format!("Attempted to set context {} on a ContextVec of length {}", value, length));
        }
        self.context.clear();
        self.context.push(value);
    }

    /// Sets a new context
    pub fn set_context_vec(&mut self, mut values: Vec<usize>) {
        self.context.clear();
        let length = self.vector.len();
        for value in values.drain(..) {
            if value >= length {
                panic!(format!("Attempted to set context {} on a ContextVec of length {}", value, length));
            }
            self.context.push(value);
        }
    }

    /// Set to a new vector.
    /// Clears the context.
    pub fn set_vec(&mut self, vector: Vec<T>) {
        self.context.clear();
        self.vector = vector;
    }

    /// Clears the vector and the context
    pub fn clear(&mut self) {
        self.context.clear();
        self.vector.clear();
    }

    /// Push a value to the end of the vector
    pub fn push(&mut self, value: T) {
        self.vector.push(value);
    }

    /// Insert a value into the vector.
    /// Invalid context indices are updated.
    pub fn insert(&mut self, index: usize, value: T) {
        self.vector.insert(index, value);

        for i in self.context.iter_mut() {
            if *i >= index {
                *i += 1;
            }
        }
    }

    /// Pop a value from the end of the vector.
    /// If it succeeds invalid context indices are removed.
    pub fn pop(&mut self) -> Option<T> {
        match self.vector.pop() {
            Some(value) => {
                let len = self.vector.len();
                self.context.retain(|&x| x < len);
                Some(value)
            }
            None => {
                None
            }
        }
    }

    /// Remove a value at the specified index.
    /// Deletes any context indices pointing to the removed value.
    /// Shifts all larger context indices down.
    pub fn remove(&mut self, to_remove: usize) -> T {
        let element = self.vector.remove(to_remove);
        let mut new_context: Vec<usize> = vec!();
        for i in self.context.drain(..) {
            if i < to_remove {
                new_context.push(i);
            }
            else if i > to_remove {
                new_context.push(i-1);
            }
        }
        self.context = new_context;
        element
    }
}

impl<T> Deref for ContextVec<T> {
    type Target = [T];
    fn deref(&self) -> &[T] {
        &self.vector
    }
}

impl<T> DerefMut for ContextVec<T> {
    fn deref_mut(&mut self) -> &mut [T] {
        &mut self.vector
    }
}

impl<T> Node for ContextVec<T> where T: Node + Serialize + DeserializeOwned + Default {
    fn node_step(&mut self, mut runner: NodeRunner) -> String {
        match runner.step() {
            NodeToken::ChainIndex (index) => {
                let length = self.vector.len();
                match self.vector.get_mut(index) {
                    Some (item) => item.node_step(runner),
                    None => {
                        return match length {
                             0 => format!("Used index {} on an empty vector", index),
                             1 => format!("Used index {} on a vector of size 1 (try 0)", index),
                             _ => format!("Used index {} on a vector of size {} (try a value between 0-{})", index, length, length-1)
                        }
                    }
                }
            }
            NodeToken::ChainContext => {
                let mut combined = String::from("|");
                for i in self.context.iter() {
                    match self.vector.get_mut(*i) {
                        Some(ref mut node) => {
                            let result = node.node_step(runner.clone());
                            combined.push_str(result.as_str());
                        }
                        None => {
                            combined.push_str("Context out of range. This should never happen.");
                        }
                    }
                    combined.push('|');
                }
                combined
            }
            NodeToken::ChainAll => {
                let mut combined = String::from("|");
                for item in self.vector.iter_mut() {
                    combined.push_str(item.node_step(runner.clone()).as_ref());
                    combined.push('|');
                }
                combined
            }
            NodeToken::ChainProperty (ref s) if s == "length" => { self.vector.len().node_step(runner) }
            NodeToken::Get => {
                serde_json::to_string_pretty(&mut self.vector).unwrap()
            }
            NodeToken::Set(value) => {
                match serde_json::from_str(&value) {
                    Ok(result) => {
                        self.vector = result;
                        String::from("")
                    }
                    Err(err) => {
                        format!("vector set error: {}", err)
                    }
                }
            }
            NodeToken::Insert => {
                self.push(T::default());
                String::new()
            }
            NodeToken::Remove => {
                if let Some(_) = self.pop() {
                    String::new()
                } else {
                    String::from("Tried to remove from an empty vector.")
                }
            }
            NodeToken::InsertIndex (index) => {
                let max_index = self.len();
                if index > max_index {
                    format!("Tried to insert at index {} on a vector of size {} (try a value between 0-{})", index, max_index, max_index)
                }
                else {
                    self.insert(index, T::default());
                    String::new()
                }
            }
            NodeToken::RemoveIndex (index) => {
                let max_index = self.len() - 1;
                if index > max_index {
                    format!("Tried to remove the value at index {} on a vector of size {} (try a value between 0-{})", index, self.len(), max_index)
                }
                else {
                    self.remove(index);
                    String::new()
                }
            }
            NodeToken::SetDefault => {
                self.vector = vec!();
                String::new()
            }
            NodeToken::Help => {
                String::from(r#"
Context Vector Help

Commands:
*   help          - display this help
*   get           - display JSON
*   set           - set to JSON
*   insert        - create a new element at the end of the vector
*   insert $INDEX - create a new element at $INDEX
*   remove        - remove the element at the end of the vector
*   remove $INDEX - remove the element at $INDEX
*   reset         - reset to empty vector

Accessors:
*   [INDEX] - access item at INDEX
*   [?]     - access items at current context
*   .length - display number of items"#)
            }
            action => { format!("vector cannot '{:?}'", action) }
        }
    }
}

impl<T> Serialize for ContextVec<T> where T: Serialize {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
        self.vector.serialize(serializer)
    }
}

impl<'de, T> Deserialize<'de> for ContextVec<T> where T: Deserialize<'de> {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de> {
        Ok(ContextVec {
            context: vec!(),
            vector: Vec::<T>::deserialize(deserializer)?,
        })
    }
}