use super::rules::{Polarity, RuleRecord, RuleState, Standing};
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct RuleSubject {
pub roles: Vec<String>,
pub selector: String,
pub configuration: String,
pub projection: String,
pub object: Option<crate::identity::StableFaceId>,
}
impl RuleSubject {
pub fn named(selector: &str) -> Self {
Self {
selector: selector.to_owned(),
..Self::default()
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AuthorityStanding {
pub anchor: String,
pub in_force: bool,
pub reason: Option<String>,
}
impl AuthorityStanding {
pub fn in_force(anchor: &str) -> Self {
Self {
anchor: anchor.to_owned(),
in_force: true,
reason: None,
}
}
pub fn lapsed(anchor: &str, reason: &str) -> Self {
Self {
anchor: anchor.to_owned(),
in_force: false,
reason: Some(reason.to_owned()),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum PredicateAnswer {
Satisfied,
NotSatisfied,
Unknown(String),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum RuleOutcome {
NotApplicable {
reason: &'static str,
},
NotConstraining {
reason: String,
},
Exempt {
scope: String,
authority: String,
reason: String,
},
Satisfied,
Violated,
Unknown {
reason: String,
},
NeedsReview {
reason: String,
},
}
impl RuleOutcome {
pub fn label(&self) -> &'static str {
match self {
Self::NotApplicable { .. } => "not applicable",
Self::NotConstraining { .. } => "does not constrain",
Self::Exempt { .. } => "exempt",
Self::Satisfied => "met",
Self::Violated => "violated",
Self::Unknown { .. } => "unknown",
Self::NeedsReview { .. } => "needs review",
}
}
pub fn is_satisfied(&self) -> bool {
matches!(self, Self::Satisfied)
}
}
impl RuleRecord {
pub fn decide(
&self,
subject: &RuleSubject,
current: &[(String, String)],
authority: Option<&AuthorityStanding>,
conflict: Option<&str>,
predicate: Option<&PredicateAnswer>,
) -> RuleOutcome {
if let Some(reason) = self.applicability(subject) {
return reason;
}
if let Standing::NotConstraining(reason) = self.standing(current) {
return RuleOutcome::NotConstraining {
reason: reason.to_owned(),
};
}
if let Some(anchor) = &self.authority_ref {
match authority {
None => {
return RuleOutcome::NotConstraining {
reason: format!(
"its authority `{anchor}` was not resolved against this tree's adoption ledger, so this rule cannot be read as constraining"
),
};
}
Some(standing) if !standing.in_force => {
return RuleOutcome::NotConstraining {
reason: format!(
"its authority `{}` is no longer in force: {}",
standing.anchor,
standing
.reason
.as_deref()
.unwrap_or("the newest record lapsed")
),
};
}
Some(standing) if standing.anchor != *anchor => {
return RuleOutcome::NotConstraining {
reason: format!(
"the authority resolved for this decision is `{}`, but the rule was accepted against `{anchor}`",
standing.anchor
),
};
}
Some(_) => {}
}
}
if let Some(reason) = conflict {
return RuleOutcome::NeedsReview {
reason: reason.to_owned(),
};
}
if let Some(exception) = self.exception_for(&subject.selector) {
return RuleOutcome::Exempt {
scope: exception.scope.clone(),
authority: exception.authority.clone(),
reason: exception.reason.clone(),
};
}
match predicate {
None => RuleOutcome::Unknown {
reason: format!(
"this decider does not evaluate `{}`; it needs an executable check reference or a semantic review before it can answer met or violated",
self.requirement
),
},
Some(PredicateAnswer::Unknown(reason)) => RuleOutcome::Unknown {
reason: reason.clone(),
},
Some(PredicateAnswer::Satisfied) => match self.polarity {
Polarity::Require => RuleOutcome::Satisfied,
Polarity::Forbid => RuleOutcome::Violated,
},
Some(PredicateAnswer::NotSatisfied) => match self.polarity {
Polarity::Require => RuleOutcome::Violated,
Polarity::Forbid => RuleOutcome::Satisfied,
},
}
}
fn applicability(&self, subject: &RuleSubject) -> Option<RuleOutcome> {
if subject.selector.trim().is_empty() {
return Some(RuleOutcome::Unknown {
reason: "no subject selector was supplied, so no rule can be matched to an object"
.to_owned(),
});
}
if let Some(binding) = &self.object {
match &subject.object {
None => {
return Some(RuleOutcome::Unknown {
reason: format!(
"this rule is bound to object {} (`{}`); the decision could not resolve the subject's persistent identity",
binding.stable, binding.path
),
});
}
Some(identity) if identity != &binding.stable => {
return Some(RuleOutcome::NotApplicable {
reason: "the subject is a different object than the one this rule is bound to",
});
}
Some(_) => {}
}
}
if !self.scope.roles.is_empty() {
if subject.roles.is_empty() {
return Some(RuleOutcome::Unknown {
reason: format!(
"this rule speaks about the role(s) {}; the decision was given no roles for the subject",
self.scope.roles.join(", ")
),
});
}
if !self
.scope
.roles
.iter()
.any(|role| subject.roles.contains(role))
{
return Some(RuleOutcome::NotApplicable {
reason: "the subject's role is not one this rule speaks about",
});
}
}
if !self.scope.configurations.is_empty() {
if subject.configuration.is_empty() {
return Some(RuleOutcome::Unknown {
reason: format!(
"this rule was accepted for the configuration(s) {}; the decision names no configuration for the subject",
self.scope.configurations.join(", ")
),
});
}
if !self
.scope
.configurations
.iter()
.any(|known| known == &subject.configuration)
{
return Some(RuleOutcome::NotApplicable {
reason: "the subject is not under a configuration this rule was accepted for",
});
}
}
if subject.projection.is_empty() {
return Some(RuleOutcome::Unknown {
reason: "the decision names no projection for the subject".to_owned(),
});
}
if self.scope.projection != subject.projection {
return Some(RuleOutcome::NotApplicable {
reason: "the subject is under a different projection than the one this rule speaks about",
});
}
if self.object.is_none() && self.scope.selector != subject.selector {
return Some(RuleOutcome::NotApplicable {
reason: "the subject is not the entity this rule selects",
});
}
None
}
}
pub fn conflict_reason(rule: &RuleRecord, other: &RuleRecord) -> Option<String> {
if !matches!(rule.state, RuleState::Accepted | RuleState::Conflicted)
|| !matches!(other.state, RuleState::Accepted | RuleState::Conflicted)
{
return None;
}
if !rule.contradicts(other) {
return None;
}
Some(format!(
"`{}` and `{}` are accepted rules over the same scope with opposite polarity; a person resolves this, and a newer revision does not win by itself",
rule.rule_id, other.rule_id
))
}
#[cfg(test)]
#[path = "decision_tests.rs"]
mod decision_tests;