use alloc::string::String;
use alloc::vec;
use alloc::vec::Vec;
use super::{BitOrder, By, Coverage, Discriminant, Endian, Field, Kind, Len, Scalar, Stated};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Count {
Field {
by: By,
cap: Option<usize>,
},
Squared {
by: By,
cap: Option<usize>,
},
Strided {
by: By,
cap: Option<usize>,
stride: By,
},
Window(Len),
Terminated {
mask: u8,
},
Fill {
cap: Option<usize>,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Presence {
Mask {
field: String,
mask: u64,
},
Flag {
field: String,
},
Bit,
Remaining,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Absence {
ZeroOffset,
ZeroLength {
by: String,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum Collection {
#[default]
Vec,
Boxed,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Segment {
Block(Vec<Field>),
Value {
name: String,
doc: Option<String>,
stated: Option<Stated>,
kind: Kind,
},
Repeat {
name: String,
doc: Option<String>,
stated: Option<Stated>,
element: Kind,
count: Count,
at: Option<String>,
collection: Collection,
},
Placed {
name: String,
doc: Option<String>,
stated: Option<Stated>,
kind: Kind,
at: String,
absent: Option<Absence>,
},
Switch {
name: String,
doc: Option<String>,
stated: Option<Stated>,
on: Discriminant,
choice: String,
window: Option<Len>,
},
Opt {
when: Presence,
field: Field,
},
Checksum {
name: String,
doc: Option<String>,
stated: Option<Stated>,
repr: Scalar,
algorithm: String,
over: Coverage,
},
}
impl Segment {
#[must_use]
pub fn block(fields: Vec<Field>) -> Self {
Segment::Block(fields)
}
#[must_use]
pub fn value(name: &str, kind: Kind) -> Self {
Segment::Value { name: String::from(name), doc: None, stated: None, kind }
}
#[must_use]
pub fn repeat(
name: &str,
element: Kind,
count: Count,
at: Option<&str>,
collection: Collection,
) -> Self {
Segment::Repeat {
name: String::from(name),
doc: None,
stated: None,
element,
count,
at: at.map(String::from),
collection,
}
}
#[must_use]
pub fn placed(name: &str, kind: Kind, at: &str, absent: Option<Absence>) -> Self {
Segment::Placed {
name: String::from(name),
doc: None,
stated: None,
kind,
at: String::from(at),
absent,
}
}
#[must_use]
pub fn switch(name: &str, on: Discriminant, choice: &str, window: Option<Len>) -> Self {
Segment::Switch {
name: String::from(name),
doc: None,
stated: None,
on,
choice: String::from(choice),
window,
}
}
#[must_use]
pub fn opt(when: Presence, field: Field) -> Self {
Segment::Opt { when, field }
}
#[must_use]
pub fn checksum(name: &str, repr: Scalar, algorithm: &str, over: Coverage) -> Self {
Segment::Checksum {
name: String::from(name),
doc: None,
stated: None,
repr,
algorithm: String::from(algorithm),
over,
}
}
#[must_use]
pub fn with_doc(mut self, text: &str) -> Self {
match &mut self {
Segment::Block(_) => {}
Segment::Opt { field, .. } => field.doc = Some(String::from(text)),
Segment::Value { doc, .. }
| Segment::Repeat { doc, .. }
| Segment::Placed { doc, .. }
| Segment::Switch { doc, .. }
| Segment::Checksum { doc, .. } => *doc = Some(String::from(text)),
}
self
}
#[must_use]
pub fn with_stated(mut self, at: Stated) -> Self {
match &mut self {
Segment::Block(_) => {}
Segment::Opt { field, .. } => field.stated = Some(at),
Segment::Value { stated, .. }
| Segment::Repeat { stated, .. }
| Segment::Placed { stated, .. }
| Segment::Switch { stated, .. }
| Segment::Checksum { stated, .. } => *stated = Some(at),
}
self
}
#[must_use]
pub fn places(&self) -> Vec<&str> {
match self {
Segment::Block(fields) => fields.iter().map(|f| f.name.as_str()).collect(),
Segment::Value { name, .. }
| Segment::Repeat { name, .. }
| Segment::Switch { name, .. }
| Segment::Checksum { name, .. } => vec![name.as_str()],
Segment::Placed { name, absent: None, .. } => vec![name.as_str()],
Segment::Placed { absent: Some(_), .. } => Vec::new(),
Segment::Opt { .. } => Vec::new(),
}
}
#[must_use]
pub fn placed_optionally(&self) -> Option<&str> {
match self {
Segment::Opt { field, .. } => Some(field.name.as_str()),
Segment::Placed { name, absent: Some(_), .. } => Some(name.as_str()),
_ => None,
}
}
#[must_use]
pub fn name(&self) -> Option<&str> {
match self {
Segment::Block(_) => None,
Segment::Opt { field, .. } => Some(&field.name),
Segment::Value { name, .. }
| Segment::Repeat { name, .. }
| Segment::Placed { name, .. }
| Segment::Switch { name, .. }
| Segment::Checksum { name, .. } => Some(name),
}
}
#[must_use]
pub fn stated(&self) -> Option<Stated> {
match self {
Segment::Block(_) => None,
Segment::Opt { field, .. } => field.stated,
Segment::Value { stated, .. }
| Segment::Repeat { stated, .. }
| Segment::Placed { stated, .. }
| Segment::Switch { stated, .. }
| Segment::Checksum { stated, .. } => *stated,
}
}
#[must_use]
pub fn fixed_bits(&self) -> Option<u64> {
match self {
Segment::Block(fields) => fields.iter().map(|f| f.kind.bits()).sum(),
Segment::Checksum { repr, .. } => Some(repr.bits()),
Segment::Value { kind, .. } => kind.bits(),
Segment::Repeat { count, at, .. } => match (count, at) {
(Count::Window(Len::Bytes(n)), None) => Some(*n as u64 * 8),
_ => None,
},
Segment::Switch { window: Some(Len::Bytes(n)), .. } => Some(*n as u64 * 8),
Segment::Switch { .. } | Segment::Placed { .. } | Segment::Opt { .. } => None,
}
}
#[must_use]
pub fn open_ended(&self) -> bool {
match self {
Segment::Repeat { count, .. } => matches!(count, Count::Fill { .. }),
Segment::Value { kind, .. } | Segment::Opt { field: Field { kind, .. }, .. } => {
matches!(kind, Kind::Text { len: Len::Fill, .. })
}
Segment::Block(_)
| Segment::Checksum { .. }
| Segment::Switch { .. }
| Segment::Placed { .. } => false,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct Param {
pub name: String,
pub repr: Scalar,
}
impl Param {
#[must_use]
pub fn new(name: &str, repr: Scalar) -> Self {
Self { name: String::from(name), repr }
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct MessageDef {
pub name: String,
pub endian: Endian,
pub bits: Option<BitOrder>,
pub needs: Vec<Param>,
pub segments: Vec<Segment>,
}
impl MessageDef {
#[must_use]
pub fn new(name: &str, segments: Vec<Segment>) -> Self {
Self {
name: String::from(name),
endian: Endian::Le,
bits: None,
needs: Vec::new(),
segments,
}
}
#[must_use]
pub fn with_needs(self, needs: Vec<Param>) -> Self {
Self { needs, ..self }
}
#[must_use]
pub fn with_endian(self, endian: Endian) -> Self {
Self { endian, ..self }
}
#[must_use]
pub fn with_bits(self, order: BitOrder) -> Self {
Self { bits: Some(order), ..self }
}
}
pub fn split_ref(name: &str) -> (&str, Option<&str>) {
match name.split_once('.') {
Some((outer, inner)) => (outer, Some(inner)),
None => (name, None),
}
}