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
//
// Copyright (c) 2015 Plausible Labs Cooperative, Inc.
// All rights reserved.
//

use std::fmt;

/// An element in a `LensPath`.
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Clone, Copy)]
pub struct LensPathElement {
    id: u64,
}

impl LensPathElement {
    pub fn new(id: u64) -> LensPathElement {
        LensPathElement { id }
    }
}

/// Describes a lens relative to a source data structure.
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone)]
pub struct LensPath {
    /// The path elements.
    pub elements: Vec<LensPathElement>,
}

impl LensPath {
    /// Creates a new `LensPath` with no elements.
    pub fn empty() -> LensPath {
        LensPath { elements: vec![] }
    }

    /// Creates a new `LensPath` with a single element.
    pub fn new(id: u64) -> LensPath {
        LensPath {
            elements: vec![LensPathElement { id }],
        }
    }

    /// Creates a new `LensPath` with a single index (for an indexed type such as `Vec`).
    pub fn from_index(index: usize) -> LensPath {
        LensPath {
            elements: vec![LensPathElement { id: index as u64 }],
        }
    }

    /// Creates a new `LensPath` with two elements.
    pub fn from_pair(id0: u64, id1: u64) -> LensPath {
        LensPath {
            elements: vec![LensPathElement { id: id0 }, LensPathElement { id: id1 }],
        }
    }

    /// Creates a new `LensPath` from a vector of element identifiers.
    pub fn from_vec(ids: Vec<u64>) -> LensPath {
        LensPath {
            elements: ids.iter().map(|id| LensPathElement { id: *id }).collect(),
        }
    }

    /// Creates a new `LensPath` that is the concatenation of the two paths.
    pub fn concat(lhs: LensPath, rhs: LensPath) -> LensPath {
        let mut elements = lhs.elements;
        elements.extend(&rhs.elements);
        LensPath { elements }
    }
}

impl fmt::Debug for LensPath {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "[{}]",
            self.elements
                .iter()
                .map(|elem| elem.id.to_string())
                .collect::<Vec<String>>()
                .join(", ")
        )
    }
}

#[test]
fn test_lens_path_concat() {
    let p0 = LensPath::from_vec(vec![1, 2, 3]);
    let p1 = LensPath::from_vec(vec![4, 5]);
    let p2 = LensPath::concat(p0, p1);
    assert_eq!(p2, LensPath::from_vec(vec![1, 2, 3, 4, 5]));
}

#[test]
fn test_lens_path_debug() {
    let path = LensPath::from_vec(vec![1, 2, 3, 4, 5]);
    assert_eq!(format!("{:?}", path), "[1, 2, 3, 4, 5]".to_string());
}