use crate::im::{AttributeId, ClusterId, CommandId, EventId};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Conformance {
Mandatory,
Optional,
Provisional,
Deprecated,
Disallowed,
Described,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Condition {
Always,
Feature(u8),
Attribute(AttributeId),
Command(CommandId),
Event(EventId),
Cluster(ClusterId),
Named(&'static str),
Not(&'static Condition),
All(&'static [Condition]),
Any(&'static [Condition]),
ExactlyOne(&'static [Condition]),
}
pub trait Supports {
fn feature_map(&self) -> u32;
fn has_attribute(&self, id: AttributeId) -> bool;
fn has_command(&self, id: CommandId) -> bool;
fn has_event(&self, id: EventId) -> bool;
fn has_cluster(&self, id: ClusterId) -> bool;
fn named(&self, _name: &str) -> Option<bool> {
None
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Truth {
True,
False,
Unknown,
}
impl Truth {
const fn not(self) -> Self {
match self {
Self::True => Self::False,
Self::False => Self::True,
Self::Unknown => Self::Unknown,
}
}
}
impl Condition {
fn eval<S: Supports + ?Sized>(&self, device: &S) -> Truth {
match self {
Self::Always => Truth::True,
Self::Feature(bit) => {
if *bit >= 32 {
return Truth::False;
}
if device.feature_map() & (1u32 << *bit) != 0 {
Truth::True
} else {
Truth::False
}
}
Self::Attribute(id) => Truth::from(device.has_attribute(*id)),
Self::Command(id) => Truth::from(device.has_command(*id)),
Self::Event(id) => Truth::from(device.has_event(*id)),
Self::Cluster(id) => Truth::from(device.has_cluster(*id)),
Self::Named(name) => device.named(name).map_or(Truth::Unknown, Truth::from),
Self::Not(inner) => inner.eval(device).not(),
Self::All(terms) => {
let mut unknown = false;
for term in *terms {
match term.eval(device) {
Truth::False => return Truth::False,
Truth::Unknown => unknown = true,
Truth::True => {}
}
}
if unknown { Truth::Unknown } else { Truth::True }
}
Self::Any(terms) => {
let mut unknown = false;
for term in *terms {
match term.eval(device) {
Truth::True => return Truth::True,
Truth::Unknown => unknown = true,
Truth::False => {}
}
}
if unknown {
Truth::Unknown
} else {
Truth::False
}
}
Self::ExactlyOne(terms) => {
let mut count = 0usize;
for term in *terms {
match term.eval(device) {
Truth::True => count = count.saturating_add(1),
Truth::Unknown => return Truth::Unknown,
Truth::False => {}
}
}
Truth::from(count == 1)
}
}
}
}
impl From<bool> for Truth {
fn from(value: bool) -> Self {
if value { Self::True } else { Self::False }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Clause {
pub verdict: Conformance,
pub when: Condition,
}
impl Clause {
#[must_use]
pub const fn always(verdict: Conformance) -> Self {
Self {
verdict,
when: Condition::Always,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Conform {
pub clauses: &'static [Clause],
}
impl Conform {
pub const MANDATORY: Self = Self {
clauses: &[Clause::always(Conformance::Mandatory)],
};
pub const OPTIONAL: Self = Self {
clauses: &[Clause::always(Conformance::Optional)],
};
pub const DISALLOWED: Self = Self {
clauses: &[Clause::always(Conformance::Disallowed)],
};
pub const DESCRIBED: Self = Self {
clauses: &[Clause::always(Conformance::Described)],
};
#[must_use]
pub fn verdict<S: Supports + ?Sized>(&self, device: &S) -> Conformance {
for clause in self.clauses {
match clause.when.eval(device) {
Truth::True => return clause.verdict,
Truth::Unknown => return Conformance::Described,
Truth::False => {}
}
}
Conformance::Disallowed
}
}