Skip to main content

immutable_json/
array.rs

1use crate::api::Number::{Decimal, Integer};
2use crate::api::{Number, Value};
3use crate::error::JsonIndexError;
4use crate::object::Object;
5use crate::pointer::JsonPointer;
6use imbl::Vector;
7use imbl::shared_ptr::DefaultSharedPtr;
8use imbl::vector::Iter;
9use imbl_util::vector;
10use std::cmp::PartialEq;
11use std::fmt::{Display, Formatter};
12use std::hash::Hash;
13
14/// Represents a JSON array.
15#[derive(Clone, Debug, Eq, PartialEq, Hash)]
16pub struct Array {
17    vec: Vector<Value>,
18}
19
20impl Default for Array {
21    fn default() -> Self {
22        Self::new()
23    }
24}
25
26impl Display for Array {
27    /**
28    Converts a JSON array to a string.
29    ```rust
30    # use immutable_json::api::Value;
31    # use immutable_json::error::Error;
32    # use std::str::FromStr;
33    # fn main() -> Result<(), Error>{
34    let data = r#"
35       [
36           "string",
37           1,
38           3.0,
39           false,
40           {"test": "test"},
41           [1]
42       ]"#;
43
44    let v: serde_json::Value = serde_json::from_str(data)?;
45
46    assert_eq!(Some(v), serde_json::from_str(&Value::from_str(data)?.to_string()).ok());
47    #   Ok(())
48    # }
49    ```
50    */
51    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
52        Display::fmt(&Value::Array(self.clone()), f)
53    }
54}
55
56impl FromIterator<Value> for Array {
57    /// Creates a JSON array from a stream of JSON values.
58    fn from_iter<T: IntoIterator<Item = Value>>(iter: T) -> Self {
59        iter.into_iter().fold(Self::new(), |a, v| a.add(&v))
60    }
61}
62
63impl<'a> IntoIterator for &'a Array {
64    type Item = Value;
65    type IntoIter = ArrayIter<'a>;
66
67    /// Iterates over the values in a JSON array,
68    fn into_iter(self) -> ArrayIter<'a> {
69        self.iter()
70    }
71}
72
73impl Array {
74    /**
75    Adds a JSON value to an array.
76    ```rust
77    # use immutable_json::array::Array;
78    # use immutable_json::api::Value;
79    # fn main() {
80    assert_eq!(Some(true), Array::new().add(&Value::Bool(true)).get_bool(0).ok().flatten());
81    # }
82    ```
83    */
84    pub fn add(&self, value: &Value) -> Self {
85        Self {
86            vec: vector::push_back(&self.vec, value.clone()),
87        }
88    }
89
90    /**
91    Adds an array to an array.
92    ```rust
93    # use immutable_json::array::Array;
94    # fn main() {
95    assert_eq!(
96        Some(0),
97        Array::new()
98            .add_array(&Array::new().add_integer(0))
99            .get_array(0).ok().flatten().and_then(|a| a.get_integer(0).ok().flatten()));
100    # }
101    ```
102    */
103    pub fn add_array(&self, value: &Array) -> Self {
104        self.add(&Value::Array(value.clone()))
105    }
106
107    pub fn add_array_p(&self, pointer: &str, value: &Array) -> Option<Self> {
108        self.add_p(pointer, &Value::Array(value.clone()))
109    }
110
111    /**
112    Adds a bool to an array.
113    ```rust
114    # use immutable_json::array::Array;
115    # fn main() {
116    assert_eq!(Some(true), Array::new().add_bool(true).get_bool(0).ok().flatten());
117    # }
118    ```
119    */
120    pub fn add_bool(&self, value: bool) -> Self {
121        self.add(&Value::Bool(value))
122    }
123
124    pub fn add_bool_p(&self, pointer: &str, value: bool) -> Option<Self> {
125        self.add_p(pointer, &Value::Bool(value))
126    }
127
128    /**
129    Adds a decimal to an array.
130    ```rust
131    # use immutable_json::array::Array;
132    # fn main() {
133    assert_eq!(Some(2.0), Array::new().add_decimal(2.0).get_decimal(0).ok().flatten());
134    # }
135    ```
136    */
137    pub fn add_decimal(&self, value: f64) -> Self {
138        self.add(&Value::Number(Decimal(value)))
139    }
140
141    pub fn add_decimal_p(&self, pointer: &str, value: f64) -> Option<Self> {
142        self.add_p(pointer, &Value::Number(Number::Decimal(value)))
143    }
144
145    /**
146    Adds an integer to an array.
147    ```rust
148    # use immutable_json::array::Array;
149    # fn main() {
150    assert_eq!(Some(0), Array::new().add_integer(0).get_integer(0).ok().flatten());
151    # }
152    ```
153    */
154    pub fn add_integer(&self, value: i128) -> Self {
155        self.add(&Value::Number(Integer(value)))
156    }
157
158    pub fn add_integer_p(&self, pointer: &str, value: i128) -> Option<Self> {
159        self.add_p(pointer, &Value::Number(Number::Integer(value)))
160    }
161
162    /**
163    Adds a number to an array.
164    ```rust
165    # use immutable_json::array::Array;
166    # use immutable_json::api::Number::Integer;
167    # fn main() {
168    assert_eq!(
169       Some(Integer(0)),
170       Array::new().add_number(Integer(0)).get_number(0).ok().flatten());
171    # }
172    ```
173    */
174    pub fn add_number(&self, value: Number) -> Self {
175        self.add(&Value::Number(value))
176    }
177
178    pub fn add_number_p(&self, pointer: &str, value: Number) -> Option<Self> {
179        self.add_p(pointer, &Value::Number(value))
180    }
181
182    /**
183    Adds an object to an array.
184    ```rust
185    # use immutable_json::array::Array;
186    # use immutable_json::object::Object;
187    # fn main() {
188    assert_eq!(
189       Some(0),
190       Array::new()
191           .add_object(&Object::new().add_integer("test", 0))
192           .get_object(0).ok().flatten().and_then(|o| o.get_integer("test")));
193    # }
194    ```
195    */
196    pub fn add_object(&self, value: &Object) -> Self {
197        self.add(&Value::Object(value.clone()))
198    }
199
200    pub fn add_object_p(&self, pointer: &str, value: &Object) -> Option<Self> {
201        self.add_p(pointer, &Value::Object(value.clone()))
202    }
203
204    pub fn add_p(&self, pointer: &str, value: &Value) -> Option<Self> {
205        JsonPointer::add_pointer(pointer, &Value::Array(self.clone()), value)
206            .and_then(|v| v.as_array())
207    }
208
209    /**
210    Adds a string to an array.
211    ```rust
212    # use immutable_json::array::Array;
213    # fn main() {
214    assert_eq!(
215       Some("test".to_string()),
216       Array::new().add_string("test").get_string(0).ok().flatten());
217    # }
218    ```
219    */
220    pub fn add_string(&self, value: &str) -> Self {
221        self.add(&Value::String(value.to_string()))
222    }
223
224    pub fn add_string_p(&self, pointer: &str, value: &str) -> Option<Self> {
225        self.add_p(pointer, &Value::String(value.to_string()))
226    }
227
228    /**
229    Appends an array to the current array.
230    ```rust
231    # use immutable_json::array::Array;
232    # fn main() {
233    assert_eq!(
234       Array::new().add_integer(0).add_integer(1),
235       Array::new().add_integer(0).append(&Array::new().add_integer(1)))
236    # }
237    ```
238    */
239    pub fn append(&self, array: &Array) -> Self {
240        Self {
241            vec: vector::append(&self.vec, &array.vec),
242        }
243    }
244
245    /// Gets a JSON value from an array at a given index, which is positive and less than the length
246    /// of the array.
247    pub fn get(&self, index: usize) -> Result<Value, JsonIndexError> {
248        self.vec.get(index).cloned().ok_or_else(|| JsonIndexError {
249            index,
250            len: self.len(),
251        })
252    }
253
254    /**
255    Gets an array from an array at a given index, which is positive and less than the length
256    of the array. If the value at the position is not an array, `None` is returned.
257    ```rust
258    # use immutable_json::array::Array;
259    # use immutable_json::api::Number::Integer;
260    # use immutable_json::error::JsonIndexError;
261    # fn main() {
262    let array = Array::new().add_number(Integer(0));
263
264    assert_eq!(Some(Integer(0)), array.get_number(0).ok().flatten());
265    assert_eq!(None, array.get_string(0).ok().flatten());
266    assert_eq!(Some(JsonIndexError { index: 1, len: 1, }), array.get_number(1).err());
267    # }
268    ```
269    */
270    pub fn get_array(&self, index: usize) -> Result<Option<Array>, JsonIndexError> {
271        self.get(index).map(|v| v.as_array())
272    }
273
274    pub fn get_array_p(&self, pointer: &str) -> Option<Array> {
275        self.get_p(pointer).and_then(|v| v.as_array())
276    }
277
278    /**
279    Gets a bool from an array at a given index, which is positive and less than the length
280    of the array. If the value at the position is not a Boolean, `None` is returned.
281    ```rust
282    # use immutable_json::array::Array;
283    # use immutable_json::error::JsonIndexError;
284    # fn main() {
285    let array = Array::new().add_bool(true);
286
287    assert_eq!(Some(true), array.get_bool(0).ok().flatten());
288    assert_eq!(None, array.get_string(0).ok().flatten());
289    assert_eq!(Some(JsonIndexError { index: 1, len: 1, }), array.get_bool(1).err());
290    # }
291    ```
292    */
293    pub fn get_bool(&self, index: usize) -> Result<Option<bool>, JsonIndexError> {
294        self.get(index).map(|v| v.as_bool())
295    }
296
297    pub fn get_bool_p(&self, pointer: &str) -> Option<bool> {
298        self.get_p(pointer).and_then(|v| v.as_bool())
299    }
300
301    /**
302    Gets a decimal from an array at a given index, which is positive and less than the length
303    of the array. If the value at the position is not a decimal, `None` is returned.
304    ```rust
305    # use immutable_json::array::Array;
306    # use immutable_json::error::JsonIndexError;
307    # fn main() {
308    let array = Array::new().add_decimal(2.0);
309
310    assert_eq!(Some(2.0), array.get_decimal(0).ok().flatten());
311    assert_eq!(None, array.get_string(0).ok().flatten());
312    assert_eq!(Some(JsonIndexError { index: 1, len: 1, }), array.get_decimal(1).err());
313    # }
314    ```
315    */
316    pub fn get_decimal(&self, index: usize) -> Result<Option<f64>, JsonIndexError> {
317        self.get(index).map(|v| v.as_decimal())
318    }
319
320    pub fn get_decimal_p(&self, pointer: &str) -> Option<f64> {
321        self.get_p(pointer).and_then(|v| v.as_decimal())
322    }
323
324    /**
325    Gets an integer from an array at a given index, which is positive and less than the length
326    of the array. If the value at the position is not an integer, `None` is returned.
327    ```rust
328    # use immutable_json::array::Array;
329    # use immutable_json::error::JsonIndexError;
330    # fn main() {
331    let array = Array::new().add_integer(0);
332
333    assert_eq!(Some(0), array.get_integer(0).ok().flatten());
334    assert_eq!(None, array.get_string(0).ok().flatten());
335    assert_eq!(Some(JsonIndexError { index: 1, len: 1, }), array.get_integer(1).err());
336    # }
337    ```
338    */
339    pub fn get_integer(&self, index: usize) -> Result<Option<i128>, JsonIndexError> {
340        self.get(index).map(|v| v.as_integer())
341    }
342
343    pub fn get_integer_p(&self, pointer: &str) -> Option<i128> {
344        self.get_p(pointer).and_then(|v| v.as_integer())
345    }
346
347    /**
348    Gets a number from an array at a given index, which is positive and less than the length
349    of the array. If the value at the position is not a number, `None` is returned.
350    ```rust
351    # use immutable_json::array::Array;
352    # use immutable_json::api::Number::Integer;
353    # use immutable_json::error::JsonIndexError;
354    # fn main() {
355    let array = Array::new().add_number(Integer(0));
356
357    assert_eq!(Some(Integer(0)), array.get_number(0).ok().flatten());
358    assert_eq!(None, array.get_string(0).ok().flatten());
359    assert_eq!(Some(JsonIndexError { index: 1, len: 1, }), array.get_number(1).err());
360    # }
361    ```
362    */
363    pub fn get_number(&self, index: usize) -> Result<Option<Number>, JsonIndexError> {
364        self.get(index).map(|v| v.as_number())
365    }
366
367    pub fn get_number_p(&self, pointer: &str) -> Option<Number> {
368        self.get_p(pointer).and_then(|v| v.as_number())
369    }
370
371    /**
372    Gets an object from an array at a given index, which is positive and less than the length
373    of the array. If the value at the position is not a JSON object, `None` is returned.
374    ```rust
375    # use immutable_json::array::Array;
376    # use immutable_json::object::Object;
377    # use immutable_json::error::JsonIndexError;
378    # fn main() {
379    let array = Array::new().add_object(&Object::new().add_integer("test", 0));
380
381    assert_eq!(Some(0),
382       array.get_object(0).ok().flatten().and_then(|o| o.get_integer("test")));
383    assert_eq!(None, array.get_string(0).ok().flatten());
384    assert_eq!(Some(JsonIndexError { index: 1, len: 1, }), array.get_object(1).err());
385    # }
386    ```
387    */
388    pub fn get_object(&self, index: usize) -> Result<Option<Object>, JsonIndexError> {
389        self.get(index).map(|v| v.as_object())
390    }
391
392    pub fn get_object_p(&self, pointer: &str) -> Option<Object> {
393        self.get_p(pointer).and_then(|v| v.as_object())
394    }
395
396    pub fn get_p(&self, pointer: &str) -> Option<Value> {
397        JsonPointer::get_pointer(pointer, &Value::Array(self.clone()))
398    }
399
400    /**
401    Gets a string from an array at a given index, which is positive and less than the length
402    of the array. If the value at the position is not a string, `None` is returned.
403    ```rust
404    # use immutable_json::array::Array;
405    # use immutable_json::error::JsonIndexError;
406    # fn main() {
407    let array = Array::new().add_string("test");
408
409    assert_eq!(Some("test".to_string()), array.get_string(0).ok().flatten());
410    assert_eq!(None, array.get_integer(0).ok().flatten());
411    assert_eq!(Some(JsonIndexError { index: 1, len: 1, }), array.get_string(1).err());
412    # }
413    ```
414    */
415    pub fn get_string(&self, index: usize) -> Result<Option<String>, JsonIndexError> {
416        self.get(index).map(|v| v.as_string())
417    }
418
419    pub fn get_string_p(&self, pointer: &str) -> Option<String> {
420        self.get_p(pointer).and_then(|v| v.as_string())
421    }
422
423    /**
424    Inserts a JSON value to an array at a given index. If the index is equal to the length of
425    the array, the value is added at the end of it.
426    ```rust
427    # use immutable_json::array::Array;
428    # use immutable_json::api::Value;
429    # use immutable_json::error::Error;
430    # fn main() -> Result<(), Error>{
431    assert_eq!(Some(true), Array::new().insert(0, &Value::Bool(true))?.get_bool(0).ok().flatten());
432    # Ok(())
433    # }
434    ```
435    */
436    pub fn insert(&self, index: usize, value: &Value) -> Result<Self, JsonIndexError> {
437        match vector::insert(&self.vec, index, value.clone()) {
438            Some(v) => Ok(Self { vec: v }),
439            None => Err(JsonIndexError {
440                index,
441                len: self.len(),
442            }),
443        }
444    }
445
446    /**
447    Inserts an array to an array at a given index. If the index is equal to the length of
448    the array, the value is added at the end of it.
449    ```rust
450    # use immutable_json::array::Array;
451    # use immutable_json::error::Error;
452    # fn main() -> Result<(), Error>{
453    assert_eq!(
454       Some(0),
455       Array::new()
456           .insert_array(0, &Array::new().insert_integer(0, 0)?)?
457           .get_array(0).ok().flatten().and_then(|a| a.get_integer(0).ok().flatten()));
458    # Ok(())
459    # }
460    ```
461    */
462    pub fn insert_array(&self, index: usize, value: &Array) -> Result<Self, JsonIndexError> {
463        self.insert(index, &Value::Array(value.clone()))
464    }
465
466    /**
467    Inserts a bool to an array at a given index. If the index is equal to the length of
468    the array, the value is added at the end of it.
469    ```rust
470    # use immutable_json::array::Array;
471    # use immutable_json::error::Error;
472    # fn main() -> Result<(), Error>{
473    assert_eq!(Some(true), Array::new().insert_bool(0, true)?.get_bool(0).ok().flatten());
474    # Ok(())
475    # }
476    ```
477    */
478    pub fn insert_bool(&self, index: usize, value: bool) -> Result<Self, JsonIndexError> {
479        self.insert(index, &Value::Bool(value))
480    }
481
482    /**
483    Inserts a decimal to an array at a given index. If the index is equal to the length of
484    the array, the value is added at the end of it.
485    ```rust
486    # use immutable_json::array::Array;
487    # use immutable_json::error::Error;
488    # fn main() -> Result<(), Error>{
489    assert_eq!(Some(2.0), Array::new().insert_decimal(0, 2.0)?.get_decimal(0).ok().flatten());
490    # Ok(())
491    # }
492    ```
493    */
494    pub fn insert_decimal(&self, index: usize, value: f64) -> Result<Self, JsonIndexError> {
495        self.insert(index, &Value::Number(Decimal(value)))
496    }
497
498    /**
499    Inserts an integer to an array at a given index. If the index is equal to the length of
500    the array, the value is added at the end of it.
501    ```rust
502    # use immutable_json::array::Array;
503    # use immutable_json::error::Error;
504    # fn main() -> Result<(), Error>{
505    assert_eq!(Some(0), Array::new().insert_integer(0, 0)?.get_integer(0).ok().flatten());
506    # Ok(())
507    # }
508    ```
509    */
510    pub fn insert_integer(&self, index: usize, value: i128) -> Result<Self, JsonIndexError> {
511        self.insert(index, &Value::Number(Integer(value)))
512    }
513
514    /**
515    Inserts a number to an array at a given index. If the index is equal to the length of
516    the array, the value is added at the end of it.
517    ```rust
518    # use immutable_json::array::Array;
519    # use immutable_json::api::Number::Integer;
520    # use immutable_json::error::Error;
521    # fn main() -> Result<(), Error>{
522    assert_eq!(
523       Some(Integer(0)),
524       Array::new().insert_number(0, Integer(0))?.get_number(0).ok().flatten());
525    # Ok(())
526    # }
527    ```
528    */
529    pub fn insert_number(&self, index: usize, value: Number) -> Result<Self, JsonIndexError> {
530        self.insert(index, &Value::Number(value))
531    }
532
533    /**
534    Inserts an object to an array at a given index. If the index is equal to the length of
535    the array, the value is added at the end of it.
536    ```rust
537    # use immutable_json::array::Array;
538    # use immutable_json::object::Object;
539    # use immutable_json::error::Error;
540    # fn main() -> Result<(), Error>{
541    assert_eq!(
542       Some(0),
543       Array::new()
544           .insert_object(0, &Object::new().add_integer("test", 0))?
545           .get_object(0).ok().flatten().and_then(|o| o.get_integer("test")));
546    # Ok(())
547    # }
548    ```
549    */
550    pub fn insert_object(&self, index: usize, value: &Object) -> Result<Self, JsonIndexError> {
551        self.insert(index, &Value::Object(value.clone()))
552    }
553
554    /**
555    Inserts a string to an array at a given index. If the index is equal to the length of
556    the array, the value is added at the end of it.
557    ```rust
558    # use immutable_json::array::Array;
559    # use immutable_json::error::Error;
560    # fn main() -> Result<(), Error>{
561    assert_eq!(
562       Some("test".to_string()),
563       Array::new().insert_string(0, "test")?.get_string(0).ok().flatten());
564    # Ok(())
565    # }
566    ```
567    */
568    pub fn insert_string(&self, index: usize, value: &str) -> Result<Self, JsonIndexError> {
569        self.insert(index, &Value::String(value.to_string()))
570    }
571
572    /**
573    Indicates if the array is empty or not.
574    ```rust
575    # use immutable_json::array::Array;
576    # fn main() {
577    assert_eq!(true, Array::new().is_empty());
578    assert_eq!(false, Array::new().add_integer(0).is_empty());
579    # }
580    ```
581    */
582    pub fn is_empty(&self) -> bool {
583        self.vec.is_empty()
584    }
585
586    /**
587    Returns an iterator over the values of the array.
588    ```rust
589    # use immutable_json::array::Array;
590    # fn main() {
591    let array = Array::new().add_string("test").add_integer(1);
592
593    assert_eq!(array, Array::from_iter(array.iter()));
594    # }
595    ```
596    */
597    pub fn iter(&'_ self) -> ArrayIter<'_> {
598        ArrayIter {
599            iter: self.vec.iter(),
600        }
601    }
602
603    /**
604    Returns the length of an array.
605    ```rust
606    # use immutable_json::array::Array;
607    # fn main() {
608    assert_eq!(0, Array::new().len());
609    assert_eq!(1, Array::new().add_integer(0).len());
610    # }
611    ```
612    */
613    pub fn len(&self) -> usize {
614        self.vec.len()
615    }
616
617    /// Creates an empty JSON array.
618    pub fn new() -> Self {
619        Self { vec: Vector::new() }
620    }
621
622    /**
623    Removes a JSON value from an array at a given index, which is positive and less than the
624    length of the array.
625    ```rust
626    # use immutable_json::array::Array;
627    # use immutable_json::error::JsonIndexError;
628    # fn main() {
629    assert_eq!(Some(0), Array::new().add_integer(0).remove(0).ok().map(|a| a.len()));
630    assert_eq!(Some(JsonIndexError { index: 1, len: 0, }), Array::new().remove(1).err());
631    # }
632    ```
633    */
634    pub fn remove(&self, index: usize) -> Result<Self, JsonIndexError> {
635        if index >= self.len() {
636            Err(JsonIndexError {
637                index,
638                len: self.len(),
639            })
640        } else {
641            let mut new_vec = self.vec.clone();
642
643            new_vec.remove(index);
644            Ok(Self { vec: new_vec })
645        }
646    }
647
648    pub fn remove_p(&self, pointer: &str) -> Option<Self> {
649        JsonPointer::remove_pointer(pointer, &Value::Array(self.clone())).and_then(|v| v.as_array())
650    }
651
652    /// Sets a JSON value in an array at a given index, which is positive and less than the length
653    /// of the array.
654    pub fn set(&self, index: usize, value: &Value) -> Result<Self, JsonIndexError> {
655        if index >= self.len() {
656            Err(JsonIndexError {
657                index,
658                len: self.len(),
659            })
660        } else {
661            let mut new_vec = self.vec.clone();
662
663            new_vec.set(index, value.clone());
664            Ok(Self { vec: new_vec })
665        }
666    }
667
668    /**
669    Sets an array in an array at a given index, which is positive and less than the length
670    of the array.
671    ```rust
672    # use immutable_json::array::Array;
673    # use immutable_json::error::JsonIndexError;
674    # fn main() {
675    let array = Array::new().add_bool(true);
676
677    assert_eq!(
678       Some(0),
679       array
680           .set_array(0, &Array::new().add_integer(0))
681           .ok()
682           .and_then(|a| {
683               a.get_array(0).ok().flatten().and_then(|a| a.get_integer(0).ok().flatten())
684           }));
685    assert_eq!(Some(JsonIndexError { index: 1, len: 1, }), array.set_bool(1, false).err());
686    # }
687    ```
688    */
689    pub fn set_array(&self, index: usize, value: &Array) -> Result<Self, JsonIndexError> {
690        self.set(index, &Value::Array(value.clone()))
691    }
692
693    pub fn set_array_p(&self, pointer: &str, value: &Array) -> Option<Self> {
694        self.set_p(pointer, &Value::Array(value.clone()))
695    }
696
697    /**
698    Sets a bool in an array at a given index, which is positive and less than the length
699    of the array.
700    ```rust
701    # use immutable_json::array::Array;
702    # use immutable_json::error::JsonIndexError;
703    # fn main() {
704    let array = Array::new().add_bool(true);
705
706    assert_eq!(
707       Some(false),
708       array
709           .set_bool(0, false)
710           .ok()
711           .and_then(|a| a.get_bool(0).ok().flatten()));
712    assert_eq!(Some(JsonIndexError { index: 1, len: 1, }), array.set_bool(1, false).err());
713    # }
714    ```
715    */
716    pub fn set_bool(&self, index: usize, value: bool) -> Result<Self, JsonIndexError> {
717        self.set(index, &Value::Bool(value))
718    }
719
720    pub fn set_bool_p(&self, pointer: &str, value: bool) -> Option<Self> {
721        self.set_p(pointer, &Value::Bool(value))
722    }
723
724    /**
725    Sets a decimal in an array at a given index, which is positive and less than the length
726    of the array.
727    ```rust
728    # use immutable_json::array::Array;
729    # use immutable_json::error::JsonIndexError;
730    # fn main() {
731    let array = Array::new().add_bool(true);
732
733    assert_eq!(
734       Some(3.0),
735       array
736           .set_decimal(0, 3.0)
737           .ok()
738           .and_then(|a| a.get_decimal(0).ok().flatten()));
739    assert_eq!(Some(JsonIndexError { index: 1, len: 1, }), array.set_decimal(1, 1.0).err());
740    # }
741    ```
742    */
743    pub fn set_decimal(&self, index: usize, value: f64) -> Result<Self, JsonIndexError> {
744        self.set(index, &Value::Number(Decimal(value)))
745    }
746
747    pub fn set_decimal_p(&self, pointer: &str, value: f64) -> Option<Self> {
748        self.set_p(pointer, &Value::Number(Number::Decimal(value)))
749    }
750
751    /**
752    Sets an integer in an array at a given index, which is positive and less than the length
753    of the array.
754    ```rust
755    # use immutable_json::array::Array;
756    # use immutable_json::error::JsonIndexError;
757    # fn main() {
758    let array = Array::new().add_bool(true);
759
760    assert_eq!(
761       Some(3),
762       array
763           .set_integer(0, 3)
764           .ok()
765           .and_then(|a| a.get_integer(0).ok().flatten()));
766    assert_eq!(Some(JsonIndexError { index: 1, len: 1, }), array.set_integer(1, 1).err());
767    # }
768    ```
769    */
770    pub fn set_integer(&self, index: usize, value: i128) -> Result<Self, JsonIndexError> {
771        self.set(index, &Value::Number(Integer(value)))
772    }
773
774    pub fn set_integer_p(&self, pointer: &str, value: i128) -> Option<Self> {
775        self.set_p(pointer, &Value::Number(Number::Integer(value)))
776    }
777
778    /**
779    Sets a number in an array at a given index, which is positive and less than the length
780    of the array.
781    ```rust
782    # use immutable_json::array::Array;
783    # use immutable_json::api::Number::Decimal;
784    # use immutable_json::error::JsonIndexError;
785    # fn main() {
786    let array = Array::new().add_bool(true);
787
788    assert_eq!(
789       Some(Decimal(3.0)),
790       array
791           .set_number(0, Decimal(3.0))
792           .ok()
793           .and_then(|a| a.get_number(0).ok().flatten()));
794    assert_eq!(
795       Some(JsonIndexError { index: 1, len: 1, }),
796       array.set_number(1, Decimal(1.0)).err());
797    # }
798    ```
799    */
800    pub fn set_number(&self, index: usize, value: Number) -> Result<Self, JsonIndexError> {
801        self.set(index, &Value::Number(value))
802    }
803
804    pub fn set_number_p(&self, pointer: &str, value: Number) -> Option<Self> {
805        self.set_p(pointer, &Value::Number(value))
806    }
807
808    /**
809    Sets an object in an array at a given index, which is positive and less than the length
810    of the array.
811    ```rust
812    # use immutable_json::array::Array;
813    # use immutable_json::object::Object;
814    # use immutable_json::error::JsonIndexError;
815    # fn main() {
816    let array = Array::new().add_bool(true);
817
818    assert_eq!(
819       Some("test".to_string()),
820       array
821           .set_object(0, &Object::new().add_string("test", "test"))
822           .ok()
823           .and_then(|a| a.get_object(0).ok().flatten().and_then(|o| o.get_string("test"))));
824    assert_eq!(
825       Some(JsonIndexError { index: 1, len: 1, }),
826       array.set_object(1, &Object::new()).err());
827    # }
828    ```
829    */
830    pub fn set_object(&self, index: usize, value: &Object) -> Result<Self, JsonIndexError> {
831        self.set(index, &Value::Object(value.clone()))
832    }
833
834    pub fn set_object_p(&self, pointer: &str, value: &Object) -> Option<Self> {
835        self.set_p(pointer, &Value::Object(value.clone()))
836    }
837
838    pub fn set_p(&self, pointer: &str, value: &Value) -> Option<Self> {
839        JsonPointer::set_pointer(pointer, &Value::Array(self.clone()), value)
840            .and_then(|v| v.as_array())
841    }
842
843    /**
844    Sets a string in an array at a given index, which is positive and less than the length
845    of the array.
846    ```rust
847    # use immutable_json::array::Array;
848    # use immutable_json::error::JsonIndexError;
849    # fn main() {
850    let array = Array::new().add_bool(true);
851
852    assert_eq!(
853       Some("test".to_string()),
854       array
855           .set_string(0, "test")
856           .ok()
857           .and_then(|a| a.get_string(0).ok().flatten()));
858    assert_eq!(Some(JsonIndexError { index: 1, len: 1, }), array.set_string(1, "test").err());
859    # }
860    ```
861    */
862    pub fn set_string(&self, index: usize, value: &str) -> Result<Self, JsonIndexError> {
863        self.set(index, &Value::String(value.to_string()))
864    }
865
866    pub fn set_string_p(&self, pointer: &str, value: &str) -> Option<Self> {
867        self.set_p(pointer, &Value::String(value.to_string()))
868    }
869}
870
871#[derive(Clone)]
872pub struct ArrayIter<'a> {
873    iter: Iter<'a, Value, DefaultSharedPtr>,
874}
875
876impl<'a> Iterator for ArrayIter<'a> {
877    type Item = Value;
878
879    fn next(&mut self) -> Option<Self::Item> {
880        self.iter.next().cloned()
881    }
882}