plexus_core/plexus/
path.rs1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
5pub struct Provenance {
6 segments: Vec<String>,
9}
10
11impl Provenance {
12 pub fn root(behavior_name: impl Into<String>) -> Self {
13 Self {
14 segments: vec![behavior_name.into()],
15 }
16 }
17
18 pub fn extend(&self, behavior_name: impl Into<String>) -> Self {
19 let mut new_path = self.clone();
20 new_path.segments.push(behavior_name.into());
21 new_path
22 }
23
24 pub fn depth(&self) -> usize {
25 self.segments.len()
26 }
27
28 pub fn segments(&self) -> &[String] {
29 &self.segments
30 }
31
32 pub fn parent(&self) -> Option<Self> {
33 if self.segments.len() <= 1 {
34 None
35 } else {
36 Some(Self {
37 segments: self.segments[..self.segments.len() - 1].to_vec(),
38 })
39 }
40 }
41}
42
43impl std::fmt::Display for Provenance {
44 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
45 write!(f, "{}", self.segments.join("."))
46 }
47}