keyvalues-parser 0.2.3

A parser/renderer for vdf text
Documentation
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
#![doc = include_str!("../README.md")]
#![allow(unknown_lints)]
#![allow(clippy::result_large_err)]
// TODO: resolve this ^^
#![cfg_attr(docsrs, feature(doc_cfg))]

use std::{
    borrow::Cow,
    collections::{btree_map::IntoIter, BTreeMap},
    fmt,
    ops::{Deref, DerefMut},
};

pub mod error;
#[cfg(feature = "serde")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
mod serde;
pub mod text;

/// `pest` re-exported for your convenience :)
pub use pest;

/// Parse a KeyValues document to a loosely typed representation
///
/// This is shorthand for parsing a document with default settings aka `Parser::new().parse(text)`
pub fn parse<'text>(text: &'text str) -> error::Result<PartialVdf<'text>> {
    Parser::new().parse(text)
}

/// A configurable KeyValues parser allowing for adjusting settings before parsing
#[derive(Clone, Debug, Default)]
pub struct Parser {
    literal_special_chars: bool,
}

impl Parser {
    /// Constructs a default parser
    ///
    /// Currently this consists of:
    ///
    /// | Toggle | Description |
    /// | :---: | :--- |
    /// | [`Parser::literal_special_chars()`] | Whether to interpret `\` in strings as the start of an escaped special character, or a literal `\` |
    pub const fn new() -> Self {
        // same as Default, but const 😏
        Self {
            literal_special_chars: false,
        }
    }

    /// Toggle how to interpret `\` in strings
    ///
    /// By default (`false`) the parser will interpret backslashes (`\`) in strings as the start of
    /// an escaped special character (e.g. `\\` -> `\`, `\"` -> `"`). When `true` the parser will
    /// instead interpret backslashes (`\`) as a literal backslash. Commonly seen with
    /// windows-paths, for instance
    pub const fn literal_special_chars(mut self, yes: bool) -> Self {
        self.literal_special_chars = yes;
        self
    }

    /// Parse a KeyValues document to a loosely typed representation
    ///
    /// # Example
    ///
    /// ```
    /// use keyvalues_parser::Parser;
    /// let vdf = Parser::new()
    ///     .literal_special_chars(true)
    ///     .parse(r"InstallDir C:\You\Later")
    ///     .unwrap();
    /// assert_eq!(vdf.value.unwrap_str(), r"C:\You\Later");
    /// ```
    pub fn parse<'text>(&self, vdf: &'text str) -> error::Result<PartialVdf<'text>> {
        if self.literal_special_chars {
            #[expect(deprecated)] // deprecated for thee, but not for me!
            text::parse::raw_parse(vdf)
        } else {
            #[expect(deprecated)] // deprecated for thee, but not for me!
            text::parse::escaped_parse(vdf)
        }
    }
}

/// A Key is simply an alias for `Cow<str>`
pub type Key<'text> = Cow<'text, str>;

/// A loosely typed representation of VDF text
///
/// `Vdf` is represented as a single [`Key`] mapped to a single [`Value`]
///
/// ## Parse
///
/// `Vdf`s will generally be created through the use of [`parse()`] or [`Parser::parse()`] which
/// takes a string representing VDF text and attempts to parse it to a `Vdf` representation.
///
/// ## Mutate
///
/// From there you can manipulate/extract from the representation as desired by using the standard
/// conventions on the internal types (plain old `BTreeMap`s, `Vec`s, and `Cow`s all the way down)
///
/// ## Render
///
/// The `Vdf` can also be rendered back to its text form through its `Display` implementation
///
/// ## Example
///
/// ```
/// // Parse
/// let vdf_text = r#"
/// "Outer Key"
/// {
///     "Inner Key" "Inner Value"
///     "Inner Key"
///     {
///     }
/// }
/// "#;
/// let mut parsed = keyvalues_parser::parse(vdf_text)?;
///
/// // Mutate: i.e. remove the last "Inner Key" pair
/// parsed
///     .value
///     .get_mut_obj()
///     .unwrap()
///     .get_mut("Inner Key")
///     .unwrap()
///     .pop();
///
/// // Render: prints
/// // "Outer Key"
/// // {
/// //     "Inner Key" "Inner Value"
/// // }
/// println!("{}", parsed);
/// # Ok::<(), keyvalues_parser::error::Error>(())
/// ```
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Vdf<'text> {
    pub key: Key<'text>,
    pub value: Value<'text>,
}

impl<'text> Vdf<'text> {
    /// Creates a [`Vdf`] using a provided key and value
    ///
    /// ```
    /// use keyvalues_parser::{Vdf, Value};
    /// use std::borrow::Cow;
    ///
    /// let vdf = Vdf::new(Cow::from("Much Key"), Value::Str(Cow::from("Such Wow")));
    /// // prints
    /// // "Much Key"  "Such Wow"
    /// println!("{}", vdf);
    /// ```
    pub fn new(key: Key<'text>, value: Value<'text>) -> Self {
        Self { key, value }
    }
}

impl<'text> From<PartialVdf<'text>> for Vdf<'text> {
    fn from(partial: PartialVdf<'text>) -> Self {
        Self {
            key: partial.key,
            value: partial.value,
        }
    }
}

// TODO: Just store a `Vdf` internally?
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct PartialVdf<'text> {
    pub key: Key<'text>,
    pub value: Value<'text>,
    pub bases: Vec<Cow<'text, str>>,
}

// TODO: why is this type alias a thing if it's not private but the usage of it inside `Obj` is?
type ObjInner<'text> = BTreeMap<Key<'text>, Vec<Value<'text>>>;
type ObjInnerPair<'text> = (Key<'text>, Vec<Value<'text>>);

#[derive(Clone, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Obj<'text>(pub ObjInner<'text>);

/// A slightly customized multi-map debug impl
///
/// Two slight tweaks:
///
/// 1. Just show the inner map instead with no `Obj()` wrapping
/// 2. We're a multi-map where most sequences only have one value. Omit the `[]` in single-value
///    sequences
impl fmt::Debug for Obj<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut map_f = f.debug_map();
        for (k, v) in &self.0 {
            map_f.key(k);
            match v.as_slice() {
                [single] => _ = map_f.value(single),
                _ => _ = map_f.value(v),
            }
        }
        map_f.finish()
    }
}

impl<'text> Obj<'text> {
    /// Creates an empty object value
    ///
    /// Internally This is just a [`BTreeMap`] that maps [`Key`]s to a [`Vec`] of [`Value`]s
    ///
    /// ```
    /// # use keyvalues_parser::{Obj, Value};
    /// # use std::borrow::Cow;
    /// let mut obj = Obj::new();
    /// obj.insert(
    ///     Cow::from("key"),
    ///     vec![]
    /// );
    /// obj.insert(
    ///     Cow::from("earlier key"),
    ///     vec![Value::Obj(Obj::default())]
    /// );
    ///
    /// // It's a b-tree map so the entries are sorted by keys
    /// assert_eq!(
    ///     obj.keys().collect::<Vec<_>>(),
    ///     ["earlier key", "key"]
    /// );
    /// ```
    pub fn new() -> Self {
        Self::default()
    }

    /// Returns the inner [`BTreeMap`]
    ///
    /// ```
    /// # use keyvalues_parser::{Obj, Value};
    /// # use std::{borrow::Cow, collections::BTreeMap};
    /// let mut obj = Obj::new();
    /// obj.insert(Cow::from("much key"), vec![]);
    ///
    /// let inner: BTreeMap<_, _> = obj.into_inner();
    /// // Prints:
    /// // {
    /// //     "much key": [],
    /// // }
    /// println!("{:#?}", inner);
    /// ```
    pub fn into_inner(self) -> ObjInner<'text> {
        self.0
    }

    /// Creates an iterator that returns the [`Vdf`]s that compose the object
    ///
    /// This is notably different compared to just iterating over the `BTreeMap`s items because it
    /// will emit a [`Vdf`] for each key-value pair while the actual items are key-values pairs.
    /// This means that empty values will not emit a [`Vdf`] at all, and a pair that has multiple
    /// entries in values will emit a [`Vdf`] for each pairing
    ///
    /// ```
    /// # use keyvalues_parser::{Obj, Value, Vdf};
    /// # use std::borrow::Cow;
    /// let mut obj = Obj::new();
    /// obj.insert(
    ///     Cow::from("no values"),
    ///     vec![]
    /// );
    /// obj.insert(
    ///     Cow::from("multiple values"),
    ///     vec![Value::Str(Cow::from("first")), Value::Str(Cow::from("second"))]
    /// );
    ///
    /// let vdfs: Vec<_> = obj.into_vdfs().collect();
    /// assert_eq!(
    ///     vdfs,
    ///     [
    ///         Vdf {
    ///             key: Cow::from("multiple values"),
    ///             value: Value::Str(Cow::from("first"))
    ///         },
    ///         Vdf {
    ///             key: Cow::from("multiple values"),
    ///             value: Value::Str(Cow::from("second"))
    ///         },
    ///     ]
    /// );
    /// ```
    pub fn into_vdfs(self) -> IntoVdfs<'text> {
        IntoVdfs::new(self)
    }
}

impl<'text> FromIterator<ObjInnerPair<'text>> for Obj<'text> {
    fn from_iter<T: IntoIterator<Item = ObjInnerPair<'text>>>(iter: T) -> Self {
        let mut inner = BTreeMap::new();
        for (key, values) in iter {
            inner.insert(key, values);
        }

        Self(inner)
    }
}

impl<'text> Deref for Obj<'text> {
    type Target = ObjInner<'text>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for Obj<'_> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

/// An iterator over an [`Obj`]'s [`Vdf`] pairs
///
/// Typically created by calling [`Obj::into_vdfs`] on an existing object
pub struct IntoVdfs<'text> {
    // TODO: can this just store an iterator for the values instead of `.collect()`ing
    current_entry: Option<ObjInnerPair<'text>>,
    it: IntoIter<Key<'text>, Vec<Value<'text>>>,
}

impl<'text> IntoVdfs<'text> {
    fn new(obj: Obj<'text>) -> Self {
        Self {
            current_entry: None,
            it: obj.into_inner().into_iter(),
        }
    }
}

impl<'text> Iterator for IntoVdfs<'text> {
    type Item = Vdf<'text>;

    fn next(&mut self) -> Option<Self::Item> {
        // Iteration will pop the first pair off `current_entry` if it's set and then falls back to
        // reading in a new `current_entry` from `it`. If `it` is exhausted then we're done
        loop {
            match self.current_entry.take() {
                // There is a pair to return
                Some((key, mut values)) if !values.is_empty() => {
                    let value = values.pop().expect("values isn't empty");
                    self.current_entry = Some((key.clone(), values));
                    return Some(Vdf::new(key, value));
                }
                _ => {
                    let (key, values) = self.it.next()?;
                    // Store the next entry. Flip the values so that `pop`ing returns correct order
                    self.current_entry = Some((key, values.into_iter().rev().collect()));
                }
            }
        }
    }
}

// TODO: custom Debug that's more succinct
/// Enum representing all valid VDF values
///
/// VDF is composed of [`Key`]s and their respective [`Value`]s where this represents the latter. A
/// value is either going to be a `Str(Cow<str>)`, or an `Obj(Obj)` that contains a list of keys
/// and values.
///
/// ```
/// # use keyvalues_parser::{Obj, Value};
/// # use std::borrow::Cow;
/// let value_str = Value::Str(Cow::from("some text"));
/// let value_obj = Value::Obj(Obj::new());
/// ```
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum Value<'text> {
    Str(Cow<'text, str>),
    Obj(Obj<'text>),
}

impl fmt::Debug for Value<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Str(s) => {
                f.write_str("Str(")?;
                fmt::Debug::fmt(s, f)?;
                f.write_str(")")
            }
            Self::Obj(o) => {
                f.write_str("Obj(")?;
                fmt::Debug::fmt(o, f)?;
                f.write_str(")")
            }
        }
    }
}

impl<'text> Value<'text> {
    /// Returns if the current value is the `Str` variant
    ///
    /// ```
    /// use std::borrow::Cow;
    /// use keyvalues_parser::{Obj, Value};
    ///
    /// let value_str = Value::Str(Cow::default());
    /// assert!(value_str.is_str());
    /// ```
    pub fn is_str(&self) -> bool {
        self.get_str().is_some()
    }

    /// Returns if the current value is the `Obj` variant
    ///
    /// ```
    /// use keyvalues_parser::{Obj, Value};
    ///
    /// let value_obj = Value::Obj(Obj::default());
    /// assert!(value_obj.is_obj());
    /// ```
    pub fn is_obj(&self) -> bool {
        self.get_obj().is_some()
    }

    /// Gets the inner `&str` if this is a `Value::Str`
    ///
    /// ```
    /// # use keyvalues_parser::Value;
    /// # use std::borrow::Cow;
    /// let value = Value::Str(Cow::from("some text"));
    ///
    /// if let Some(s) = value.get_str() {
    ///     println!("value str: {}", s);
    /// }
    /// ```
    pub fn get_str(&self) -> Option<&str> {
        if let Self::Str(s) = self {
            Some(s)
        } else {
            None
        }
    }

    /// Gets the inner `&Obj` if this value is a `Value::Obj`
    ///
    /// ```
    /// # use keyvalues_parser::{Obj, Value};
    /// let value = Value::Obj(Obj::new());
    ///
    /// if let Some(obj) = value.get_obj() {
    ///     println!("value obj: {:?}", obj);
    /// }
    /// ```
    pub fn get_obj(&self) -> Option<&Obj<'_>> {
        if let Self::Obj(obj) = self {
            Some(obj)
        } else {
            None
        }
    }

    /// Gets the inner `&mut str` if this is a `Value::Str`
    ///
    /// ```
    /// # use keyvalues_parser::Value;
    /// # use std::borrow::Cow;
    /// let mut value = Value::Str(Cow::from("some text"));
    /// let mut inner_str = value.get_mut_str().unwrap();
    /// inner_str.to_mut().make_ascii_uppercase();
    ///
    /// assert_eq!(
    ///     value,
    ///     Value::Str(Cow::from("SOME TEXT"))
    /// );
    /// ```
    pub fn get_mut_str(&mut self) -> Option<&mut Cow<'text, str>> {
        if let Self::Str(s) = self {
            Some(s)
        } else {
            None
        }
    }

    /// Gets the inner `&mut Obj` if this is a `Value::Obj`
    ///
    /// ```
    /// # use keyvalues_parser::{Obj, Value};
    /// # use std::borrow::Cow;
    /// let mut value = Value::Obj(Obj::new());
    /// let mut inner_obj = value.get_mut_obj().unwrap();
    /// inner_obj.insert(Cow::from("new key"), vec![]);
    ///
    /// // Prints:
    /// // Value::Obj({
    /// //    "new key": [],
    /// // })
    /// println!("{:?}", value);
    /// ```
    pub fn get_mut_obj(&mut self) -> Option<&mut Obj<'text>> {
        if let Self::Obj(obj) = self {
            Some(obj)
        } else {
            None
        }
    }

    /// Unwraps the `Cow<str>` from the `Value::Str`
    ///
    /// # Panics
    ///
    /// If the variant was `Value::Obj`
    ///
    /// # Examples
    ///
    /// ```
    /// use keyvalues_parser::Value;
    /// use std::borrow::Cow;
    ///
    /// let value = Value::Str(Cow::from("Sample text"));
    /// assert_eq!(value.unwrap_str(), "Sample text");
    /// ```
    ///
    /// ```should_panic
    /// use keyvalues_parser::{Value, Obj};
    ///
    /// let value = Value::Obj(Obj::new());
    /// value.unwrap_str(); // <-- panics
    /// ```
    pub fn unwrap_str(self) -> Cow<'text, str> {
        self.expect_str("Called `unwrap_str` on a `Value::Obj` variant")
    }

    /// Unwraps the [`Obj`] from the `Value::Obj`
    ///
    /// # Panics
    ///
    /// If the variant was `Value::Str`
    ///
    /// # Examples
    ///
    /// ```
    /// use keyvalues_parser::{Obj, Value};
    ///
    /// let value = Value::Obj(Obj::new());
    /// assert_eq!(value.unwrap_obj(), Obj::new());
    /// ```
    ///
    /// ```should_panic
    /// use keyvalues_parser::Value;
    /// use std::borrow::Cow;
    ///
    /// let value = Value::Str(Cow::from("D'Oh"));
    /// value.unwrap_obj(); // <-- panics
    /// ```
    pub fn unwrap_obj(self) -> Obj<'text> {
        self.expect_obj("Called `unwrap_obj` on a `Value::Str` variant")
    }

    /// Refer to [Value::unwrap_str]. Same situation, but with a custom message
    pub fn expect_str(self, msg: &str) -> Cow<'text, str> {
        if let Self::Str(s) = self {
            s
        } else {
            panic!("{}", msg)
        }
    }

    /// Refer to [Value::unwrap_obj]. Same situation, but with a custom message
    pub fn expect_obj(self, msg: &str) -> Obj<'text> {
        if let Self::Obj(obj) = self {
            obj
        } else {
            panic!("{}", msg)
        }
    }
}