use alloc::format;
use alloc::string::String;
use alloc::vec;
use alloc::vec::Vec;
use layline_core::table::StatedUnit;
use super::Coverage;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct Stated {
pub unit: StatedUnit,
pub pos: u64,
}
impl Stated {
#[must_use]
pub fn new(unit: StatedUnit, pos: u64) -> Self {
Self { unit, pos }
}
#[must_use]
pub fn bit(pos: u64) -> Self {
Self { unit: StatedUnit::Bit, pos }
}
#[must_use]
pub fn byte(pos: u64) -> Self {
Self { unit: StatedUnit::Byte, pos }
}
#[must_use]
pub fn bits(self) -> u64 {
self.pos * self.unit.bits()
}
#[must_use]
pub fn published(self, physical: u64) -> Stated {
if physical.is_multiple_of(self.unit.bits()) {
Stated { unit: self.unit, pos: physical / self.unit.bits() }
} else {
Stated::bit(physical)
}
}
pub(crate) fn check(self, field: &str, at: u64) -> Result<(), String> {
if self.bits() == at {
return Ok(());
}
let noun = self.unit.name();
let landed = if self.unit == StatedUnit::Byte && !at.is_multiple_of(8) {
format!("bit {at} — not a byte boundary")
} else {
format!("{noun} {}", at / self.unit.bits())
};
Err(format!(
"field `{field}`: #[at({noun} = {})] but the solver placed it at {landed}",
self.pos
))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Scalar {
U(u64),
I(u64),
F32,
F64,
Bool,
ByteBool,
}
impl Scalar {
#[must_use]
pub fn bits(self) -> u64 {
match self {
Scalar::U(n) | Scalar::I(n) => n,
Scalar::F32 => 32,
Scalar::F64 => 64,
Scalar::Bool => 1,
Scalar::ByteBool => 8,
}
}
#[must_use]
pub fn primitive(self) -> &'static str {
match self {
Scalar::F32 => "f32",
Scalar::F64 => "f64",
Scalar::Bool | Scalar::ByteBool => "bool",
Scalar::U(n) => match n {
0..=8 => "u8",
9..=16 => "u16",
17..=32 => "u32",
33..=64 => "u64",
_ => "u128",
},
Scalar::I(n) => match n {
0..=8 => "i8",
9..=16 => "i16",
17..=32 => "i32",
33..=64 => "i64",
_ => "i128",
},
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct By {
pub field: String,
pub scale: u32,
pub offset: i32,
}
impl By {
#[must_use]
pub fn field(field: &str) -> Self {
Self { field: String::from(field), scale: 1, offset: 0 }
}
#[must_use]
pub fn new(field: &str, scale: u32, offset: i32) -> Self {
Self { field: String::from(field), scale, offset }
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Len {
Bytes(usize),
Field {
by: By,
cap: Option<usize>,
align: Option<u64>,
},
Until {
terminator: u8,
},
Fill,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Kind {
Scalar(Scalar),
Array(Scalar, Vec<usize>),
Codec {
ty: String,
bits: u64,
},
Nested {
ty: String,
bytes: usize,
},
NestedArray {
ty: String,
bytes: usize,
len: usize,
},
Var {
ty: String,
},
Msg {
ty: String,
boxed: bool,
with: Vec<String>,
},
Checksum {
repr: Scalar,
algorithm: String,
over: Coverage,
},
Text {
len: Len,
codec: Option<String>,
},
}
impl Kind {
#[must_use]
pub fn bytes(n: usize) -> Self {
Kind::array(Scalar::U(8), n)
}
#[must_use]
pub fn array(s: Scalar, len: usize) -> Self {
Kind::Array(s, vec![len])
}
#[must_use]
pub fn checksum(repr: Scalar, algorithm: &str, over: Coverage) -> Self {
Kind::Checksum { repr, algorithm: String::from(algorithm), over }
}
#[must_use]
pub fn bits(&self) -> Option<u64> {
Some(match self {
Kind::Scalar(s) | Kind::Checksum { repr: s, .. } => s.bits(),
Kind::Array(s, dims) => s.bits() * dims.iter().product::<usize>() as u64,
Kind::Codec { bits, .. } => *bits,
Kind::Nested { bytes, .. } => *bytes as u64 * 8,
Kind::NestedArray { bytes, len, .. } => (*bytes * *len) as u64 * 8,
Kind::Text { len: Len::Bytes(n), .. } => *n as u64 * 8,
Kind::Var { .. } | Kind::Msg { .. } | Kind::Text { .. } => return None,
})
}
pub(crate) fn width(&self) -> u64 {
self.bits().unwrap_or(0)
}
#[must_use]
pub fn is_integer(&self) -> bool {
match self {
Kind::Scalar(Scalar::U(_) | Scalar::I(_)) | Kind::Var { .. } | Kind::Codec { .. } => {
true
}
Kind::Scalar(Scalar::F32 | Scalar::F64 | Scalar::Bool | Scalar::ByteBool)
| Kind::Array(..)
| Kind::Nested { .. }
| Kind::NestedArray { .. }
| Kind::Msg { .. }
| Kind::Text { .. }
| Kind::Checksum { .. } => false,
}
}
#[must_use]
pub fn describe(&self) -> &'static str {
match self {
Kind::Scalar(Scalar::U(_) | Scalar::I(_)) => "an integer",
Kind::Scalar(Scalar::F32 | Scalar::F64) => "a float",
Kind::Scalar(Scalar::Bool | Scalar::ByteBool) => "a bool",
Kind::Array(Scalar::U(8), _) => "a run of raw bytes",
Kind::Array(..) => "an array",
Kind::Codec { .. } => "a `FieldCodec` value",
Kind::Nested { .. } => "a nested layout",
Kind::NestedArray { .. } => "an array of nested layouts",
Kind::Var { .. } => "a self-delimiting value",
Kind::Msg { .. } => "a nested message",
Kind::Checksum { .. } => "a check value over the bytes it covers",
Kind::Text { .. } => "text",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct Field {
pub name: String,
pub kind: Kind,
pub doc: Option<String>,
pub stated: Option<Stated>,
pub magic: Option<Vec<u8>>,
pub range: Option<Range>,
pub spare: bool,
pub visibility: Option<String>,
}
impl Field {
#[must_use]
pub fn new(name: &str, kind: Kind) -> Self {
Self {
name: String::from(name),
kind,
doc: None,
stated: None,
magic: None,
range: None,
spare: false,
visibility: None,
}
}
#[must_use]
pub fn with_spare(self) -> Self {
Self { spare: true, ..self }
}
#[must_use]
pub fn with_visibility(self, visibility: &str) -> Self {
Self { visibility: Some(String::from(visibility)), ..self }
}
#[must_use]
pub fn with_doc(self, doc: &str) -> Self {
Self { doc: Some(String::from(doc)), ..self }
}
#[must_use]
pub fn with_stated(self, stated: Stated) -> Self {
Self { stated: Some(stated), ..self }
}
#[must_use]
pub fn with_magic(self, bytes: &[u8]) -> Self {
Self { magic: Some(bytes.to_vec()), ..self }
}
#[must_use]
pub fn with_range(self, range: Range) -> Self {
Self { range: Some(range), ..self }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub struct Range {
pub lo: Option<i64>,
pub hi: Option<i64>,
}
impl Range {
#[must_use]
pub fn new(lo: Option<i64>, hi: Option<i64>) -> Self {
Self { lo, hi }
}
}