sq/
toml_edit_tree.rs

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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
//! Provides a homogeneous interface to toml_edit's various types, and
//! a traversal interface.
//!
//! toml_edit provides an inhomogeneous interface through `Item`,
//! `Table`, `Value`, `Array`, and various others, which makes
//! traversing the document tree and working with the nodes very
//! difficult.
//!
//! This module introduces a trait providing a homogeneous abstraction
//! over the various types.  Then, it provides a convenient traversal
//! interface.

use std::{
    borrow::Cow,
    fmt,
};

use toml_edit::{
    Array,
    Item,
    Table,
    Value,
};

/// Represents a path in a document tree.
#[derive(Debug, Clone)]
pub struct Path {
    components: Vec<PathComponent>,
}

impl fmt::Display for Path {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for (i, c) in self.components.iter().enumerate() {
            if i > 0 {
                f.write_str(".")?;
            }

            write!(f, "{}", c)?;
        }

        Ok(())
    }
}

impl std::str::FromStr for Path {
    type Err = PathError;

    fn from_str(s: &str) -> PathResult<Self> {
        Ok(Path {
            components: s.split(".").map(PathComponent::from_str)
                .collect::<PathResult<Vec<_>>>()?,
        })
    }
}

impl Path {
    /// Returns the number of path components.
    pub fn len(&self) -> usize {
        self.components.len()
    }

    /// Returns whether the path is empty.
    pub fn is_empty(&self) -> bool {
        self.components.is_empty()
    }

    /// Returns an empty path.
    pub fn empty() -> Path {
        Path {
            components: Vec::new(),
        }
    }

    /// Returns the `idx`th path component, if any.
    pub fn get(&self, idx: usize) -> Option<&PathComponent> {
        self.components.get(idx)
    }

    /// Iterates over the path's components.
    pub fn iter(&self) -> impl Iterator<Item = &PathComponent> {
        self.components.iter()
    }

    /// Appends a path component.
    pub fn push(&mut self, component: PathComponent) {
        self.components.push(component);
    }

    /// Removes and returns the last path component, if any.
    pub fn pop(&mut self) -> Option<PathComponent> {
        self.components.pop()
    }
}

/// A path component.
#[derive(Debug, Clone)]
pub enum PathComponent {
    /// A key name in a map.
    Symbol(String),

    /// An index into an array.
    Index(usize),
}

impl fmt::Display for PathComponent {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            PathComponent::Symbol(s) => write!(f, "{}", s),
            PathComponent::Index(i) =>  write!(f, "{}", i),
        }
    }
}

impl From<usize> for PathComponent {
    fn from(v: usize) -> Self {
        PathComponent::Index(v)
    }
}

impl From<&str> for PathComponent {
    fn from(v: &str) -> Self {
        PathComponent::Symbol(v.into())
    }
}

impl std::str::FromStr for PathComponent {
    type Err = PathError;

    fn from_str(s: &str) -> PathResult<Self> {
        if s.contains('.') {
            return Err(PathError::InvalidPathComponent(
                "must not contain a period (\".\")"));
        }

        if let Ok(i) = s.parse::<usize>() {
            Ok(PathComponent::Index(i))
        } else {
            Ok(PathComponent::Symbol(s.into()))
        }
    }
}

impl PathComponent {
    /// Returns an symbolic key, or returns an error.
    pub fn as_symbol(&self) -> Result<&str> {
        match self {
            PathComponent::Symbol(s) => Ok(s),
            PathComponent::Index(i) => Err(Error::BadSymbol(*i)),
        }
    }

    /// Returns an index, or returns an error.
    pub fn as_index(&self) -> Result<usize> {
        match self {
            PathComponent::Symbol(s) => Err(Error::BadIndex(s.to_string())),
            PathComponent::Index(i) => Ok(*i),
        }
    }
}

/// Result specialization for conversions to path components.
pub type PathResult<T> = std::result::Result<T, PathError>;

/// Errors converting to path components.
#[derive(thiserror::Error, Debug)]
pub enum PathError {
    #[error("invalid path component: {0}")]
    InvalidPathComponent(&'static str),
}

/// A unified interface to `toml_edit`.
pub trait Node: fmt::Debug {
    /// Returns the node's type name.
    fn type_name(&self) -> &'static str;

    /// Returns the node as array, if possible.
    fn as_array(&self) -> Option<&Array>;

    /// Returns the node as mutable array, if possible.
    fn as_array_mut(&mut self) -> Option<&mut Array>;

    /// Returns the node as atomic value, if possible.
    fn as_atomic_value(&self) -> Option<&Value>;

    /// Returns the node as mutable table, if possible.
    fn as_table_mut(&mut self) -> Option<&mut Table>;

    /// Returns a reference to the child denoted by `key`.
    fn get(&self, key: &PathComponent) -> Result<&dyn Node>;

    /// Returns a mutable reference to the child denoted by `key`.
    fn get_mut(&mut self, key: &PathComponent) -> Result<&mut dyn Node>;

    /// Sets the child denoted by `key` to the given value.
    fn set(&mut self, key: &PathComponent, value: Value) -> Result<()>;

    /// Removes the child denoted by `key`.
    fn remove(&mut self, key: &PathComponent) -> Result<()>;

    /// Iterates over the children of this node.
    fn iter<'i>(&'i self) -> Box<(dyn Iterator<Item = (PathComponent, &'i dyn Node)> + 'i)>;

    /// Returns a reference to the node that is reached by traversing
    /// `path` from the current node.
    fn traverse(&self, path: &Path) -> TraversalResult<&dyn Node>
    where
        Self: Sized,
    {
        let mut node: &dyn Node = self as _;
        for (i, pc) in path.iter().cloned().enumerate() {
            let type_name = node.type_name();
            node = node.get(&pc)
                .map_err(|e| e.with_context(path, i, type_name))?;
        }

        Ok(node)
    }

    /// Returns a mutable reference to the node that is reached by
    /// traversing `path` from the current node.
    fn traverse_mut(&mut self, path: &Path) -> TraversalResult<&mut dyn Node>
    where
        Self: Sized,
    {
        let mut node: &mut dyn Node = self as _;
        for (i, pc) in path.iter().cloned().enumerate() {
            let type_name = node.type_name();
            node = node.get_mut(&pc)
                .map_err(|e| e.with_context(path, i, type_name))?;
        }

        Ok(node)
    }
}

impl Node for Item {
    fn type_name(&self) -> &'static str {
        self.type_name()
    }

    fn as_array(&self) -> Option<&Array> {
        match self {
            Item::Value(v) => v.as_array(),
            | Item::None
                | Item::Table(_)
                | Item::ArrayOfTables(_) => None,
        }
    }

    fn as_array_mut(&mut self) -> Option<&mut Array> {
        match self {
            Item::Value(v) => v.as_array_mut(),
            | Item::None
                | Item::Table(_)
                | Item::ArrayOfTables(_) => None,
        }
    }

    fn as_atomic_value(&self) -> Option<&Value> {
        match self {
            Item::Value(v) => v.as_atomic_value(),
            | Item::None
                | Item::Table(_)
                | Item::ArrayOfTables(_) => None,
        }
    }

    fn as_table_mut(&mut self) -> Option<&mut Table> {
        match self {
            Item::Table(t) => Some(t),
            | Item::None
                | Item::Value(_)
                | Item::ArrayOfTables(_) => None,
        }
    }

    fn get(&self, key: &PathComponent) -> Result<&dyn Node> {
        match self {
            Item::None => Err(Error::LookupError("none")),
            Item::Value(v) => v.get(key),
            Item::Table(t) => Node::get(t, key),
            Item::ArrayOfTables(a) => {
                let i = key.as_index()?;
                Ok(a.get(i).ok_or_else(|| Error::OutOfBounds(i, a.len()))?)
            },
        }
    }

    fn get_mut(&mut self, key: &PathComponent) -> Result<&mut dyn Node> {
        match self {
            Item::None => Err(Error::LookupError("none")),
            Item::Value(v) => v.get_mut(key),
            Item::Table(t) => Node::get_mut(t, key),
            Item::ArrayOfTables(a) => {
                let i = key.as_index()?;
                let l = a.len();
                Ok(a.get_mut(i).ok_or_else(|| Error::OutOfBounds(i, l))?)
            },
        }
    }

    fn set(&mut self, key: &PathComponent, value: Value) -> Result<()> {
        match self {
            Item::None => Err(Error::InsertionError("none")),
            Item::Value(v) => v.set(key, value),
            Item::Table(t) => {
                t.insert(key.as_symbol()?, Item::Value(value));
                Ok(())
            },
            Item::ArrayOfTables(_) => {
                // XXX: ArrayOfTabels::insert does not exist.
                Err(Error::InsertionError(self.type_name()))
            },
        }
    }

    fn remove(&mut self, key: &PathComponent) -> Result<()> {
        match self {
            Item::None => Err(Error::RemovalError("none")),
            Item::Value(v) => v.remove(key),
            Item::Table(t) => {
                let s = key.as_symbol()?;
                t.remove(s).ok_or(Error::KeyNotFound(s.into()))?;
                Ok(())
            },
            Item::ArrayOfTables(a) => {
                let i = key.as_index()?;
                let l = a.len();
                if i >= l {
                    return Err(Error::OutOfBounds(i, l));
                }
                a.remove(i);
                Ok(())
            },
        }
    }

    fn iter<'i>(&'i self) -> Box<(dyn Iterator<Item = (PathComponent, &'i dyn Node)> + 'i)> {
        match self {
            Item::None => Box::new(std::iter::empty()),
            Item::Value(v) => v.iter(),
            Item::Table(t) => Node::iter(t),
            Item::ArrayOfTables(a) =>
                Box::new(a.iter().enumerate().map(|(k, v)| (k.into(), &*v as &dyn Node))),
        }
    }
}

impl Node for Table {
    fn type_name(&self) -> &'static str {
        "table"
    }

    fn as_array(&self) -> Option<&Array> {
        None
    }

    fn as_array_mut(&mut self) -> Option<&mut Array> {
        None
    }

    fn as_atomic_value(&self) -> Option<&Value> {
        None
    }

    fn as_table_mut(&mut self) -> Option<&mut Table> {
        Some(self)
    }

    fn get(&self, key: &PathComponent) -> Result<&dyn Node> {
        let s = key.as_symbol()?;
        Ok(self.get(s).ok_or_else(|| Error::KeyNotFound(s.into()))?)
    }

    fn get_mut(&mut self, key: &PathComponent) -> Result<&mut dyn Node> {
        let s = key.as_symbol()?;
        Ok(self.get_mut(s).ok_or_else(|| Error::KeyNotFound(s.into()))?)
    }

    fn set(&mut self, key: &PathComponent, value: Value) -> Result<()> {
        let s = key.as_symbol()?;
        self.insert(s, Item::Value(value));
        Ok(())
    }

    fn remove(&mut self, key: &PathComponent) -> Result<()> {
        let s = key.as_symbol()?;
        self.remove(s).ok_or(Error::KeyNotFound(s.into()))?;
        Ok(())
    }

    fn iter<'i>(&'i self) -> Box<(dyn Iterator<Item = (PathComponent, &'i dyn Node)> + 'i)> {
        Box::new(self.iter().map(|(k, v)| (k.into(), &*v as &dyn Node)))
    }
}

impl Node for Value {
    fn type_name(&self) -> &'static str {
        self.type_name()
    }

    fn as_array(&self) -> Option<&Array> {
        match self {
            Value::Array(a) => Some(a),
            | Value::String(_)
                | Value::Integer(_)
                | Value::Float(_)
                | Value::Boolean(_)
                | Value::Datetime(_)
                | Value::InlineTable(_) =>
                None,
        }
    }

    fn as_array_mut(&mut self) -> Option<&mut Array> {
        match self {
            Value::Array(a) => Some(a),
            | Value::String(_)
                | Value::Integer(_)
                | Value::Float(_)
                | Value::Boolean(_)
                | Value::Datetime(_)
                | Value::InlineTable(_) =>
                None,
        }
    }

    fn as_atomic_value(&self) -> Option<&Value> {
        match self {
            | Value::String(_)
                | Value::Integer(_)
                | Value::Float(_)
                | Value::Boolean(_)
                | Value::Datetime(_) =>
                Some(self),
            | Value::Array(_)
                | Value::InlineTable(_) =>
                None,
        }
    }

    fn as_table_mut(&mut self) -> Option<&mut Table> {
        None
    }

    fn get(&self, key: &PathComponent) -> Result<&dyn Node> {
        match self {
            | Value::String(_)
                | Value::Integer(_)
                | Value::Float(_)
                | Value::Boolean(_)
                | Value::Datetime(_) =>
                Err(Error::LookupError(self.type_name())),
            Value::Array(a) => {
                let i = key.as_index()?;
                Ok(a.get(i).ok_or_else(|| Error::OutOfBounds(i, a.len()))?)
            },
            Value::InlineTable(t) => {
                let s = key.as_symbol()?;
                Ok(t.get(s).ok_or_else(|| Error::KeyNotFound(s.into()))?)
            },
        }
    }

    fn get_mut(&mut self, key: &PathComponent) -> Result<&mut dyn Node> {
        match self {
            | Value::String(_)
                | Value::Integer(_)
                | Value::Float(_)
                | Value::Boolean(_)
                | Value::Datetime(_) =>
                Err(Error::LookupError(self.type_name())),
            Value::Array(a) => {
                let i = key.as_index()?;
                let l = a.len();
                Ok(a.get_mut(i).ok_or_else(|| Error::OutOfBounds(i, l))?)
            },
            Value::InlineTable(t) => {
                let s = key.as_symbol()?;
                Ok(t.get_mut(s).ok_or_else(|| Error::KeyNotFound(s.into()))?)
            },
        }
    }

    fn set(&mut self, key: &PathComponent, value: Value) -> Result<()> {
        match self {
            | Value::String(_)
                | Value::Integer(_)
                | Value::Float(_)
                | Value::Boolean(_)
                | Value::Datetime(_) =>
                Err(Error::InsertionError(self.type_name())),
            Value::Array(a) => {
                let i = key.as_index()?;
                let l = a.len();
                if i >= l {
                    return Err(Error::OutOfBounds(i, l));
                }
                a.replace(i, value);
                Ok(())
            },
            Value::InlineTable(t) => {
                t.insert(key.as_symbol()?, value);
                Ok(())
            },
        }
    }

    fn remove(&mut self, key: &PathComponent) -> Result<()> {
        match self {
            | Value::String(_)
                | Value::Integer(_)
                | Value::Float(_)
                | Value::Boolean(_)
                | Value::Datetime(_) =>
                Err(Error::RemovalError(self.type_name())),
            Value::Array(a) => {
                let i = key.as_index()?;
                let l = a.len();
                if i >= l {
                    return Err(Error::OutOfBounds(i, l));
                }
                a.remove(i);
                Ok(())
            },
            Value::InlineTable(t) => {
                let s = key.as_symbol()?;
                t.remove(s).ok_or(Error::KeyNotFound(s.into()))?;
                Ok(())
            },
        }
    }

    fn iter<'i>(&'i self) -> Box<(dyn Iterator<Item = (PathComponent, &'i dyn Node)> + 'i)> {
        match self {
            | Value::String(_)
                | Value::Integer(_)
                | Value::Float(_)
                | Value::Boolean(_)
                | Value::Datetime(_) =>
                Box::new(std::iter::empty()),
            Value::Array(a) =>
                Box::new(a.iter().enumerate().map(|(k, v)| (k.into(), &*v as &dyn Node))),
            Value::InlineTable(t) =>
                Box::new(t.iter().map(|(k, v)| (k.into(), &*v as &dyn Node))),
        }
    }
}

/// Result specialization for this module.
pub type Result<T> = std::result::Result<T, Error>;

/// Errors for this module.
#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[error("key {0:?} not found")]
    KeyNotFound(String),

    #[error("index {0} is out of bounds")]
    OutOfBounds(usize, usize),

    #[error("cannot index into a {0}")]
    LookupError(&'static str),

    #[error("cannot insert into a {0}")]
    InsertionError(&'static str),

    #[error("cannot remove items from a {0}")]
    RemovalError(&'static str),

    #[error("{0:?} is not a numeric index")]
    BadIndex(String),

    #[error("{0:?} is not a valid symbol")]
    BadSymbol(usize),
}

impl Error {
    /// Adds context to the error, yielding a [`TraversalError`].
    pub fn with_context(self, path: &Path, i: usize, type_name: &'static str)
                        -> TraversalError
    {
        let p = Path {
            components: path.components[..i].iter().cloned().collect(),
        };
        match self {
            Error::KeyNotFound(k) => TraversalError::KeyNotFound(p, k),
            Error::OutOfBounds(i, l) => TraversalError::OutOfBounds(p, i, l),
            Error::LookupError(t) => match &path.components[i] {
                PathComponent::Symbol(s) =>
                    TraversalError::KeyLookupBadType(p, s.into(), t),
                PathComponent::Index(i) =>
                    TraversalError::IndexLookupBadType(p, *i, t)
            },
            Error::BadIndex(s) =>
                TraversalError::KeyLookupBadType(p, s, type_name),
            Error::BadSymbol(i) =>
                TraversalError::IndexLookupBadType(p, i, type_name),
            Error::InsertionError(_) | Error::RemovalError(_) =>
                unreachable!("not applicable for traversals"),
        }
    }
}

/// Result specialization for traversal errors.
pub type TraversalResult<T> = std::result::Result<T, TraversalError>;

/// Errors traversing the document tree.
#[derive(thiserror::Error, Debug)]
pub enum TraversalError {
    #[error("Tried to look up {1:?}{path}, \
             but it does not exist",
            path=Self::fmt_path("in", &.0))]
    KeyNotFound(Path, String),

    #[error("Tried to get the item at index {1}{path}, \
             but there are only {2} items",
            path=Self::fmt_path("from", &.0))]
    OutOfBounds(Path, usize, usize),

    #[error("Tried to look up {1:?}{path}, \
             but the latter is a {2}, not a table",
            path=Self::fmt_path("in", &.0))]
    KeyLookupBadType(Path, String, &'static str),

    #[error("Tried to get the item at index {1}{path}, \
             but the latter is a {2}, not an array",
            path=Self::fmt_path("from", &.0))]
    IndexLookupBadType(Path, usize, &'static str),
}

impl TraversalError {
    /// Formats a position in the document tree for use in error
    /// messages.
    fn fmt_path(preposition: &'static str, p: &Path) -> Cow<'static, str> {
        if p.is_empty() {
            "".into()
        } else {
            format!(" {} {}", preposition, p).into()
        }
    }
}