steam_vdf_parser/value/
impls.rs

1//! Trait implementations for VDF types.
2
3use alloc::borrow::Cow;
4use alloc::string::String;
5
6use super::types::{Key, Obj, Value, Vdf};
7use core::fmt;
8use core::ops::Index;
9
10// ============================================================================
11// From implementations for Value
12// ============================================================================
13
14impl<'text> From<&'text str> for Value<'text> {
15    fn from(s: &'text str) -> Self {
16        Value::Str(Cow::Borrowed(s))
17    }
18}
19
20impl From<String> for Value<'static> {
21    fn from(s: String) -> Self {
22        Value::Str(Cow::Owned(s))
23    }
24}
25
26impl From<i32> for Value<'static> {
27    fn from(n: i32) -> Self {
28        Value::I32(n)
29    }
30}
31
32impl From<u64> for Value<'static> {
33    fn from(n: u64) -> Self {
34        Value::U64(n)
35    }
36}
37
38impl From<f32> for Value<'static> {
39    fn from(n: f32) -> Self {
40        Value::Float(n)
41    }
42}
43
44impl From<u32> for Value<'static> {
45    fn from(n: u32) -> Self {
46        Value::Pointer(n)
47    }
48}
49
50impl From<[u8; 4]> for Value<'static> {
51    fn from(color: [u8; 4]) -> Self {
52        Value::Color(color)
53    }
54}
55
56impl<'text> From<Obj<'text>> for Value<'text> {
57    fn from(obj: Obj<'text>) -> Self {
58        Value::Obj(obj)
59    }
60}
61
62impl<'text> fmt::Display for Value<'text> {
63    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64        match self {
65            Value::Str(s) => write!(f, "{}", s),
66            Value::Obj(obj) => write!(f, "{}", obj),
67            Value::I32(n) => write!(f, "{}", n),
68            Value::U64(n) => write!(f, "{}", n),
69            Value::Float(n) => write!(f, "{}", n),
70            Value::Pointer(n) => write!(f, "0x{:08x}", n),
71            Value::Color(c) => write!(f, "{}{}{}{}", c[0], c[1], c[2], c[3]),
72        }
73    }
74}
75
76impl<'text> Default for Obj<'text> {
77    fn default() -> Self {
78        Self::new()
79    }
80}
81
82impl<'text> fmt::Display for Obj<'text> {
83    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
84        write!(f, "{{")?;
85        for (i, (key, value)) in self.inner.iter().enumerate() {
86            if i > 0 {
87                write!(f, ", ")?;
88            }
89            write!(f, "{}: {}", key, value)?;
90        }
91        write!(f, "}}")
92    }
93}
94
95impl<'text> fmt::Display for Vdf<'text> {
96    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
97        write!(f, "{} {}", self.key(), self.value())
98    }
99}
100
101// ============================================================================
102// IntoIterator implementations for Obj
103// ============================================================================
104
105impl<'text> IntoIterator for Obj<'text> {
106    type Item = (Key<'text>, Value<'text>);
107    type IntoIter = hashbrown::hash_map::IntoIter<Key<'text>, Value<'text>>;
108
109    fn into_iter(self) -> Self::IntoIter {
110        self.inner.into_iter()
111    }
112}
113
114impl<'a, 'text> IntoIterator for &'a Obj<'text> {
115    type Item = (&'a Key<'text>, &'a Value<'text>);
116    type IntoIter = hashbrown::hash_map::Iter<'a, Key<'text>, Value<'text>>;
117
118    fn into_iter(self) -> Self::IntoIter {
119        self.inner.iter()
120    }
121}
122
123// ============================================================================
124// Index implementations for Value and Obj
125// ============================================================================
126
127impl<'text> Index<&str> for Value<'text> {
128    type Output = Value<'text>;
129
130    /// Returns a reference to the value at the given key.
131    ///
132    /// # Panics
133    ///
134    /// Panics if this is not an object or if the key doesn't exist.
135    /// Use `get()` for non-panicking access.
136    fn index(&self, key: &str) -> &Self::Output {
137        self.get(key).expect("key not found in Value")
138    }
139}
140
141impl<'text> Index<&str> for Obj<'text> {
142    type Output = Value<'text>;
143
144    /// Returns a reference to the value at the given key.
145    ///
146    /// # Panics
147    ///
148    /// Panics if the key doesn't exist. Use `get()` for non-panicking access.
149    fn index(&self, key: &str) -> &Self::Output {
150        self.get(key).expect("key not found in Obj")
151    }
152}