use std::fmt;
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Clone, Copy)]
pub struct LensPathElement {
id: u64,
}
impl LensPathElement {
pub fn new(id: u64) -> LensPathElement {
LensPathElement { id }
}
}
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone)]
pub struct LensPath {
pub elements: Vec<LensPathElement>,
}
impl LensPath {
pub fn empty() -> LensPath {
LensPath { elements: vec![] }
}
pub fn new(id: u64) -> LensPath {
LensPath {
elements: vec![LensPathElement::new(id)],
}
}
pub fn from_index(index: usize) -> LensPath {
LensPath::new(index as u64)
}
pub fn from_pair(id0: u64, id1: u64) -> LensPath {
LensPath {
elements: vec![LensPathElement::new(id0), LensPathElement::new(id1)],
}
}
pub fn from_vec(ids: Vec<u64>) -> LensPath {
LensPath {
elements: ids.into_iter().map(LensPathElement::new).collect(),
}
}
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<_>>()
.join(", ")
)
}
}
#[cfg(test)]
mod tests {
use super::{LensPath, LensPathElement};
#[test]
fn lens_path_constructors_should_work() {
assert_eq!(LensPath::empty().elements, Vec::<LensPathElement>::new());
assert_eq!(LensPath::new(4).elements, vec![LensPathElement::new(4)]);
assert_eq!(LensPath::from_index(2), LensPath::new(2));
assert_eq!(
LensPath::from_pair(1, 3).elements,
vec![LensPathElement::new(1), LensPathElement::new(3)]
);
assert_eq!(
LensPath::from_vec(vec![1, 2, 3]).elements,
vec![
LensPathElement::new(1),
LensPathElement::new(2),
LensPathElement::new(3),
]
);
}
#[test]
fn lens_path_concat_should_work() {
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 lens_path_debug_should_work() {
let path = LensPath::from_vec(vec![1, 2, 3, 4, 5]);
assert_eq!(format!("{path:?}"), "[1, 2, 3, 4, 5]");
}
}