steam_vdf_parser/value/
types.rs1use alloc::borrow::Cow;
4use hashbrown::HashMap;
5
6pub(crate) type Key<'text> = Cow<'text, str>;
8
9#[derive(Clone, Debug, PartialEq)]
11pub enum Value<'text> {
12 Str(Cow<'text, str>),
14 Obj(Obj<'text>),
16 I32(i32),
18 U64(u64),
20 Float(f32),
22 Pointer(u32),
24 Color([u8; 4]),
26}
27
28impl<'text> Value<'text> {
29 pub fn is_str(&self) -> bool {
31 matches!(self, Value::Str(_))
32 }
33
34 pub fn is_obj(&self) -> bool {
36 matches!(self, Value::Obj(_))
37 }
38
39 pub fn is_i32(&self) -> bool {
41 matches!(self, Value::I32(_))
42 }
43
44 pub fn is_u64(&self) -> bool {
46 matches!(self, Value::U64(_))
47 }
48
49 pub fn is_float(&self) -> bool {
51 matches!(self, Value::Float(_))
52 }
53
54 pub fn is_pointer(&self) -> bool {
56 matches!(self, Value::Pointer(_))
57 }
58
59 pub fn is_color(&self) -> bool {
61 matches!(self, Value::Color(_))
62 }
63
64 pub fn as_str(&self) -> Option<&str> {
66 match self {
67 Value::Str(s) => Some(s.as_ref()),
68 _ => None,
69 }
70 }
71
72 pub fn as_obj(&self) -> Option<&Obj<'text>> {
74 match self {
75 Value::Obj(obj) => Some(obj),
76 _ => None,
77 }
78 }
79
80 pub fn as_obj_mut(&mut self) -> Option<&mut Obj<'text>> {
82 match self {
83 Value::Obj(obj) => Some(obj),
84 _ => None,
85 }
86 }
87
88 pub fn as_i32(&self) -> Option<i32> {
90 match self {
91 Value::I32(n) => Some(*n),
92 _ => None,
93 }
94 }
95
96 pub fn as_u64(&self) -> Option<u64> {
98 match self {
99 Value::U64(n) => Some(*n),
100 _ => None,
101 }
102 }
103
104 pub fn as_float(&self) -> Option<f32> {
106 match self {
107 Value::Float(n) => Some(*n),
108 _ => None,
109 }
110 }
111
112 pub fn as_pointer(&self) -> Option<u32> {
114 match self {
115 Value::Pointer(n) => Some(*n),
116 _ => None,
117 }
118 }
119
120 pub fn as_color(&self) -> Option<[u8; 4]> {
122 match self {
123 Value::Color(c) => Some(*c),
124 _ => None,
125 }
126 }
127
128 pub fn get(&self, key: &str) -> Option<&Value<'text>> {
132 self.as_obj()?.get(key)
133 }
134
135 pub fn get_path(&self, path: &[&str]) -> Option<&Value<'text>> {
139 let mut current = self;
140 for key in path {
141 current = current.get(key)?;
142 }
143 Some(current)
144 }
145
146 pub fn get_str(&self, path: &[&str]) -> Option<&str> {
148 self.get_path(path)?.as_str()
149 }
150
151 pub fn get_obj(&self, path: &[&str]) -> Option<&Obj<'text>> {
153 self.get_path(path)?.as_obj()
154 }
155
156 pub fn get_i32(&self, path: &[&str]) -> Option<i32> {
158 self.get_path(path)?.as_i32()
159 }
160
161 pub fn get_u64(&self, path: &[&str]) -> Option<u64> {
163 self.get_path(path)?.as_u64()
164 }
165
166 pub fn get_float(&self, path: &[&str]) -> Option<f32> {
168 self.get_path(path)?.as_float()
169 }
170}
171
172#[derive(Clone, Debug, PartialEq)]
177pub struct Obj<'text> {
178 pub(crate) inner: HashMap<Key<'text>, Value<'text>>,
179}
180
181impl<'text> Obj<'text> {
182 pub fn new() -> Self {
184 Self {
185 inner: HashMap::new(),
186 }
187 }
188
189 pub fn len(&self) -> usize {
191 self.inner.len()
192 }
193
194 pub fn is_empty(&self) -> bool {
196 self.inner.is_empty()
197 }
198
199 pub fn get(&self, key: &str) -> Option<&Value<'text>> {
201 self.inner.get(key)
202 }
203
204 pub fn iter(&self) -> impl Iterator<Item = (&Key<'text>, &Value<'text>)> {
206 self.inner.iter()
207 }
208
209 pub fn keys(&self) -> impl Iterator<Item = &str> {
211 self.inner.keys().map(|k| k.as_ref())
212 }
213
214 pub fn values(&self) -> impl Iterator<Item = &Value<'text>> {
216 self.inner.values()
217 }
218
219 pub fn contains_key(&self, key: &str) -> bool {
221 self.inner.contains_key(key)
222 }
223
224 pub fn get_mut(&mut self, key: &str) -> Option<&mut Value<'text>> {
226 self.inner.get_mut(key)
227 }
228
229 pub fn insert(
233 &mut self,
234 key: impl Into<Key<'text>>,
235 value: Value<'text>,
236 ) -> Option<Value<'text>> {
237 self.inner.insert(key.into(), value)
238 }
239
240 pub fn remove(&mut self, key: &str) -> Option<Value<'text>> {
244 self.inner.remove(key)
245 }
246}
247
248#[derive(Clone, Debug, PartialEq)]
252pub struct Vdf<'text> {
253 key: Key<'text>,
254 value: Value<'text>,
255}
256
257impl<'text> Vdf<'text> {
258 pub fn new(key: impl Into<Key<'text>>, value: Value<'text>) -> Self {
260 Self {
261 key: key.into(),
262 value,
263 }
264 }
265
266 pub fn key(&self) -> &str {
268 &self.key
269 }
270
271 pub fn value(&self) -> &Value<'text> {
273 &self.value
274 }
275
276 pub fn value_mut(&mut self) -> &mut Value<'text> {
278 &mut self.value
279 }
280
281 pub fn into_parts(self) -> (Cow<'text, str>, Value<'text>) {
283 (self.key, self.value)
284 }
285
286 pub fn is_obj(&self) -> bool {
288 self.value.is_obj()
289 }
290
291 pub fn as_obj(&self) -> Option<&Obj<'text>> {
293 self.value.as_obj()
294 }
295
296 pub fn get(&self, key: &str) -> Option<&Value<'text>> {
298 self.value.get(key)
299 }
300
301 pub fn get_path(&self, path: &[&str]) -> Option<&Value<'text>> {
303 self.value.get_path(path)
304 }
305
306 pub fn get_str(&self, path: &[&str]) -> Option<&str> {
308 self.value.get_str(path)
309 }
310
311 pub fn get_obj(&self, path: &[&str]) -> Option<&Obj<'text>> {
313 self.value.get_obj(path)
314 }
315
316 pub fn get_i32(&self, path: &[&str]) -> Option<i32> {
318 self.value.get_i32(path)
319 }
320
321 pub fn get_u64(&self, path: &[&str]) -> Option<u64> {
323 self.value.get_u64(path)
324 }
325
326 pub fn get_float(&self, path: &[&str]) -> Option<f32> {
328 self.value.get_float(path)
329 }
330}