Skip to main content

datalog_structural_model/position/
head_path.rs

1//! [`HeadAtomPath`] — one atom of a rule head, through its disjunct.
2
3use std::fmt;
4
5use super::index::{HeadAtomIndex, HeadDisjunctIndex};
6
7/// Path from a rule head to one atom: the disjunct, then the atom inside that
8/// disjunct's conjunction. An atomic head is disjunct 0, atom 0.
9#[derive(
10    Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize,
11)]
12#[serde(deny_unknown_fields)]
13pub struct HeadAtomPath {
14    disjunct: HeadDisjunctIndex,
15    atom: HeadAtomIndex,
16}
17
18impl HeadAtomPath {
19    /// Atom `atom` of head disjunct `disjunct`.
20    #[must_use]
21    pub const fn new(disjunct: HeadDisjunctIndex, atom: HeadAtomIndex) -> Self {
22        Self { disjunct, atom }
23    }
24
25    /// The only atom of an atomic head.
26    #[must_use]
27    pub const fn atomic() -> Self {
28        Self::new(HeadDisjunctIndex::new(0), HeadAtomIndex::new(0))
29    }
30
31    /// The disjunct holding the atom.
32    #[must_use]
33    pub const fn disjunct(self) -> HeadDisjunctIndex {
34        self.disjunct
35    }
36
37    /// The atom's position inside its disjunct.
38    #[must_use]
39    pub const fn atom(self) -> HeadAtomIndex {
40        self.atom
41    }
42}
43
44impl fmt::Display for HeadAtomPath {
45    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46        write!(
47            f,
48            "head disjunct {} atom {}",
49            self.disjunct.get(),
50            self.atom.get()
51        )
52    }
53}