Skip to main content

datalog_structural_model/
profile.rs

1//! Checked generic language profiles; RDF/OWL semantics belong in rule packages.
2use crate::error_code::ErrorCode;
3use crate::{DatalogCapabilities, DatalogFeature, EqualityMode, Program, ProgramValidationError};
4use std::{error::Error, fmt};
5
6/// Explicit language features and equality regime selected for a program.
7/// This is neither an RDF entailment profile nor an execution strategy.
8#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
9#[serde(deny_unknown_fields)]
10pub struct DatalogProfile {
11    capabilities: DatalogCapabilities,
12    equality_mode: EqualityMode,
13}
14impl DatalogProfile {
15    /// Construct an explicit language contract. Program-specific combinations
16    /// are checked by [`Program::validate_for`].
17    #[must_use]
18    pub fn new(capabilities: DatalogCapabilities, equality_mode: EqualityMode) -> Self {
19        Self {
20            capabilities,
21            equality_mode,
22        }
23    }
24    /// Positive flat Datalog without built-in expression extensions.
25    #[must_use]
26    pub fn positive() -> Self {
27        Self::new(DatalogCapabilities::positive(), EqualityMode::Default)
28    }
29    /// Flat Datalog with stratified negation, without built-in extensions.
30    #[must_use]
31    pub fn stratified() -> Self {
32        Self::new(
33            DatalogCapabilities::positive().with(DatalogFeature::StratifiedNegation),
34            EqualityMode::Default,
35        )
36    }
37    /// The explicitly selected language features.
38    #[must_use]
39    pub fn capabilities(&self) -> &DatalogCapabilities {
40        &self.capabilities
41    }
42    /// The selected equality validation regime.
43    #[must_use]
44    pub fn equality_mode(&self) -> EqualityMode {
45        self.equality_mode
46    }
47}
48
49/// Features required by the program and absent from the selected profile.
50#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
51#[serde(deny_unknown_fields)]
52pub struct UnsupportedCapabilities {
53    missing: DatalogCapabilities,
54}
55impl UnsupportedCapabilities {
56    /// Inspect every missing feature, including nested operators.
57    #[must_use]
58    pub fn missing(&self) -> &DatalogCapabilities {
59        &self.missing
60    }
61}
62impl fmt::Display for UnsupportedCapabilities {
63    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64        write!(f, "unsupported Datalog features: {:?}", self.missing)
65    }
66}
67impl Error for UnsupportedCapabilities {}
68
69/// An invalid program or an explicitly unsupported language feature.
70#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
71#[serde(deny_unknown_fields)]
72pub enum ProfileCheckError {
73    /// Structural safety, stratification or equality compatibility failed.
74    InvalidProgram(ProgramValidationError),
75    /// One or more required features are absent from the profile.
76    Unsupported(UnsupportedCapabilities),
77}
78impl fmt::Display for ProfileCheckError {
79    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
80        match self {
81            Self::InvalidProgram(error) => error.fmt(f),
82            Self::Unsupported(error) => error.fmt(f),
83        }
84    }
85}
86impl ProfileCheckError {
87    /// Code of [`Self::InvalidProgram`]; the wrapped error carries its own code.
88    pub const CODE_INVALID_PROGRAM: ErrorCode = ErrorCode::new("invalid-program");
89    /// Code of [`Self::Unsupported`].
90    pub const CODE_UNSUPPORTED_CAPABILITIES: ErrorCode = ErrorCode::new("unsupported-capabilities");
91    /// Stable kebab-case code of this variant.
92    #[must_use]
93    pub fn code(&self) -> ErrorCode {
94        match self {
95            Self::InvalidProgram(_) => Self::CODE_INVALID_PROGRAM,
96            Self::Unsupported(_) => Self::CODE_UNSUPPORTED_CAPABILITIES,
97        }
98    }
99}
100impl Error for ProfileCheckError {
101    fn source(&self) -> Option<&(dyn Error + 'static)> {
102        match self {
103            Self::InvalidProgram(error) => Some(error),
104            Self::Unsupported(error) => Some(error),
105        }
106    }
107}
108impl Program {
109    /// Validate safety and reject every feature missing from the selected contract.
110    /// # Errors
111    /// Returns typed structural failures or the exact unsupported feature set.
112    pub fn validate_for(&self, profile: &DatalogProfile) -> Result<(), ProfileCheckError> {
113        self.validate_with_equality_mode(profile.equality_mode)
114            .map_err(ProfileCheckError::InvalidProgram)?;
115        let missing = self
116            .required_capabilities()
117            .missing_from(&profile.capabilities);
118        if !missing.is_empty() {
119            return Err(ProfileCheckError::Unsupported(UnsupportedCapabilities {
120                missing,
121            }));
122        }
123        Ok(())
124    }
125}