Skip to main content

datalog_structural_model/position/
index.rs

1//! Index newtypes: a rule in a program, a literal in a body, a slot in an
2//! atom or Skolem argument list, and a term inside a filter or BIND
3//! expression.
4
5use std::fmt;
6
7/// Position of a rule in [`Program::rules`](crate::Program::rules).
8#[derive(
9    Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize,
10)]
11#[serde(transparent)]
12pub struct RuleIndex(usize);
13
14impl RuleIndex {
15    /// Wrap a zero-based rule position.
16    #[must_use]
17    pub const fn new(index: usize) -> Self {
18        Self(index)
19    }
20
21    /// The zero-based rule position.
22    #[must_use]
23    pub const fn get(self) -> usize {
24        self.0
25    }
26}
27
28impl fmt::Display for RuleIndex {
29    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30        write!(f, "rule {}", self.0)
31    }
32}
33
34/// Position of a literal in a rule body or an aggregate's inner body.
35#[derive(
36    Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize,
37)]
38#[serde(transparent)]
39pub struct LiteralIndex(usize);
40
41impl LiteralIndex {
42    /// Wrap a zero-based literal position.
43    #[must_use]
44    pub const fn new(index: usize) -> Self {
45        Self(index)
46    }
47
48    /// The zero-based literal position.
49    #[must_use]
50    pub const fn get(self) -> usize {
51        self.0
52    }
53}
54
55/// Position of a term in an atom, triple term, or Skolem argument list.
56#[derive(
57    Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize,
58)]
59#[serde(transparent)]
60pub struct AtomSlot(usize);
61
62impl AtomSlot {
63    /// Wrap a zero-based slot position.
64    #[must_use]
65    pub const fn new(index: usize) -> Self {
66        Self(index)
67    }
68
69    /// The zero-based slot position.
70    #[must_use]
71    pub const fn get(self) -> usize {
72        self.0
73    }
74}
75
76/// Position of a top-level term inside a filter or BIND expression, in
77/// left-to-right source order.
78#[derive(
79    Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize,
80)]
81#[serde(transparent)]
82pub struct ExpressionTermIndex(usize);
83
84impl ExpressionTermIndex {
85    /// Wrap a zero-based expression-term position.
86    #[must_use]
87    pub const fn new(index: usize) -> Self {
88        Self(index)
89    }
90
91    /// The zero-based expression-term position.
92    #[must_use]
93    pub const fn get(self) -> usize {
94        self.0
95    }
96}
97
98/// Position of a disjunct in a rule head. Every non-disjunctive head has
99/// exactly one disjunct, at position zero.
100#[derive(
101    Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize,
102)]
103#[serde(transparent)]
104pub struct HeadDisjunctIndex(usize);
105
106impl HeadDisjunctIndex {
107    /// Wrap a zero-based disjunct position.
108    #[must_use]
109    pub const fn new(index: usize) -> Self {
110        Self(index)
111    }
112
113    /// The zero-based disjunct position.
114    #[must_use]
115    pub const fn get(self) -> usize {
116        self.0
117    }
118}
119
120/// Position of an atom inside one head disjunct (conjunction).
121#[derive(
122    Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize,
123)]
124#[serde(transparent)]
125pub struct HeadAtomIndex(usize);
126
127impl HeadAtomIndex {
128    /// Wrap a zero-based head-atom position.
129    #[must_use]
130    pub const fn new(index: usize) -> Self {
131        Self(index)
132    }
133
134    /// The zero-based head-atom position.
135    #[must_use]
136    pub const fn get(self) -> usize {
137        self.0
138    }
139}