1#![no_std]
31#![warn(missing_docs)]
32
33extern crate alloc;
34
35use alloc::borrow::Cow;
36use alloc::string::ToString;
37
38pub mod binary;
39pub mod error;
40pub mod text;
41pub mod value;
42
43pub use error::{Error, Result};
44pub use value::{Obj, Value, Vdf};
45
46pub use text::parse_text;
48
49pub fn parse_binary(input: &[u8]) -> Result<Vdf<'_>> {
54 binary::parse(input)
55}
56
57pub fn parse_shortcuts(input: &[u8]) -> Result<Vdf<'_>> {
62 binary::parse_shortcuts(input)
63}
64
65pub fn parse_appinfo(input: &[u8]) -> Result<Vdf<'_>> {
70 binary::parse_appinfo(input)
71}
72
73pub fn parse_packageinfo(input: &[u8]) -> Result<Vdf<'_>> {
78 binary::parse_packageinfo(input)
79}
80
81impl Vdf<'_> {
83 pub fn into_owned(self) -> Vdf<'static> {
88 let (key, value) = self.into_parts();
89 let owned_key: Cow<'static, str> = match key {
90 Cow::Borrowed(s) => Cow::Owned(s.to_string()),
91 Cow::Owned(s) => Cow::Owned(s),
92 };
93 Vdf::new(owned_key, value.into_owned())
94 }
95}
96
97impl Value<'_> {
98 pub fn into_owned(self) -> Value<'static> {
100 match self {
101 Value::Str(s) => Value::Str(match s {
102 Cow::Borrowed(b) => b.to_string().into(),
103 Cow::Owned(o) => o.into(),
104 }),
105 Value::Obj(obj) => Value::Obj(obj.into_owned()),
106 Value::I32(n) => Value::I32(n),
107 Value::U64(n) => Value::U64(n),
108 Value::Float(n) => Value::Float(n),
109 Value::Pointer(n) => Value::Pointer(n),
110 Value::Color(c) => Value::Color(c),
111 }
112 }
113}
114
115impl Obj<'_> {
116 pub fn into_owned(self) -> Obj<'static> {
118 let mut new = Obj::new();
119 for (k, v) in self.iter() {
120 let owned_key: Cow<'static, str> = match k {
121 Cow::Borrowed(b) => Cow::Owned(b.to_string()),
122 Cow::Owned(o) => Cow::Owned(o.clone()),
123 };
124 new.insert(owned_key, v.clone().into_owned());
126 }
127 new
128 }
129}
130
131#[cfg(test)]
132mod tests {
133 use super::*;
134 use alloc::vec;
135
136 const SHORTCUTS_VDF: &[u8] = &[
138 0x00, b't', b'e', b's', b't', 0x00, 0x01, b'k', b'e', b'y', 0x00, b'v', b'a', b'l', b'u', b'e', 0x00, 0x08, ];
145
146 #[test]
147 fn test_parse_binary() {
148 let vdf = parse_binary(SHORTCUTS_VDF).unwrap();
149 assert!(vdf.as_obj().is_some());
150 assert_eq!(vdf.key(), "root");
151 let obj = vdf.as_obj().unwrap();
152 let test_obj = obj.get("test").and_then(|v| v.as_obj()).unwrap();
153 assert_eq!(test_obj.get("key").and_then(|v| v.as_str()), Some("value"));
154 }
155
156 #[test]
157 fn test_parse_shortcuts() {
158 let vdf = parse_shortcuts(SHORTCUTS_VDF).unwrap();
159 assert!(vdf.as_obj().is_some());
160 assert_eq!(vdf.key(), "root");
161 let obj = vdf.as_obj().unwrap();
162 let test_obj = obj.get("test").and_then(|v| v.as_obj()).unwrap();
163 assert_eq!(test_obj.get("key").and_then(|v| v.as_str()), Some("value"));
164 }
165
166 #[test]
167 fn test_parse_appinfo() {
168 let mut input = vec![
171 0x28, 0x44, 0x56, 0x07, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, ];
175 input.resize(76, 0);
177 let result = parse_appinfo(&input);
178 if let Err(e) = &result {
179 panic!("parse_appinfo failed: {:?}", e);
180 }
181 assert!(result.is_ok());
182 let vdf = result.unwrap();
183 assert!(vdf.key().starts_with("appinfo_universe_"));
184 }
185
186 #[test]
187 fn test_into_owned_vdf() {
188 let input = r#""root"
189 {
190 "key" "value"
191 }"#;
192 let borrowed = parse_text(input).unwrap();
193 let owned = borrowed.into_owned();
194 assert_eq!(owned.key(), "root");
195 }
196
197 #[test]
198 fn test_into_owned_value_str() {
199 let borrowed = Value::Str("test".into());
200 let owned = borrowed.into_owned();
201 assert!(matches!(owned, Value::Str(Cow::Owned(_))));
202 }
203
204 #[test]
205 fn test_into_owned_value_obj() {
206 let mut obj = Obj::new();
207 obj.insert("key", Value::Str("value".into()));
208 let borrowed = Value::Obj(obj);
209 let owned = borrowed.into_owned();
210 assert!(matches!(owned, Value::Obj(_)));
211 }
212
213 #[test]
214 fn test_into_owned_obj() {
215 let mut obj = Obj::new();
216 obj.insert("key", Value::Str("value".into()));
217 let owned = obj.into_owned();
218 assert!(owned.get("key").is_some());
219 }
220}