figtree 0.2.2

Figtree is a file format for human-useable configuration
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
//! A collection of types that define a figtree document.
//!
//! These types are re-exported in the main module because there aren't too many of
//! them, and because they're useful when testing equality or building figtree documents
//! from scratch.  The `types` module is also made available to allow explicit namespaced
//! imports of these types.
//!
//! # Examples
//! ```
//! # use figtree::types::*;
//!
//! let mut doc = Document::new();
//! let mut node = doc.new_node_or_get("node_name");
//! node.insert_attr(
//!     "key".to_string(),
//!     Value::new_int(4032));
//! ```

use std::collections::HashMap;
use std::collections::hash_map::Iter;
use super::parser::ParsedValue;

/// A type to represent a figtree dict
///
/// Maps string keys to `Value`s.  Can contain any `Value`, including container types
pub type Dict = HashMap<String, Value>;

/// A type to represent a figtree list
///
/// Contains ordered `Value`s.  Can contain any `Value`, including container types
pub type List = Vec<Value>;

/// A type to represent a figtree value
///
/// Generally, this is obtained by getting the value of an attribute on a Node, although
/// there are also methods to construct all the kinds of this type.
///
/// # Examples
/// ```
/// # use figtree::types::Value;
/// let value = Value::new_string("hello!");
/// assert!(value.get_str() == Some("hello!"));
/// assert!(value.get_int() == None);
#[derive(Debug, PartialEq)]
pub enum Value {
    Str(String),
    Int(i64),
    Float(f64),
    Bool(bool),
    Ident(String),
    Dict(Dict),
    List(List),
    Null,
}

impl Value {
    /// Construct a new string `Value`.
    pub fn new_string<S>(s: S) -> Self where S: Into<String> {
        Value::Str(s.into())
    }

    /// Construct a new identifier `Value`.
    pub fn new_ident<S>(s: S) -> Self where S: Into<String> {
        Value::Ident(s.into())
    }

    /// Construct a new integer `Value`.
    pub fn new_int(s: i64) -> Self {
        Value::Int(s)
    }

    /// Construct a new float `Value`.
    pub fn new_float(s: f64) -> Self {
        Value::Float(s)
    }

    /// Construct a new boolean `Value`.
    pub fn new_bool(s: bool) -> Self {
        Value::Bool(s)
    }

    /// Construct a new null `Value`.
    pub fn new_null() -> Self {
        Value::Null
    }

    pub fn from_parsed_value(val: ParsedValue) -> Self {
        match val {
            ParsedValue::Str(s) => Self::new_string(s),
            ParsedValue::Float(f) => Self::new_float(f),
            ParsedValue::Bool(b) => Self::new_bool(b),
            ParsedValue::Int(i) => Self::new_int(i),
            ParsedValue::Ident(i) => Self::new_ident(i),
            ParsedValue::Null => Self::new_null(),
        }
    }

    /// Extract the contained value if it is a string.
    pub fn get_str(&self) -> Option<&str> {
        match *self {
            Value::Str(ref s) => Some(&s),
            _ => None
        }
    }

    /// Extract the contained value if it is an integer
    pub fn get_int(&self) -> Option<i64> {
        match *self {
            Value::Int(s) => Some(s),
            _ => None
        }
    }

    /// Extract the contained value if it is a float
    pub fn get_float(&self) -> Option<f64> {
        match *self {
            Value::Float(s) => Some(s),
            _ => None
        }
    }

    /// Extract the contained value if it is a boolean
    pub fn get_bool(&self) -> Option<bool> {
        match *self {
            Value::Bool(s) => Some(s),
            _ => None
        }
    }

    /// Extract the contained (&str) value if it is an identifier
    pub fn get_ident(&self) -> Option<&str> {
        match *self {
            Value::Ident(ref s) => Some(&s),
            _ => None
        }
    }

    /// Extract the contained value if it is a dict
    pub fn get_dict(&self) -> Option<&Dict> {
        match *self {
            Value::Dict(ref s) => Some(s),
            _ => None
        }
    }

    /// Extract the contained value as a slice if it is a list
    pub fn get_list(&self) -> Option<&[Value]> {
        match *self {
            Value::List(ref s) => Some(&s),
            _ => None
        }
    }

    pub fn is_null(&self) -> bool {
        match *self {
            Value::Null => true,
            _ => false,
        }
    }
}

/// A struct representing an individual node in a parsed document
///
/// # Examples
///
/// Manipulating subnodes:
///
/// ```
/// use figtree::types::*;
/// let mut node = Node::new();
/// assert!(node.is_empty());
/// assert!(!node.has_nodes());
///
/// node.new_node_or_get("subnode");
/// assert!(node.node_count() == 1);
///
/// { // appease the borrow checker
///     let subnode = node.get_node("subnode").expect("no such node");
///     assert!(subnode == &Node::new());  // creates a blank subnode
/// }
///
/// { // appease the borrow checker
///     let mut subnode = node.get_node_mut("subnode").expect("no such node");
///     let sub_subnode = subnode.new_node_or_get("sub_subnode");
///     // etc.
/// }
/// ```
///
/// Manipulating attributes
///
/// ```
/// use figtree::types::*;
/// let mut node = Node::new();
/// assert!(node.is_empty());
/// assert!(!node.has_attrs());
///
/// node.insert_attr("key", Value::new_int(5));
/// assert!(node.attr_count() == 1);
/// ```
#[derive(Debug, PartialEq)]
pub struct Node {
    subnodes: HashMap<String, Node>,
    attributes: HashMap<String, Value>,
}

impl Node {
    /// Construct a new, empty node
    pub fn new() -> Self {
        Node {
            subnodes: HashMap::new(),
            attributes: HashMap::new(),
        }
    }

    /// Construct a new node and automatically insert it as a subnode.
    ///
    /// Returns a mutable reference to the new node.  If there is a subnode already
    /// present with the given name, this method will not insert a new node and instead
    /// just return the old node.
    pub fn new_node_or_get<S>(&mut self, name: S) -> &mut Self where S: Into<String> {
        self.subnodes.entry(name.into()).or_insert(Self::new())
    }

    /// Inserts a node into this node as a subnode.
    ///
    /// If there is already a node with the given name, replace it and return the
    /// old node.
    pub fn insert_node<S>(&mut self, name: S, node: Node) -> Option<Node>
        where S: Into<String> {

        self.subnodes.insert(name.into(), node)
    }

    /// Remove a subnode from this node.
    ///
    /// Returns the deleted node.
    pub fn delete_node<S>(&mut self, name: S) -> Option<Node> where S: Into<String> {
        self.subnodes.remove(&name.into())
    }

    /// Get a reference to the specified subnode
    pub fn get_node<S>(&self, name: S) -> Option<&Self> where S: Into<String> {
        self.subnodes.get(&name.into())
    }

    /// Get a mutable reference to the specified subnode
    pub fn get_node_mut<S>(&mut self, name: S) -> Option<&mut Self>
        where S: Into<String> {

        self.subnodes.get_mut(&name.into())
    }

    /// Get an iterable of (&name, &node) pairs
    pub fn iter_nodes(&self) -> Iter<String, Node> {
        self.subnodes.iter()
    }

    /// Insert a new value into this node.
    ///
    /// If there is already a value with the given name, replace it and return the old
    /// value.
    pub fn insert_attr<S>(&mut self, name: S, value: Value) -> Option<Value>
        where S: Into<String> {

        self.attributes.insert(name.into(), value)
    }


    /// Remove an attribute from this node.
    ///
    /// Returns the deleted value.
    pub fn delete_attr<S>(&mut self, name: S) -> Option<Value> where S: Into<String> {
        self.attributes.remove(&name.into())
    }

    /// Get a reference to the specified attribute value
    pub fn get_attr<S>(&self, name: S) -> Option<&Value> where S: Into<String> {
        self.attributes.get(&name.into())
    }

    /// Get a mutable reference to the specified attribute value
    pub fn get_attr_mut<S>(&mut self, name: S) -> Option<&mut Value>
        where S: Into<String> {

        self.attributes.get_mut(&name.into())
    }

    /// Get an iterable of (&name, &attribute) pairs
    pub fn iter_attrs(&self) -> Iter<String, Value> {
        self.attributes.iter()
    }

    /// Test if this node has no subnodes or attributes
    pub fn is_empty(&self) -> bool {
        self.subnodes.is_empty() && self.attributes.is_empty()
    }

    /// Test if this node has a subnode with the given name.
    pub fn has_node(&self, name: &String) -> bool {
        self.subnodes.contains_key(name)
    }

    /// Test if this node has any subnodes at all.
    pub fn has_nodes(&self) -> bool {
        !self.subnodes.is_empty()
    }

    /// Returns the number of subnodes.
    pub fn node_count(&self) -> usize {
        self.subnodes.len()
    }

    /// Test if this node had an attribute with the given key.
    pub fn has_attr(&self, name: &String) -> bool {
        self.attributes.contains_key(name)
    }

    /// Test if this node has any attributes at all.
    pub fn has_attrs(&self) -> bool {
        !self.attributes.is_empty()
    }

    /// Returns the number of attributes.
    pub fn attr_count(&self) -> usize {
        self.attributes.len()
    }
}

/// A struct representing a parsed figtree document.
///
/// # Examples
///
/// ```
/// use figtree::types::*;
/// let mut doc = Document::new();
/// assert!(doc.is_empty());
///
/// doc.new_node_or_get("node name");
/// assert!(doc.node_count() == 1);
///
/// {   // appease the borrow checker by scoping this off
///     // in real code this would be unnecessary because there is no need to do
///     // something so pathological.
///     let node = doc.get_node("node name").expect("no such node");
///     assert!(node == &Node::new()); // new node is a fresh, blank node
/// }
///
/// {   // again, appease the borrow checker
///     let mut node = doc.get_node_mut("node name").expect("no such node");
///     // node can be modified here
/// }
/// ```
#[derive(Debug, PartialEq)]
pub struct Document {
    nodes: HashMap<String, Node>,
}

impl Document {
    /// Construct a new, empty document
    pub fn new() -> Self {
        Document {
            nodes: HashMap::new(),
        }
    }

    /// Construct a new node and insert it into the document.
    ///
    /// Returns a mutable reference to the new node.  If there is a node already
    /// present with the given name, this method will not insert a new node and instead
    /// just return the old node.
    pub fn new_node_or_get<S>(&mut self, name: S) -> &mut Node where S: Into<String> {
        self.nodes.entry(name.into()).or_insert(Node::new())
    }

    /// Inserts a node into the document.
    ///
    /// If there is already a node with the given name, replace it and return the
    /// old node.
    pub fn insert_node<S>(&mut self, name: S, node: Node) -> Option<Node>
        where S: Into<String> {

        self.nodes.insert(name.into(), node)
    }

    /// Remove a node from the document.
    ///
    /// Returns the deleted node, if it exists.
    pub fn delete_node<S>(&mut self, name: S) -> Option<Node> where S: Into<String> {
        self.nodes.remove(&name.into())
    }

    /// Get a reference to a specified node
    ///
    /// Essentially a thin wrapper around the `Document.nodes` mapping, but it allows for
    /// &str arguments, and allows users to do common operations without having to know
    /// about the internal structure of the node.
    pub fn get_node<S>(&self, name: S) -> Option<&Node> where S: Into<String> {
        self.nodes.get(&name.into())
    }

    /// Get a mutable reference to a specified node
    ///
    /// Essentially a thin wrapper around the `Document.nodes` mapping, but it allows for
    /// &str arguments, and allows users to do common operations without having to know
    /// about the internal structure of the node.
    pub fn get_node_mut<S>(&mut self, name: S) -> Option<&mut Node> where S: Into<String> {
        self.nodes.get_mut(&name.into())
    }

    /// Get an iterable of (&name, &node) pairs
    pub fn iter_nodes(&self) -> Iter<String, Node> {
        self.nodes.iter()
    }

    /// Test if the document is empty - if it has no nodes.
    pub fn is_empty(&self) -> bool {
        self.nodes.is_empty()
    }

    /// Test if the document has a given node.
    pub fn has_node(&self, name: &String) -> bool {
        self.nodes.contains_key(name)
    }

    /// Test if the document has any nodes.
    pub fn has_nodes(&self) -> bool {
        !self.nodes.is_empty()
    }

    /// Returns the number of nodes in the document.
    pub fn node_count(&self) -> usize {
        self.nodes.len()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn type_creations() {
        let doc = Document::new();
        assert_eq!(doc.nodes.len(), 0);

        let node = Node::new();
        assert_eq!(node.subnodes.len(), 0);
        assert_eq!(node.attributes.len(), 0);

        let string = Value::new_string("hello");
        assert_eq!(string, Value::Str("hello".to_string()));
        assert_eq!(string, Value::new_string("hello".to_string()));

        let identifier = Value::new_ident("hello");
        assert_eq!(identifier, Value::Ident("hello".to_string()));
        assert_eq!(identifier, Value::new_ident("hello".to_string()));

        let integer = Value::new_int(34);
        assert_eq!(integer, Value::Int(34));

        let floatval = Value::new_float(33.4);
        assert_eq!(floatval, Value::Float(33.4));

        let boolean = Value::new_bool(false);
        assert_eq!(boolean, Value::Bool(false));
    }

    #[test]
    fn node_with_subnodes() {
        let mut node = Node::new();
        assert!(node.is_empty());
        assert!(!node.has_nodes());
        assert!(!node.has_attrs());
        assert_eq!(node.node_count(), 0);
        assert_eq!(node.attr_count(), 0);

        node.new_node_or_get("subnode_name").new_node_or_get("secondary_subnode");
        assert_eq!(
            node.get_node("subnode_name")
                .expect("couldn't find subnode_name")
                .get_node("secondary_subnode"),
            Some(&Node::new()));
        assert!(!node.is_empty());
        assert!(node.has_nodes());
        assert!(!node.has_attrs());
        assert_eq!(node.node_count(), 1);
        assert_eq!(node.attr_count(), 0);

        let subnode = node.delete_node("subnode_name").expect("node should have existed");
        assert!(node.is_empty());
        assert!(!node.has_nodes());
        assert!(!node.has_attrs());
        assert_eq!(node.node_count(), 0);
        assert_eq!(node.attr_count(), 0);

        node.insert_node("new subnode", subnode);
        assert!(!node.is_empty());
        assert!(node.has_nodes());
        assert!(!node.has_attrs());
        assert_eq!(node.node_count(), 1);
        assert_eq!(node.attr_count(), 0);

        let mut iterable = node
            .get_node("new subnode").expect("missing subnode 'new subnode'")
            .iter_nodes();
        assert_eq!(iterable.len(), 1);
        assert_eq!(
            iterable.next(),
            Some((&("secondary_subnode".to_string()), &Node::new())));
        assert_eq!(iterable.next(), None);
    }

    #[test]
    fn node_with_attributes() {
        let mut node = Node::new();
        assert!(node.is_empty());
        assert!(!node.has_nodes());
        assert!(!node.has_attrs());
        assert_eq!(node.node_count(), 0);
        assert_eq!(node.attr_count(), 0);

        assert_eq!(node.insert_attr("key", Value::new_int(6)), None);
        assert!(!node.is_empty());
        assert!(!node.has_nodes());
        assert!(node.has_attrs());
        assert_eq!(node.node_count(), 0);
        assert_eq!(node.attr_count(), 1);

        assert_eq!(node.delete_attr("key"), Some(Value::new_int(6)));
        assert!(node.is_empty());
        assert!(!node.has_nodes());
        assert!(!node.has_attrs());
        assert_eq!(node.node_count(), 0);
        assert_eq!(node.attr_count(), 0);

        assert_eq!(node.insert_attr("key", Value::new_int(7)), None);
        assert!(!node.is_empty());
        assert!(!node.has_nodes());
        assert!(node.has_attrs());
        assert_eq!(node.node_count(), 0);
        assert_eq!(node.attr_count(), 1);

        let mut iterable = node.iter_attrs();
        assert_eq!(iterable.len(), 1);
        assert_eq!(
            iterable.next(),
            Some((&("key".to_string()), &Value::new_int(7))));
        assert_eq!(iterable.next(), None);
    }

    #[test]
    fn document_tests() {
        let mut doc = Document::new();
        doc.new_node_or_get("node_name").new_node_or_get("subnode");
        assert_eq!(
            doc.get_node("node_name")
                .expect("couldn't find node_name")
                .get_node("subnode"),
            Some(&Node::new()));

        let mut iterable = doc
            .get_node("node_name").expect("missing subnode 'node_name'")
            .iter_nodes();
        assert_eq!(iterable.len(), 1);
        assert_eq!(
            iterable.next(),
            Some((&("subnode".to_string()), &Node::new())));
        assert_eq!(iterable.next(), None);
    }
}