use std::fmt;
use super::index::{AtomSlot, ExpressionTermIndex};
#[derive(
Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize,
)]
#[serde(deny_unknown_fields)]
pub enum TermRoot {
Slot(AtomSlot),
ExpressionTerm(ExpressionTermIndex),
}
#[derive(
Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize,
)]
#[serde(deny_unknown_fields)]
pub struct TermPath {
root: TermRoot,
nested: Vec<AtomSlot>,
}
impl TermPath {
#[must_use]
pub fn new(root: TermRoot) -> Self {
Self {
root,
nested: Vec::new(),
}
}
#[must_use]
pub fn child(&self, slot: AtomSlot) -> Self {
let mut nested = self.nested.clone();
nested.push(slot);
Self {
root: self.root,
nested,
}
}
#[must_use]
pub fn root(&self) -> TermRoot {
self.root
}
#[must_use]
pub fn nested(&self) -> &[AtomSlot] {
&self.nested
}
}
impl fmt::Display for TermPath {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.root {
TermRoot::Slot(slot) => write!(f, "slot {}", slot.get())?,
TermRoot::ExpressionTerm(index) => write!(f, "expression term {}", index.get())?,
}
for slot in &self.nested {
write!(f, " > argument {}", slot.get())?;
}
Ok(())
}
}