stof 0.3.5

Stof is a unified data interface and interchange format for creating, sharing, and manipulating data. Stof removes the fragile and cumbersome parts of combining and using data in applications.
Documentation
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
//
// Copyright 2024 Formata, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

use std::{cmp::Ordering, ops::DerefMut};
use crate::{lang::SError, Library, SData, SDoc, SFunc, SNum, SVal};


/// Array library.
#[derive(Default, Debug)]
pub struct ArrayLibrary;
impl ArrayLibrary {
    /// Call array operation with an array values.
    pub fn operate(&self, pid: &str, doc: &mut SDoc, name: &str, array: &mut Vec<SVal>, parameters: &mut Vec<SVal>) -> Result<SVal, SError> {
        match name {
            // Append another array into this array, leaving the other empty.
            "append" => {
                if parameters.len() < 1 {
                    return Err(SError::array(pid, &doc, "append", "requires an array/vector argument to append"));
                }
                match &mut parameters[0] {
                    SVal::Array(other) => {
                        array.append(other);
                        Ok(SVal::Void)
                    },
                    SVal::Boxed(val) => {
                        let mut val = val.lock().unwrap();
                        let val = val.deref_mut();
                        match val {
                            SVal::Array(other) => {
                                array.append(other);
                                Ok(SVal::Void)
                            },
                            _ => {
                                Err(SError::array(pid, &doc, "append", "requires an array/vector argument to append"))
                            }
                        }
                    },
                    _ => {
                        Err(SError::array(pid, &doc, "append", "requires an array/vector argument to append"))
                    }
                }
            },
            // Push all parameters onto the array.
            "push" => {
                for v in parameters.drain(..) { array.push(v); }
                Ok(SVal::Void)
            },
            // Pop values from the array.
            "pop" => {
                if parameters.len() > 0 {
                    let index = parameters.pop().unwrap();
                    match index {
                        SVal::Number(num) => {
                            // Pop an element at a specific index
                            let index = num.int() as usize;
                            if index >= array.len() {
                                return Err(SError::array(pid, &doc, "pop", "index out of bounds"));
                            }
                            return Ok(array.remove(index));
                        },
                        _ => {
                            // Try to find the index of this 'index' value
                            let mut i = -1;
                            for idx in 0..array.len() {
                                if array[idx] == index {
                                    i = idx as i32;
                                    break; // found
                                }
                            }
                            if i < 0 {
                                return Ok(SVal::Null); // nothing found to remove
                            }
                            return Ok(array.remove(i as usize));
                        }
                    }
                }
                if let Some(val) = array.pop() {
                    return Ok(val);
                }
                return Ok(SVal::Null);
            },
            // Reverse the array in-place.
            "reverse" => {
                array.reverse();
                Ok(SVal::Void)
            },
            // Clone of this array in the reverse order.
            "reversed" => {
                let mut vals = Vec::new();
                for i in (0..array.len()).rev() {
                    vals.push(array[i].clone());
                }
                Ok(SVal::Array(vals))
            },
            // Length of this array.
            "len" => {
                Ok(SVal::Number(SNum::I64(array.len() as i64)))
            },
            // Is this array empty?
            "empty" => {
                Ok(SVal::Bool(array.len() < 1))
            },
            // Does this array have any values?
            "any" => {
                Ok(SVal::Bool(array.len() > 0))
            },
            // Get the value in the array at a specific index.
            "at" => {
                if parameters.len() < 1 {
                    return Err(SError::array(pid, &doc, "at", "requires an index argument"));
                }
                let mut results = Vec::new();
                for index in parameters.drain(..) {
                    match index {
                        SVal::Number(index) => {
                            let index = index.int() as usize;
                            if let Some(val) = array.get(index) {
                                results.push(val.clone());
                            } else {
                                return Err(SError::array(pid, &doc, "at", "index out of bounds"));
                            }
                        },
                        _ => {
                            return Err(SError::array(pid, &doc, "at", "non-numerical index is not supported"));
                        }
                    }
                }
                if results.len() == 1 {
                    Ok(results.pop().unwrap())
                } else {
                    Ok(SVal::Array(results))
                }
            },
            // Get the first value in the array.
            "first" => {
                if let Some(val) = array.first() {
                    return Ok(val.clone());
                }
                return Ok(SVal::Null);
            },
            // Get the last value in the array.
            "last" => {
                if let Some(val) = array.last() {
                    return Ok(val.clone());
                }
                return Ok(SVal::Null);
            },
            // Join the elements of this array together with a separator.
            "join" => {
                if parameters.len() < 1 {
                    return Err(SError::array(pid, &doc, "join", "requires a value to use in joining items (all converted to strings)"));
                }
                let separator = parameters[0].to_string();
                let mut str_vals = Vec::new();
                for v in array {
                    str_vals.push(v.to_string());
                }
                Ok(SVal::String(str_vals.join(&separator)))
            },
            // Has or contains a value?
            "has" |
            "contains" => {
                if parameters.len() < 1 {
                    return Err(SError::array(pid, &doc, "contains", "requires a value argument to search for"));
                }
                let search = parameters.pop().unwrap();
                let mut found = false;
                for val in array {
                    if val == &search {
                        found = true;
                        break;
                    }
                }
                Ok(SVal::Bool(found))
            },
            // Find the first index of a value in this array.
            // Takes one value parameter and returns -1 if not found or the index if found.
            "find" => {
                if parameters.len() < 1 {
                    return Err(SError::array(pid, &doc, "find", "requires a value argument to search for"));
                }
                let search = parameters.pop().unwrap();
                let mut index = SVal::Number(SNum::I64(-1));
                for i in 0..array.len() {
                    if &array[i] == &search {
                        index = SVal::Number(SNum::I64(i as i64));
                        break;
                    }
                }
                Ok(index)
            },
            // Remove the first matching value from this array (not by index, but by value).
            // If trying to remove by index, see "pop".
            "remove" => {
                if parameters.len() < 1 {
                    return Err(SError::array(pid, &doc, "remove", "requires a value argument to search for"));
                }
                let search = parameters.pop().unwrap();
                let mut index = -1;
                for i in 0..array.len() {
                    if &array[i] == &search {
                        index = i as i32;
                        break;
                    }
                }
                if index > -1 {
                    Ok(array.remove(index as usize))
                } else {
                    Ok(SVal::Null)
                }
            },
            // Remove the last matching value from this array (not by index, but by value).
            // If trying to remove by index, see "pop".
            "removeLast" => {
                if parameters.len() < 1 {
                    return Err(SError::array(pid, &doc, "removeLast", "requires a value argument to search for"));
                }
                let search = parameters.pop().unwrap();
                let mut index = -1;
                for i in (0..array.len()).rev() {
                    if &array[i] == &search {
                        index = i as i32;
                        break;
                    }
                }
                if index > -1 {
                    Ok(array.remove(index as usize))
                } else {
                    Ok(SVal::Null)
                }
            },
            // Remove all matching values from this array (by value).
            "removeAll" => {
                if parameters.len() < 1 {
                    return Err(SError::array(pid, &doc, "removeAll", "search value argument not found"));
                }
                let mut results = Vec::new();
                for value in parameters.drain(..) {
                    let mut to_remove = Vec::new();
                    for i in 0..array.len() {
                        if &array[i] == &value {
                            to_remove.push(i);
                        }
                    }
                    to_remove.reverse();
                    let mut res = Vec::new();
                    for i in to_remove {
                        res.push(array.remove(i));
                    }
                    res.reverse();
                    results.append(&mut res);
                }
                Ok(SVal::Array(results))
            },
            // Insert values into the array at a specific index.
            "insert" => {
                if parameters.len() < 2 {
                    return Err(SError::array(pid, &doc, "insert", "requires an index and at least one value to insert"));
                }
                let index = parameters.remove(0);
                match index {
                    SVal::Number(num) => {
                        let mut index = num.int() as usize;
                        if index >= array.len() {
                            return Err(SError::array(pid, &doc, "insert", "out of bounds"));
                        }
                        for val in parameters.drain(..) {
                            array.insert(index, val);
                            index += 1;
                        }
                        Ok(SVal::Void)
                    },
                    _ => {
                        Err(SError::array(pid, &doc, "insert", "non-numerical index is not supported"))
                    }
                }
            },
            // Insert values into the array at a specific index, replacing the current value.
            "set" |
            "replace" => {
                if parameters.len() < 2 {
                    return Err(SError::array(pid, &doc, "set", "requires an index and at least one value to insert"));
                }
                let index = parameters.remove(0);
                match index {
                    SVal::Number(num) => {
                        let mut index = num.int() as usize;
                        if index >= array.len() {
                            return Err(SError::array(pid, &doc, "set", "out of bounds"));
                        }
                        array.remove(index);
                        for val in parameters.drain(..) {
                            array.insert(index, val);
                            index += 1;
                        }
                        Ok(SVal::Void)
                    },
                    _ => {
                        Err(SError::array(pid, &doc, "set", "non-numerical index not supported"))
                    }
                }
            },
            // Iterate over this array, calling a function for each value.
            "iter" => {
                if parameters.len() < 1 {
                    return Err(SError::array(pid, &doc, "iter", "function argument not found"));
                }
                match &parameters[0] {
                    SVal::FnPtr(dref) => {
                        if let Some(func) = SData::get::<SFunc>(&doc.graph, dref) {
                            let rtype = func.rtype.clone();
                            let statements = func.statements.clone();
                            let params = func.params.clone();
                            for val in array {
                                let res = SFunc::call_internal(dref, pid, doc, vec![val.clone()], true, &params, &statements, &rtype)?;
                                if !res.is_empty() {
                                    *val = res;
                                }
                            }
                        }
                        Ok(SVal::Void)
                    },
                    _ => return Err(SError::array(pid, &doc, "iter", "function argument not found"))
                }
            },
            // Retain values in this array, according to a function call.
            "retain" => {
                if parameters.len() < 1 {
                    return Err(SError::array(pid, &doc, "retain", "requires a predicate function argument"));
                }
                match &parameters[0] {
                    SVal::FnPtr(dref) => {
                        if let Some(func) = SData::get::<SFunc>(&doc.graph, dref) {
                            let rtype = func.rtype.clone();
                            let statements = func.statements.clone();
                            let params = func.params.clone();
                            array.retain(|val| -> bool {
                                let res = SFunc::call_internal(dref, pid, doc, vec![val.clone()], true, &params, &statements, &rtype).unwrap_or(SVal::Null);
                                res.truthy()
                            });
                        }
                        Ok(SVal::Void)
                    },
                    _ => return Err(SError::array(pid, &doc, "retain", "predicate function not found"))
                }
            },
            // Sort this array in-place.
            "sort" => {
                if parameters.len() == 0 {
                    array.sort_by(|a, b| {
                        let lt = a.lt(b);
                        if lt.is_err() {
                            return Ordering::Equal;
                        }
                        if lt.unwrap().truthy() {
                            return Ordering::Less;
                        }

                        let gt = a.gt(b);
                        if gt.is_err() {
                            return Ordering::Equal;
                        }
                        if gt.unwrap().truthy() {
                            return Ordering::Greater;
                        }
                        Ordering::Equal
                    });
                    Ok(SVal::Void)
                } else {
                    match &parameters[0] {
                        SVal::FnPtr(dref) => {
                            if let Some(func) = SData::get::<SFunc>(&doc.graph, dref) {
                                let rtype = func.rtype.clone();
                                let statements = func.statements.clone();
                                let params = func.params.clone();
                                array.sort_by(|a, b| {
                                    let res = SFunc::call_internal(dref, pid, doc, vec![a.clone(), b.clone()], true, &params, &statements, &rtype).unwrap_or(SVal::Number(SNum::I64(0)));
                                    match res {
                                        SVal::Number(num) => {
                                            let int = num.int();
                                            if int < 0 {
                                                Ordering::Less
                                            } else if int > 0 {
                                                Ordering::Greater
                                            } else {
                                                Ordering::Equal
                                            }
                                        },
                                        _ => {
                                            Ordering::Equal
                                        }
                                    }
                                });
                            }
                            Ok(SVal::Void)
                        },
                        _ => return Err(SError::array(pid, &doc, "sort", "function not found"))
                    }
                }
            },
            _ => {
                return Err(SError::array(pid, &doc, "NotFound", &format!("{} is not a function in the Array Library", name)));
            }
        }
    }
}
impl Library for ArrayLibrary {
    /// Scope.
    fn scope(&self) -> String {
        "Array".to_string()
    }
    
    /// Call into the Array library.
    fn call(&self, pid: &str, doc: &mut SDoc, name: &str, parameters: &mut Vec<SVal>) -> Result<SVal, SError> {
        if parameters.len() > 0 {
            match name {
                "toString" => {
                    return Ok(SVal::String(parameters[0].print(doc)));
                },
                "or" => {
                    for param in parameters.drain(..) {
                        if !param.is_empty() {
                            return Ok(param);
                        }
                    }
                    return Ok(SVal::Null);
                },
                _ => {}
            }

            let mut params;
            if parameters.len() > 1 {
                params = parameters.drain(1..).collect();
            } else {
                params = Vec::new();
            }
            match &mut parameters[0] {
                SVal::Array(vals) => {
                    return self.operate(pid, doc, name, vals, &mut params);
                },
                SVal::Boxed(val) => {
                    let mut val = val.lock().unwrap();
                    let val = val.deref_mut();
                    match val {
                        SVal::Array(vals) => {
                            return self.operate(pid, doc, name, vals, &mut params);
                        },
                        _ => {
                            return Err(SError::array(pid, &doc, "InvalidArgument", "array (vec) argument not found"));
                        }
                    }
                },
                _ => {
                    return Err(SError::array(pid, &doc, "InvalidArgument", "array (vec) argument not found"));
                }
            }
        } else {
            return Err(SError::array(pid, &doc, "InvalidArgument", "array (vec) argument not found"));
        }
    }
}