sosaku 0.6.2

Filtering DSL for JSON and JSON-like data formats.
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
use std::{
    borrow::Cow,
    collections::{BTreeMap, HashMap},
    fmt::{Debug, Display},
    hash::BuildHasher,
};

#[cfg(feature = "serde_json")]
use serde::Deserialize;

use crate::{JsonMap, JsonValue, utils::escape_str_for_json};

/// The type of a literal value. This can be obtained using the [`Value::type_name`] method on a [`Value`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Type {
    /// An integer value (base 10, signed 64-bit integer).
    Integer,
    /// A UTF-8 string value.
    String,
    /// A boolean value (`true` or `false`).
    Bool,
    /// An IEEE-754 floating-point value (64-bit).
    Float,
    /// An array of values
    Array,
    /// A mapping of string keys to values
    Object,
    /// A null value, representing the absence of a value.
    /// This type has no associated data and is used to represent null literals and null values in JSON.
    /// It is distinct from other types and is considered falsy in boolean contexts.
    NullType,
}

impl Display for Type {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Integer => write!(f, "int"),
            Self::String => write!(f, "string"),
            Self::Bool => write!(f, "bool"),
            Self::Float => write!(f, "float"),
            Self::NullType => write!(f, "null"),
            Self::Array => write!(f, "array"),
            Self::Object => write!(f, "object"),
        }
    }
}

/// A literal value that can be used in expressions.
///
/// This type represents the various literal values that can be used in expressions,
/// such as integers, strings, booleans, floats, arrays, objects, and null.
///
/// It is designed to be flexible and can contain borrowed references
/// to data (e.g. from JSON values) or owned data.
///
/// The lifetime parameter `'a` is used for borrowing data from either bindings or the expression itself,
/// which allows for efficient handling of JSON values without unnecessary cloning.
///
/// To obtain a fully owned version of a [`Value`], you can use the [`Value::into_owned`] method,
/// which will clone any borrowed data and return a new [`Value`] with a `'static` lifetime.
#[derive(Debug, Clone, PartialEq)]
pub enum Value<'a> {
    /// An integer value (base 10, signed 64-bit integer).
    Int(i64),
    /// An IEEE-754 floating-point value (64-bit).
    Float(f64),
    /// A boolean value (`true` or `false`).
    Bool(bool),
    /// A UTF-8 string value or reference.
    String(Cow<'a, str>),
    /// A sequence of values
    Array(Vec<Self>),
    /// A mapping of string keys to values
    Object(BTreeMap<String, Self>),
    /// A null value, representing the absence of a value.
    /// This type has no associated data and is used to represent null literals and null values in JSON.
    /// It is distinct from other types and is considered falsy in boolean contexts.
    Null,
}

impl Value<'_> {
    /// Convert this [`Value`] into an owned version,
    /// all borrowed data will be cloned and the resulting [`Value`] will have a `'static` lifetime.
    ///
    /// This is useful for cases where you want to take ownership of a [`Value`] that may contain
    /// borrowed data (e.g. from a JSON value) and ensure that it is fully owned and independent of any original data sources.
    pub fn into_owned(self) -> Value<'static> {
        match self {
            Value::String(s) => Value::String(match s {
                Cow::Borrowed(b) => Cow::Owned(b.to_owned()),
                Cow::Owned(o) => Cow::Owned(o),
            }),
            Value::Int(i) => Value::Int(i),
            Value::Float(f) => Value::Float(f),
            Value::Bool(b) => Value::Bool(b),
            Value::Array(v) => Value::Array(v.into_iter().map(Value::into_owned).collect()),
            Value::Object(m) => {
                Value::Object(m.into_iter().map(|(k, v)| (k, v.into_owned())).collect())
            }
            Value::Null => Value::Null,
        }
    }

    /// Create a new [`Value`] from a JSON value reference,
    /// trying to convert it to the most specific literal type possible.
    ///
    /// # Returns
    ///
    /// The resulting [`Value`] value, which may contain borrowed references to the original JSON value's data.
    ///
    /// # Errors
    ///
    /// If the JSON value is an unsupported type (e.g. a JSON number that is not an integer or float),
    /// this function will return an error.
    pub fn from_json_object_ref<V: JsonValue>(value: &V) -> Result<Value<'_>, String> {
        if let Some(s) = value.as_str() {
            Ok(Value::String(Cow::Borrowed(s)))
        } else if let Some(i) = value.as_i64() {
            Ok(Value::Int(i))
        } else if let Some(f) = value.as_f64() {
            Ok(Value::Float(f))
        } else if let Some(b) = value.as_bool() {
            Ok(Value::Bool(b))
        } else if value.as_null().is_some() {
            Ok(Value::Null)
        } else if let Some(v) = value.as_array() {
            Ok(Value::Array(
                v.iter()
                    .map(Self::from_json_object_ref)
                    .collect::<Result<Vec<_>, _>>()?,
            ))
        } else if let Some(m) = value.as_object() {
            let new = m
                .iter()
                .map(|(k, v)| (k.clone(), Self::from_json_object_ref(v)))
                .map(|(k, rv)| rv.map(|v| (k, v)))
                .collect::<Result<BTreeMap<_, _>, _>>()?;

            Ok(Value::Object(new))
        } else {
            Err("Unsupported value type".into())
        }
    }

    /// Create a new [`Value`] from a JSON value, trying to convert it to the most specific literal type possible.
    ///
    /// # Returns
    ///
    /// The resulting [`Value`] value.
    ///
    /// # Errors
    ///
    /// If the JSON value is an unsupported type (e.g. a JSON number that is not an integer or float),
    /// this function will return an error.
    #[expect(clippy::missing_panics_doc)]
    pub fn from_json_object<'a, V: JsonValue + 'a>(value: V) -> Result<Value<'a>, String> {
        if value.is_string() {
            Ok(Value::String(Cow::Owned(
                value.into_string().expect("Expected JSON string"),
            )))
        } else if let Some(i) = value.as_i64() {
            Ok(Value::Int(i))
        } else if let Some(f) = value.as_f64() {
            Ok(Value::Float(f))
        } else if let Some(b) = value.as_bool() {
            Ok(Value::Bool(b))
        } else if value.as_null().is_some() {
            Ok(Value::Null)
        } else if value.is_array() {
            Ok(Value::Array(
                value
                    .into_array()
                    .expect("Expected JSON array")
                    .into_iter()
                    .map(|v| Self::from_json_object(v))
                    .collect::<Result<Vec<_>, _>>()?,
            ))
        } else if value.is_object() {
            let new = value
                .into_object()
                .expect("Expected JSON object")
                .into_iter()
                .map(|(k, v)| (k, Self::from_json_object(v)))
                .map(|(k, rv)| rv.map(|v| (k, v)))
                .collect::<Result<BTreeMap<_, _>, _>>()?;

            Ok(Value::Object(new))
        } else {
            Err("Unsupported value type".into())
        }
    }

    pub(crate) fn from_json_object_cow<V: JsonValue + Clone>(
        value: Cow<'_, V>,
    ) -> Result<Value<'_>, String> {
        match value {
            Cow::Borrowed(b) => Self::from_json_object_ref(b),
            Cow::Owned(o) => Self::from_json_object(o),
        }
    }
}

impl Value<'_> {
    /// The type of the value.
    pub const fn type_name(&self) -> Type {
        match self {
            Self::Int(_) => Type::Integer,
            Self::Float(_) => Type::Float,
            Self::Bool(_) => Type::Bool,
            Self::String(_) => Type::String,
            Self::Null => Type::NullType,
            Self::Array(_) => Type::Array,
            Self::Object(_) => Type::Object,
        }
    }
}

impl Display for Value<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Int(i) => write!(f, "{i}"),
            Self::Float(fl) => write!(f, "{fl}"),
            Self::Bool(b) => write!(f, "{b}"),
            Self::String(s) => write!(f, "{s}"),
            Self::Null => write!(f, "null"),
            Self::Array(arr) => {
                write!(f, "[")?;
                for (i, item) in arr.iter().enumerate() {
                    if i > 0 {
                        write!(f, ", ")?;
                    }
                    if let Self::String(s) = item {
                        write!(f, "\"{}\"", escape_str_for_json(s))?;
                    } else {
                        write!(f, "{item}")?;
                    }
                }
                write!(f, "]")
            }
            Self::Object(obj) => {
                write!(f, "{{")?;
                for (i, (k, v)) in obj.iter().enumerate() {
                    if i > 0 {
                        write!(f, ", ")?;
                    }
                    if let Self::String(s) = v {
                        write!(f, "\"{k}\": \"{}\"", escape_str_for_json(s))?;
                    } else {
                        write!(f, "\"{k}\": {v}")?;
                    }
                }
                write!(f, "}}")
            }
        }
    }
}

impl<'a> From<&'a Value<'a>> for bool {
    // Truthiness of a literal value:
    // - Integers are false if they are 0, true otherwise
    // - Floats are false if they are 0.0, true otherwise
    // - Booleans are their own truthiness
    // - Strings are false if they are empty, true otherwise
    fn from(lit: &'a Value<'a>) -> Self {
        match lit {
            Value::Int(i) => *i != 0,
            Value::Float(f) => *f != 0.0,
            Value::Bool(b) => *b,
            Value::String(s) => !s.is_empty(),
            Value::Null => false,
            Value::Array(arr) => !arr.is_empty(),
            Value::Object(obj) => !obj.is_empty(),
        }
    }
}

impl<'a> From<Value<'a>> for Type {
    fn from(lit: Value<'a>) -> Self {
        lit.type_name()
    }
}

impl<'a> TryFrom<&'a Value<'a>> for i64 {
    type Error = String;

    fn try_from(value: &'a Value<'a>) -> Result<Self, Self::Error> {
        match value {
            Value::Int(i) => Ok(*i),
            Value::Float(f) => Ok(*f as Self),
            Value::String(s) => s
                .parse()
                .map_err(|e| format!("Failed to parse string as integer: {e}")),
            Value::Bool(b) => Ok(Self::from(*b)),
            Value::Null => Err("Cannot convert null to integer".into()),
            Value::Array(_) => Err("Cannot convert array to integer".into()),
            Value::Object(_) => Err("Cannot convert object to integer".into()),
        }
    }
}

impl<'a> TryFrom<&'a Value<'a>> for f64 {
    type Error = String;

    fn try_from(value: &'a Value<'a>) -> Result<Self, Self::Error> {
        match value {
            Value::Float(f) => Ok(*f),
            Value::Int(i) => Ok(*i as Self),
            Value::String(s) => s
                .parse()
                .map_err(|e| format!("Failed to parse string as float: {e}")),
            Value::Bool(b) => Ok(Self::from(*b)),
            Value::Null => Err("Cannot convert null to float".into()),
            Value::Array(_) => Err("Cannot convert array to float".into()),
            Value::Object(_) => Err("Cannot convert object to float".into()),
        }
    }
}

impl FromIterator<Self> for Value<'_> {
    fn from_iter<T: IntoIterator<Item = Self>>(iter: T) -> Self {
        Self::Array(iter.into_iter().collect())
    }
}

impl<K> FromIterator<(K, Self)> for Value<'_>
where
    K: Into<String>,
{
    fn from_iter<T: IntoIterator<Item = (K, Self)>>(iter: T) -> Self {
        Self::Object(
            iter.into_iter()
                .map(|(k, v)| (k.into(), v))
                .collect::<BTreeMap<String, Value<'_>>>(),
        )
    }
}

impl From<String> for Value<'_> {
    fn from(s: String) -> Self {
        Self::String(Cow::Owned(s))
    }
}

impl<'a> From<&'a str> for Value<'a> {
    fn from(s: &'a str) -> Self {
        Self::String(Cow::Borrowed(s))
    }
}

impl From<bool> for Value<'_> {
    fn from(b: bool) -> Self {
        Self::Bool(b)
    }
}

impl From<i64> for Value<'_> {
    fn from(i: i64) -> Self {
        Self::Int(i)
    }
}

impl From<f64> for Value<'_> {
    fn from(f: f64) -> Self {
        Self::Float(f)
    }
}

impl From<i32> for Value<'_> {
    fn from(i: i32) -> Self {
        Self::Int(i64::from(i))
    }
}

impl From<f32> for Value<'_> {
    fn from(f: f32) -> Self {
        Self::Float(f64::from(f))
    }
}

impl From<()> for Value<'_> {
    fn from((): ()) -> Self {
        Self::Null
    }
}

impl<V: Into<Self>> From<Vec<V>> for Value<'_> {
    fn from(vec: Vec<V>) -> Self {
        Self::Array(vec.into_iter().map(Into::into).collect())
    }
}

impl<K, V> From<BTreeMap<K, V>> for Value<'_>
where
    K: Into<String>,
    V: Into<Self>,
{
    fn from(map: BTreeMap<K, V>) -> Self {
        Self::Object(
            map.into_iter()
                .map(|(k, v)| (k.into(), v.into()))
                .collect::<BTreeMap<String, Value<'_>>>(),
        )
    }
}

impl<K, V, S: BuildHasher> From<HashMap<K, V, S>> for Value<'_>
where
    K: Into<String>,
    V: Into<Self>,
{
    fn from(map: HashMap<K, V, S>) -> Self {
        Self::Object(
            map.into_iter()
                .map(|(k, v)| (k.into(), v.into()))
                .collect::<BTreeMap<String, Value<'_>>>(),
        )
    }
}

#[cfg(feature = "serde_json")]
impl<'de> Deserialize<'de> for Value<'de> {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let value = serde_json::Value::deserialize(deserializer)?;
        Self::try_from(value).map_err(serde::de::Error::custom)
    }
}

#[cfg(feature = "serde_json")]
impl TryFrom<serde_json::Value> for Value<'_> {
    type Error = String;

    fn try_from(value: serde_json::Value) -> Result<Self, Self::Error> {
        match value {
            serde_json::Value::String(s) => Ok(Self::String(s.into())),
            serde_json::Value::Number(n) if n.is_i64() => {
                Ok(Self::Int(n.as_i64().expect("Failed to parse integer")))
            }
            serde_json::Value::Number(n) if n.is_f64() => {
                Ok(Self::Float(n.as_f64().expect("Failed to parse float")))
            }
            serde_json::Value::Number(n) => Err(format!("Unsupported number type: {n}")),
            serde_json::Value::Bool(b) => Ok(Self::Bool(b)),
            serde_json::Value::Null => Ok(Self::Null),
            serde_json::Value::Array(arr) => Ok(Self::Array(
                arr.into_iter()
                    .map(Self::try_from)
                    .collect::<Result<Vec<_>, _>>()?,
            )),
            serde_json::Value::Object(obj) => {
                let new = obj
                    .into_iter()
                    .map(|(k, v)| (k, Self::try_from(v)))
                    .map(|(k, rv)| rv.map(|v| (k, v)))
                    .collect::<Result<BTreeMap<_, _>, _>>()?;

                Ok(Self::Object(new))
            }
        }
    }
}