datalog_structural_model/position/
term_path.rs1use std::fmt;
5
6use super::index::{AtomSlot, ExpressionTermIndex};
7
8#[derive(
10 Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize,
11)]
12#[serde(deny_unknown_fields)]
13pub enum TermRoot {
14 Slot(AtomSlot),
16 ExpressionTerm(ExpressionTermIndex),
18}
19
20#[derive(
23 Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize,
24)]
25#[serde(deny_unknown_fields)]
26pub struct TermPath {
27 root: TermRoot,
28 nested: Vec<AtomSlot>,
30}
31
32impl TermPath {
33 #[must_use]
35 pub fn new(root: TermRoot) -> Self {
36 Self {
37 root,
38 nested: Vec::new(),
39 }
40 }
41
42 #[must_use]
44 pub fn child(&self, slot: AtomSlot) -> Self {
45 let mut nested = self.nested.clone();
46 nested.push(slot);
47 Self {
48 root: self.root,
49 nested,
50 }
51 }
52
53 #[must_use]
55 pub fn root(&self) -> TermRoot {
56 self.root
57 }
58
59 #[must_use]
61 pub fn nested(&self) -> &[AtomSlot] {
62 &self.nested
63 }
64}
65
66impl fmt::Display for TermPath {
67 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
68 match self.root {
69 TermRoot::Slot(slot) => write!(f, "slot {}", slot.get())?,
70 TermRoot::ExpressionTerm(index) => write!(f, "expression term {}", index.get())?,
71 }
72 for slot in &self.nested {
73 write!(f, " > argument {}", slot.get())?;
74 }
75 Ok(())
76 }
77}