use std::collections::BTreeSet;
use std::{error::Error, fmt};
use super::cycle::UnstratifiableCycle;
use crate::error_code::ErrorCode;
use crate::position::RuleIndex;
#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Stratification {
pub strata: Vec<Stratum>,
pub rule_stratum: Vec<usize>,
}
impl Stratification {
#[must_use]
pub fn stratum_of_rule(&self, rule: RuleIndex) -> Option<usize> {
self.rule_stratum.get(rule.get()).copied()
}
}
#[derive(Clone, Debug, Default, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Stratum {
pub rules: BTreeSet<RuleIndex>,
}
#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub enum StratificationError {
UnstratifiableCycle(UnstratifiableCycle),
}
impl StratificationError {
pub const CODE_UNSTRATIFIABLE_CYCLE: ErrorCode = ErrorCode::new("unstratifiable-cycle");
#[must_use]
pub fn code(&self) -> ErrorCode {
match self {
Self::UnstratifiableCycle(_) => Self::CODE_UNSTRATIFIABLE_CYCLE,
}
}
}
impl fmt::Display for StratificationError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::UnstratifiableCycle(cycle) => cycle.fmt(f),
}
}
}
impl Error for StratificationError {}