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
use std::fmt;
use tinyjson::JsonValue;

pub struct JsonExtError(String);

impl fmt::Display for JsonExtError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

impl fmt::Debug for JsonExtError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

impl std::error::Error for JsonExtError {}

impl JsonExtError {
    fn unexpected(value: &JsonValue, expected: &str) -> Self {
        JsonExtError(format!(
            "Expecting {expected}, but found {}",
            value.get_value_type()
        ))
    }
}

pub trait JsonValueExt {
    fn get_owned(self, key: &dyn JsonKey) -> Result<JsonValue, JsonExtError>;

    fn try_into_string(self) -> Result<String, JsonExtError>;

    fn get_value_type(&self) -> &'static str;
}

impl JsonValueExt for JsonValue {
    fn get_owned(self, key: &dyn JsonKey) -> Result<JsonValue, JsonExtError> {
        key.extract_from_value(self)
    }

    fn try_into_string(self) -> Result<String, JsonExtError> {
        match self {
            JsonValue::String(s) => Ok(s),
            value => Err(JsonExtError::unexpected(&value, "String")),
        }
    }

    fn get_value_type(&self) -> &'static str {
        match self {
            JsonValue::Number(..) => "Number",
            JsonValue::Boolean(..) => "Boolean",
            JsonValue::String(..) => "String",
            JsonValue::Null => "Null",
            JsonValue::Array(..) => "Array",
            JsonValue::Object(..) => "Object",
        }
    }
}

pub trait JsonKey {
    fn extract_from_value(&self, value: JsonValue) -> Result<JsonValue, JsonExtError>;
}

impl<T: JsonKey> JsonKey for &T {
    fn extract_from_value(&self, value: JsonValue) -> Result<JsonValue, JsonExtError> {
        T::extract_from_value(*self, value)
    }
}

impl JsonKey for usize {
    fn extract_from_value(&self, value: JsonValue) -> Result<JsonValue, JsonExtError> {
        let index = *self;

        match value {
            JsonValue::Array(mut values) => {
                let len = values.len();

                if index < len {
                    Ok(values.swap_remove(index))
                } else {
                    Err(JsonExtError(format!(
                        "Index {index} is too large for array of len {len}",
                    )))
                }
            }

            value => Err(JsonExtError::unexpected(&value, "Array")),
        }
    }
}

impl JsonKey for &str {
    fn extract_from_value(&self, value: JsonValue) -> Result<JsonValue, JsonExtError> {
        let key = *self;

        match value {
            JsonValue::Object(mut map) => map
                .remove(key)
                .ok_or_else(|| JsonExtError(format!("Key {key} not found in object"))),

            value => Err(JsonExtError::unexpected(&value, "Object")),
        }
    }
}