use alloc::string::String;
use alloc::vec::Vec;
use super::Derive;
use super::{Endian, Segment};
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Discriminant {
Field(String),
BodyLength,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct Arm {
pub value: u64,
pub body: Vec<Segment>,
pub name: String,
pub doc: Option<String>,
}
impl Arm {
#[must_use]
pub fn new(value: u64, name: &str, body: Vec<Segment>) -> Self {
Self { value, body, name: String::from(name), doc: None }
}
#[must_use]
pub fn with_doc(self, doc: &str) -> Self {
Self { doc: Some(String::from(doc)), ..self }
}
#[must_use]
pub fn open_ended(&self) -> bool {
self.body.iter().any(Segment::open_ended)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct ChoiceDef {
pub name: String,
pub endian: Endian,
pub arms: Vec<Arm>,
pub other: Option<String>,
pub other_bytes: Option<usize>,
pub doc: Option<String>,
pub derives: Vec<Derive>,
}
impl ChoiceDef {
#[must_use]
pub fn new(name: &str, arms: Vec<Arm>) -> Self {
Self {
name: String::from(name),
endian: Endian::Le,
arms,
other: None,
other_bytes: None,
doc: None,
derives: Vec::new(),
}
}
#[must_use]
pub fn with_derives(self, derives: Vec<Derive>) -> Self {
Self { derives, ..self }
}
#[must_use]
pub fn with_endian(self, endian: Endian) -> Self {
Self { endian, ..self }
}
#[must_use]
pub fn with_other(self, other: &str) -> Self {
Self { other: Some(String::from(other)), ..self }
}
#[must_use]
pub fn with_other_bytes(self, bytes: usize) -> Self {
Self { other_bytes: Some(bytes), ..self }
}
#[must_use]
pub fn with_doc(self, doc: &str) -> Self {
Self { doc: Some(String::from(doc)), ..self }
}
#[must_use]
pub fn open_ended(&self) -> bool {
(self.other.is_some() && self.other_bytes.is_none())
|| self.arms.iter().any(Arm::open_ended)
}
}