Skip to main content

datalog_structural_model/position/
literal_path.rs

1//! [`LiteralPath`] — a literal in a rule body, possibly inside aggregates.
2
3use std::fmt;
4
5use super::index::LiteralIndex;
6
7/// Path from a rule body to one literal: the outer body position, then one
8/// inner-body position per enclosing aggregate.
9#[derive(
10    Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize,
11)]
12#[serde(transparent)]
13pub struct LiteralPath(
14    // bounded: one step per authored aggregate nesting level.
15    Vec<LiteralIndex>,
16);
17
18impl LiteralPath {
19    /// Path to a literal directly in the rule body.
20    #[must_use]
21    pub fn top(index: LiteralIndex) -> Self {
22        Self(vec![index])
23    }
24
25    /// Path to a literal inside the aggregate this path points to.
26    #[must_use]
27    pub fn inner(&self, index: LiteralIndex) -> Self {
28        let mut steps = self.0.clone();
29        steps.push(index);
30        Self(steps)
31    }
32
33    /// Outer body position followed by each inner-body position.
34    #[must_use]
35    pub fn steps(&self) -> &[LiteralIndex] {
36        &self.0
37    }
38
39    /// Position in the rule body of the outermost literal.
40    #[must_use]
41    pub fn outer(&self) -> LiteralIndex {
42        self.0[0]
43    }
44}
45
46impl fmt::Display for LiteralPath {
47    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48        write!(f, "body literal {}", self.0[0].get())?;
49        for step in &self.0[1..] {
50            write!(f, " > inner literal {}", step.get())?;
51        }
52        Ok(())
53    }
54}