#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ByteOrder {
LittleEndian,
BigEndian,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Signal {
BeginMessage,
EndMessage,
BeginField,
EndField,
BeginComposite,
EndComposite,
BeginEnum,
EndEnum,
BeginSet,
EndSet,
BeginGroup,
EndGroup,
BeginVarData,
EndVarData,
Encoding,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Presence {
#[default]
Required,
Optional,
Constant,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PrimitiveType {
Char,
Int8,
UInt8,
Int16,
UInt16,
Int32,
UInt32,
Int64,
UInt64,
Float,
Double,
}
impl PrimitiveType {
#[must_use]
pub const fn size(self) -> usize {
match self {
Self::Char | Self::Int8 | Self::UInt8 => 1,
Self::Int16 | Self::UInt16 => 2,
Self::Int32 | Self::UInt32 | Self::Float => 4,
Self::Int64 | Self::UInt64 | Self::Double => 8,
}
}
pub(crate) const fn is_unsigned_int(self) -> bool {
matches!(
self,
Self::Char | Self::UInt8 | Self::UInt16 | Self::UInt32 | Self::UInt64
)
}
pub(crate) const fn is_signed_int(self) -> bool {
matches!(self, Self::Int8 | Self::Int16 | Self::Int32 | Self::Int64)
}
pub(crate) const fn signed_range(self) -> Option<(i64, i64)> {
match self {
Self::Int8 => Some((i8::MIN as i64, i8::MAX as i64)),
Self::Int16 => Some((i16::MIN as i64, i16::MAX as i64)),
Self::Int32 => Some((i32::MIN as i64, i32::MAX as i64)),
Self::Int64 => Some((i64::MIN, i64::MAX)),
_ => None,
}
}
pub(crate) const fn unsigned_max(self) -> Option<u64> {
match self {
Self::Char | Self::UInt8 => Some(u8::MAX as u64),
Self::UInt16 => Some(u16::MAX as u64),
Self::UInt32 => Some(u32::MAX as u64),
Self::UInt64 => Some(u64::MAX),
_ => None,
}
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Encoding {
pub primitive_type: Option<PrimitiveType>,
pub offset: Option<usize>,
pub presence: Presence,
pub since_version: u16,
pub null_value: Option<u64>,
pub character_encoding: Option<String>,
pub semantic_type: Option<String>,
pub min_value: Option<u64>,
pub max_value: Option<u64>,
pub description: Option<String>,
pub constant_value: Option<String>,
pub length: Option<usize>,
pub epoch: Option<String>,
pub time_unit: Option<String>,
pub is_variable_length: bool,
pub deprecated: bool,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Token {
pub id: Option<u16>,
pub name: String,
pub signal: Signal,
pub encoding: Encoding,
pub span: Option<std::ops::Range<usize>>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Ir {
pub package: String,
pub id: u16,
pub version: u16,
pub byte_order: ByteOrder,
pub description: Option<String>,
pub semantic_version: Option<String>,
pub header_type: String,
pub tokens: Vec<Token>,
}