datalog_structural_model/stratify/
types.rs1use std::collections::BTreeSet;
4use std::{error::Error, fmt};
5
6use super::cycle::UnstratifiableCycle;
7use crate::error_code::ErrorCode;
8use crate::position::RuleIndex;
9
10#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
16#[serde(deny_unknown_fields)]
17pub struct Stratification {
18 pub strata: Vec<Stratum>,
20 pub rule_stratum: Vec<usize>,
22}
23
24impl Stratification {
25 #[must_use]
27 pub fn stratum_of_rule(&self, rule: RuleIndex) -> Option<usize> {
28 self.rule_stratum.get(rule.get()).copied()
29 }
30}
31
32#[derive(Clone, Debug, Default, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
34#[serde(deny_unknown_fields)]
35pub struct Stratum {
36 pub rules: BTreeSet<RuleIndex>,
38}
39
40#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
42#[serde(deny_unknown_fields)]
43pub enum StratificationError {
44 UnstratifiableCycle(UnstratifiableCycle),
46}
47
48impl StratificationError {
49 pub const CODE_UNSTRATIFIABLE_CYCLE: ErrorCode = ErrorCode::new("unstratifiable-cycle");
51
52 #[must_use]
54 pub fn code(&self) -> ErrorCode {
55 match self {
56 Self::UnstratifiableCycle(_) => Self::CODE_UNSTRATIFIABLE_CYCLE,
57 }
58 }
59}
60
61impl fmt::Display for StratificationError {
62 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63 match self {
64 Self::UnstratifiableCycle(cycle) => cycle.fmt(f),
65 }
66 }
67}
68
69impl Error for StratificationError {}