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}