use crate::ids::{ContainerId, MetaCommandId, ParamId, SpaceSystemId, Span};
use crate::intern::NameId;
#[derive(Clone, Debug)]
pub struct Container {
pub name: NameId,
pub qualified_name: NameId,
pub space_system: SpaceSystemId,
pub is_abstract: bool,
pub base: Option<ContainerId>,
pub is_command: bool,
pub command: Option<MetaCommandId>,
pub restriction: Vec<MatchCriteria>,
pub entries: Span,
pub inheritors: Vec<ContainerId>,
pub short_description: Option<NameId>,
pub long_description: Option<NameId>,
}
#[derive(Clone, Copy, Debug)]
pub struct Entry {
pub kind: EntryKind,
pub location: Option<Location>,
pub repeat: Option<u32>,
}
#[derive(Clone, Copy, Debug)]
pub enum EntryKind {
Parameter(ParamId),
Container(ContainerId),
FixedValue {
name: Option<NameId>,
value: Span,
size_in_bits: u32,
},
Unsupported {
element: NameId,
},
}
#[derive(Clone, Copy, Debug)]
pub struct Location {
pub reference: LocationReference,
pub offset_in_bits: i64,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub enum LocationReference {
#[default]
PreviousEntry,
ContainerStart,
ContainerEnd,
NextEntry,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum CompareOp {
Equal,
NotEqual,
Less,
LessOrEqual,
Greater,
GreaterOrEqual,
}
impl CompareOp {
#[must_use]
pub fn parse(text: &str) -> Option<Self> {
Some(match text {
"==" | "eq" => Self::Equal,
"!=" | "neq" => Self::NotEqual,
"<" | "lt" | "<" => Self::Less,
"<=" | "leq" | "<=" => Self::LessOrEqual,
">" | "gt" | ">" => Self::Greater,
">=" | "geq" | ">=" => Self::GreaterOrEqual,
_ => return None,
})
}
#[must_use]
pub const fn matches(self, ordering: std::cmp::Ordering) -> bool {
use std::cmp::Ordering::{Equal, Greater, Less};
matches!(
(self, ordering),
(Self::Equal, Equal)
| (Self::NotEqual, Less | Greater)
| (Self::Less, Less)
| (Self::LessOrEqual, Less | Equal)
| (Self::Greater, Greater)
| (Self::GreaterOrEqual, Greater | Equal)
)
}
}
#[derive(Clone, Debug)]
pub enum MatchCriteria {
Comparison(Comparison),
Boolean(BooleanExpr),
Unsupported {
element: NameId,
},
}
#[derive(Clone, Copy, Debug)]
pub struct ComparisonValue {
pub text: NameId,
pub as_int: Option<i128>,
pub as_float: Option<f64>,
}
impl ComparisonValue {
#[must_use]
pub fn new(text: NameId, literal: &str) -> Self {
let literal = literal.trim();
Self {
text,
as_int: literal.parse::<i128>().ok(),
as_float: literal.parse::<f64>().ok(),
}
}
}
#[derive(Clone, Copy, Debug)]
pub struct Comparison {
pub parameter: ParamId,
pub operator: CompareOp,
pub value: ComparisonValue,
pub use_calibrated: bool,
}
#[derive(Clone, Debug)]
pub enum BooleanExpr {
Condition(Condition),
And(Vec<BooleanExpr>),
Or(Vec<BooleanExpr>),
}
#[derive(Clone, Copy, Debug)]
pub struct Condition {
pub left: Operand,
pub operator: CompareOp,
pub right: Operand,
}
#[derive(Clone, Copy, Debug)]
pub enum Operand {
Parameter {
parameter: ParamId,
use_calibrated: bool,
},
Literal(ComparisonValue),
}
#[derive(Clone, Debug)]
pub struct SpaceSystem {
pub name: NameId,
pub qualified_name: NameId,
pub parent: Option<SpaceSystemId>,
pub children: Vec<SpaceSystemId>,
}