Skip to main content

graphitepdf_utils/
value.rs

1use std::collections::BTreeMap;
2
3pub type Object = BTreeMap<String, Value>;
4
5#[derive(Clone, Debug, PartialEq)]
6pub enum Value {
7    Null,
8    Bool(bool),
9    Number(f64),
10    String(String),
11    Array(Vec<Value>),
12    Object(Object),
13}
14
15impl Value {
16    pub fn as_object(&self) -> Option<&Object> {
17        match self {
18            Self::Object(object) => Some(object),
19            _ => None,
20        }
21    }
22}
23
24impl From<bool> for Value {
25    fn from(value: bool) -> Self {
26        Self::Bool(value)
27    }
28}
29
30impl From<f64> for Value {
31    fn from(value: f64) -> Self {
32        Self::Number(value)
33    }
34}
35
36impl From<f32> for Value {
37    fn from(value: f32) -> Self {
38        Self::Number(f64::from(value))
39    }
40}
41
42impl From<i64> for Value {
43    fn from(value: i64) -> Self {
44        Self::Number(value as f64)
45    }
46}
47
48impl From<i32> for Value {
49    fn from(value: i32) -> Self {
50        Self::Number(f64::from(value))
51    }
52}
53
54impl From<usize> for Value {
55    fn from(value: usize) -> Self {
56        Self::Number(value as f64)
57    }
58}
59
60impl From<String> for Value {
61    fn from(value: String) -> Self {
62        Self::String(value)
63    }
64}
65
66impl From<&str> for Value {
67    fn from(value: &str) -> Self {
68        Self::String(value.to_string())
69    }
70}
71
72impl From<Vec<Value>> for Value {
73    fn from(value: Vec<Value>) -> Self {
74        Self::Array(value)
75    }
76}
77
78impl From<Object> for Value {
79    fn from(value: Object) -> Self {
80        Self::Object(value)
81    }
82}
83
84#[derive(Clone, Copy, Debug, PartialEq, Eq)]
85pub enum Path<'a> {
86    Key(&'a str),
87    Keys(&'a [&'a str]),
88}
89
90impl<'a> Path<'a> {
91    pub fn for_each(self, mut f: impl FnMut(&'a str)) {
92        match self {
93            Self::Key(key) => f(key),
94            Self::Keys(keys) => {
95                for key in keys {
96                    f(key);
97                }
98            }
99        }
100    }
101}
102
103impl<'a> From<&'a str> for Path<'a> {
104    fn from(value: &'a str) -> Self {
105        Self::Key(value)
106    }
107}
108
109impl<'a, const N: usize> From<&'a [&'a str; N]> for Path<'a> {
110    fn from(value: &'a [&'a str; N]) -> Self {
111        Self::Keys(value.as_slice())
112    }
113}
114
115impl<'a> From<&'a [&'a str]> for Path<'a> {
116    fn from(value: &'a [&'a str]) -> Self {
117        Self::Keys(value)
118    }
119}
120
121#[derive(Clone, Copy, Debug, PartialEq, Eq)]
122pub enum Keys<'a> {
123    Key(&'a str),
124    Many(&'a [&'a str]),
125}
126
127impl<'a> Keys<'a> {
128    pub fn for_each(self, mut f: impl FnMut(&'a str)) {
129        match self {
130            Self::Key(key) => f(key),
131            Self::Many(keys) => {
132                for key in keys {
133                    f(key);
134                }
135            }
136        }
137    }
138}
139
140impl<'a> From<&'a str> for Keys<'a> {
141    fn from(value: &'a str) -> Self {
142        Self::Key(value)
143    }
144}
145
146impl<'a, const N: usize> From<&'a [&'a str; N]> for Keys<'a> {
147    fn from(value: &'a [&'a str; N]) -> Self {
148        Self::Many(value.as_slice())
149    }
150}
151
152impl<'a> From<&'a [&'a str]> for Keys<'a> {
153    fn from(value: &'a [&'a str]) -> Self {
154        Self::Many(value)
155    }
156}