Skip to main content

blockworx/document/
path.rs

1use std::fmt;
2
3use crate::store::RectId;
4
5#[derive(Clone, Default, Debug, PartialEq, Eq)]
6pub struct BlockPath(Vec<RectId>);
7
8impl BlockPath {
9    pub fn empty() -> Self {
10        Self(Vec::new())
11    }
12
13    pub fn push(&mut self, id: RectId) {
14        self.0.push(id);
15    }
16
17    pub fn pop(&mut self) -> Option<RectId> {
18        self.0.pop()
19    }
20
21    pub fn segments(&self) -> &[RectId] {
22        &self.0
23    }
24
25    pub fn is_empty(&self) -> bool {
26        self.0.is_empty()
27    }
28}
29
30impl fmt::Display for BlockPath {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        let mut first = true;
33        for seg in &self.0 {
34            if !first {
35                f.write_str(":")?;
36            }
37            write!(f, "{seg}")?;
38            first = false;
39        }
40        Ok(())
41    }
42}
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47
48    #[test]
49    fn push_pop_segments() {
50        let mut p = BlockPath::empty();
51        assert!(p.is_empty());
52        p.push(RectId::nth_default(1));
53        p.push(RectId::nth_default(3));
54        assert_eq!(
55            p.segments(),
56            &[RectId::nth_default(1), RectId::nth_default(3)]
57        );
58        assert_eq!(p.pop(), Some(RectId::nth_default(3)));
59        assert_eq!(p.segments(), &[RectId::nth_default(1)]);
60    }
61
62    #[test]
63    fn display() {
64        let mut p = BlockPath::empty();
65        assert_eq!(p.to_string(), "");
66        p.push(RectId::nth_default(0));
67        p.push(RectId::nth_default(7));
68        assert_eq!(p.to_string(), "b0:b7");
69    }
70}