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
/*
==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--

Binn-IR

Copyright (C) 2018-2023  Anonymous

There are several releases over multiple years,
they are listed as ranges, such as: "2018-2023".

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.

::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--
*/

//! # Container functions

use crate::{Blob, List, Map, MapKey, Object, ObjectKey, Value};

/// # Makes new blob
pub fn blob() -> Value {
    Value::Blob(Blob::new())
}

/// # Makes new blob with capacity
pub fn blob_with_capacity(capacity: usize) -> Value {
    Value::Blob(Blob::with_capacity(capacity))
}

/// # Makes new list
pub fn list() -> Value {
    Value::List(List::new())
}

/// # Makes new list with capacity
pub fn list_with_capacity(capacity: usize) -> Value {
    Value::List(List::with_capacity(capacity))
}

/// # Pushes new value into a list
pub fn push<T>(list: &mut List, value: T) where T: Into<Value> {
    list.push(value.into());
}

/// # Makes new map
pub fn map() -> Value {
    Value::Map(Map::new())
}

/// # Makes new map from one pair of key/value
pub fn map_from<K, V>(key: K, value: V) -> Value where K: Into<MapKey>, V: Into<Value> {
    let mut map = Map::new();
    map_insert(&mut map, key, value);
    map.into()
}

/// # Inserts new item into a map
///
/// Returns previous value (if it existed).
pub fn map_insert<K, V>(map: &mut Map, key: K, value: V) -> Option<Value> where K: Into<MapKey>, V: Into<Value> {
    map.insert(key.into(), value.into())
}

/// # Makes new object
pub fn object() -> Value {
    Value::Object(Object::new())
}

/// # Makes new object from one pair of key/value
pub fn object_from<K, V>(key: K, value: V) -> Value where K: Into<ObjectKey>, V: Into<Value> {
    let mut object = Object::new();
    object_insert(&mut object, key, value);
    object.into()
}

/// # Inserts new item into an object
///
/// Returns previous value (if it existed).
pub fn object_insert<K, V>(object: &mut Object, key: K, value: V) -> Option<Value> where K: Into<ObjectKey>, V: Into<Value> {
    object.insert(key.into(), value.into())
}