Skip to main content

datalog_structural_model/ir/error/
display.rs

1//! `Display`, `Error`, and `From<StratificationError>` for
2//! [`ProgramValidationError`].
3
4use std::error::Error;
5use std::fmt;
6
7use super::variants::ProgramValidationError;
8use crate::ir::symbols::VariableSymbol;
9use crate::stratify::StratificationError;
10
11impl From<StratificationError> for ProgramValidationError {
12    fn from(err: StratificationError) -> Self {
13        let StratificationError::UnstratifiableCycle(cycle) = err;
14        Self::UnstratifiableCycle(cycle)
15    }
16}
17
18struct VariableList<'a>(&'a [VariableSymbol]);
19
20impl fmt::Display for VariableList<'_> {
21    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22        for (index, var) in self.0.iter().enumerate() {
23            if index > 0 {
24                f.write_str(", ")?;
25            }
26            f.write_str(var.as_str())?;
27        }
28        Ok(())
29    }
30}
31
32impl fmt::Display for ProgramValidationError {
33    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34        write!(f, "[{}] ", self.code())?;
35        match self {
36            Self::UnsafeRule { rule, variables } => write!(
37                f,
38                "{rule} is unsafe; head variables not bound by the body: {}",
39                VariableList(variables)
40            ),
41            Self::UnsafeNegation {
42                rule,
43                literal,
44                variables,
45            } => write!(
46                f,
47                "{rule}, {literal}: negated literal uses variables not bound by the positive \
48                 body: {}",
49                VariableList(variables)
50            ),
51            Self::UnsafeFilter {
52                rule,
53                literal,
54                variables,
55            } => write!(
56                f,
57                "{rule}, {literal}: filter uses variables not bound by the positive body: {}",
58                VariableList(variables)
59            ),
60            Self::UnsafeBind {
61                rule,
62                literal,
63                variables,
64            } => write!(
65                f,
66                "{rule}, {literal}: unsafe BIND (unbound expression reference or colliding \
67                 output name): {}",
68                VariableList(variables)
69            ),
70            Self::UnsafeAggregate {
71                rule,
72                literal,
73                variables,
74            } => write!(
75                f,
76                "{rule}, {literal}: unsafe aggregate (unbound input or group key, colliding \
77                 result variable, or unsafe inner literal): {}",
78                VariableList(variables)
79            ),
80            Self::NestedAggregateInInnerBody { rule, literal } => write!(
81                f,
82                "{rule}, {literal}: aggregate nested inside an aggregate's inner body; lift it \
83                 into its own rule"
84            ),
85            Self::UnstratifiableCycle(cycle) => write!(f, "program contains a {cycle}"),
86            Self::NegationUnderEqualityMode { rule, literal } => write!(
87                f,
88                "{rule}, {literal}: negative literal while equality mode is `SameAs`"
89            ),
90            Self::AggregationUnderEqualityMode { rule, literal } => write!(
91                f,
92                "{rule}, {literal}: aggregate literal while equality mode is `SameAs`"
93            ),
94            Self::SkolemInBodyAtom {
95                rule,
96                literal,
97                term,
98            } => write!(
99                f,
100                "{rule}, {literal}, {term}: SKOLEM term in a body literal; SKOLEM is only \
101                 allowed in the head"
102            ),
103            Self::TripleTermNestingExceeded {
104                rule,
105                site,
106                term,
107                depth,
108                limit,
109            } => write!(
110                f,
111                "{rule}, {site}, {term}: triple term nested {depth} deep; limit is {limit}"
112            ),
113            Self::InvalidSkolemArguments { rule, site, term } => write!(
114                f,
115                "{rule}, {site}, {term}: Skolem arguments must be variables or constants"
116            ),
117        }
118    }
119}
120
121impl Error for ProgramValidationError {}