1use alloc::format;
4use alloc::string::String;
5use alloc::vec;
6use alloc::vec::Vec;
7
8use layline_core::table::StatedUnit;
9
10use super::Coverage;
11
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
17#[non_exhaustive]
18pub struct Stated {
19 pub unit: StatedUnit,
21 pub pos: u64,
23}
24
25impl Stated {
26 #[must_use]
28 pub fn new(unit: StatedUnit, pos: u64) -> Self {
29 Self { unit, pos }
30 }
31
32 #[must_use]
34 pub fn bit(pos: u64) -> Self {
35 Self { unit: StatedUnit::Bit, pos }
36 }
37
38 #[must_use]
40 pub fn byte(pos: u64) -> Self {
41 Self { unit: StatedUnit::Byte, pos }
42 }
43
44 #[must_use]
46 pub fn bits(self) -> u64 {
47 self.pos * self.unit.bits()
48 }
49
50 #[must_use]
54 pub fn published(self, physical: u64) -> Stated {
55 if physical.is_multiple_of(self.unit.bits()) {
56 Stated { unit: self.unit, pos: physical / self.unit.bits() }
57 } else {
58 Stated::bit(physical)
59 }
60 }
61
62 pub(crate) fn check(self, field: &str, at: u64) -> Result<(), String> {
68 if self.bits() == at {
69 return Ok(());
70 }
71 let noun = self.unit.name();
72 let landed = if self.unit == StatedUnit::Byte && !at.is_multiple_of(8) {
73 format!("bit {at} — not a byte boundary")
74 } else {
75 format!("{noun} {}", at / self.unit.bits())
76 };
77 Err(format!(
78 "field `{field}`: #[at({noun} = {})] but the solver placed it at {landed}",
79 self.pos
80 ))
81 }
82}
83
84#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
86#[non_exhaustive]
87pub enum Scalar {
88 U(u64),
90 I(u64),
92 F32,
94 F64,
96 Bool,
98}
99
100impl Scalar {
101 #[must_use]
103 pub fn bits(self) -> u64 {
104 match self {
105 Scalar::U(n) | Scalar::I(n) => n,
106 Scalar::F32 => 32,
107 Scalar::F64 => 64,
108 Scalar::Bool => 1,
109 }
110 }
111
112 #[must_use]
114 pub fn primitive(self) -> &'static str {
115 match self {
116 Scalar::F32 => "f32",
117 Scalar::F64 => "f64",
118 Scalar::Bool => "bool",
119 Scalar::U(n) => match n {
120 0..=8 => "u8",
121 9..=16 => "u16",
122 17..=32 => "u32",
123 33..=64 => "u64",
124 _ => "u128",
125 },
126 Scalar::I(n) => match n {
127 0..=8 => "i8",
128 9..=16 => "i16",
129 17..=32 => "i32",
130 33..=64 => "i64",
131 _ => "i128",
132 },
133 }
134 }
135}
136
137#[derive(Debug, Clone, PartialEq, Eq, Hash)]
141#[non_exhaustive]
142pub struct By {
143 pub field: String,
145 pub scale: u32,
147 pub offset: i32,
149}
150
151impl By {
152 #[must_use]
154 pub fn field(field: &str) -> Self {
155 Self { field: String::from(field), scale: 1, offset: 0 }
156 }
157
158 #[must_use]
160 pub fn new(field: &str, scale: u32, offset: i32) -> Self {
161 Self { field: String::from(field), scale, offset }
162 }
163}
164
165#[derive(Debug, Clone, PartialEq, Eq, Hash)]
169#[non_exhaustive]
170pub enum Len {
171 Bytes(usize),
173 Field {
175 by: By,
177 cap: Option<usize>,
179 },
180 Until {
182 terminator: u8,
184 },
185 Fill,
187}
188
189#[derive(Debug, Clone, PartialEq, Eq, Hash)]
191#[non_exhaustive]
192pub enum Kind {
193 Scalar(Scalar),
195 Array(Scalar, Vec<usize>),
197 Codec {
199 ty: String,
201 bits: u64,
203 },
204 Nested {
206 ty: String,
208 bytes: usize,
210 },
211 NestedArray {
213 ty: String,
215 bytes: usize,
217 len: usize,
219 },
220 Var {
222 ty: String,
224 },
225 Msg {
227 ty: String,
229 boxed: bool,
231 with: Vec<String>,
233 },
234 Checksum {
238 repr: Scalar,
240 algorithm: String,
242 over: Coverage,
244 },
245 Text {
247 len: Len,
249 codec: Option<String>,
251 },
252}
253
254impl Kind {
255 #[must_use]
259 pub fn bytes(n: usize) -> Self {
260 Kind::array(Scalar::U(8), n)
261 }
262
263 #[must_use]
265 pub fn array(s: Scalar, len: usize) -> Self {
266 Kind::Array(s, vec![len])
267 }
268
269 #[must_use]
271 pub fn checksum(repr: Scalar, algorithm: &str, over: Coverage) -> Self {
272 Kind::Checksum { repr, algorithm: String::from(algorithm), over }
273 }
274
275 #[must_use]
277 pub fn bits(&self) -> Option<u64> {
278 Some(match self {
279 Kind::Scalar(s) | Kind::Checksum { repr: s, .. } => s.bits(),
280 Kind::Array(s, dims) => s.bits() * dims.iter().product::<usize>() as u64,
281 Kind::Codec { bits, .. } => *bits,
282 Kind::Nested { bytes, .. } => *bytes as u64 * 8,
283 Kind::NestedArray { bytes, len, .. } => (*bytes * *len) as u64 * 8,
284 Kind::Text { len: Len::Bytes(n), .. } => *n as u64 * 8,
285 Kind::Var { .. } | Kind::Msg { .. } | Kind::Text { .. } => return None,
286 })
287 }
288
289 pub(crate) fn width(&self) -> u64 {
291 self.bits().unwrap_or(0)
292 }
293
294 #[must_use]
296 pub fn is_integer(&self) -> bool {
297 match self {
298 Kind::Scalar(Scalar::U(_) | Scalar::I(_)) | Kind::Var { .. } | Kind::Codec { .. } => {
299 true
300 }
301 Kind::Scalar(Scalar::F32 | Scalar::F64 | Scalar::Bool)
302 | Kind::Array(..)
303 | Kind::Nested { .. }
304 | Kind::NestedArray { .. }
305 | Kind::Msg { .. }
306 | Kind::Text { .. }
307 | Kind::Checksum { .. } => false,
308 }
309 }
310
311 #[must_use]
313 pub fn describe(&self) -> &'static str {
314 match self {
315 Kind::Scalar(Scalar::U(_) | Scalar::I(_)) => "an integer",
316 Kind::Scalar(Scalar::F32 | Scalar::F64) => "a float",
317 Kind::Scalar(Scalar::Bool) => "a bool",
318 Kind::Array(Scalar::U(8), _) => "a run of raw bytes",
319 Kind::Array(..) => "an array",
320 Kind::Codec { .. } => "a `FieldCodec` value",
321 Kind::Nested { .. } => "a nested layout",
322 Kind::NestedArray { .. } => "an array of nested layouts",
323 Kind::Var { .. } => "a self-delimiting value",
324 Kind::Msg { .. } => "a nested message",
325 Kind::Checksum { .. } => "a check value over the bytes it covers",
326 Kind::Text { .. } => "text",
327 }
328 }
329}
330
331#[derive(Debug, Clone, PartialEq, Eq)]
333#[non_exhaustive]
334pub struct Field {
335 pub name: String,
337 pub kind: Kind,
339 pub doc: Option<String>,
341 pub stated: Option<Stated>,
343 pub magic: Option<Vec<u8>>,
345 pub range: Option<Range>,
347 pub spare: bool,
352 pub visibility: Option<String>,
357}
358
359impl Field {
360 #[must_use]
362 pub fn new(name: &str, kind: Kind) -> Self {
363 Self {
364 name: String::from(name),
365 kind,
366 doc: None,
367 stated: None,
368 magic: None,
369 range: None,
370 spare: false,
371 visibility: None,
372 }
373 }
374
375 #[must_use]
377 pub fn with_spare(self) -> Self {
378 Self { spare: true, ..self }
379 }
380
381 #[must_use]
383 pub fn with_visibility(self, visibility: &str) -> Self {
384 Self { visibility: Some(String::from(visibility)), ..self }
385 }
386
387 #[must_use]
389 pub fn with_doc(self, doc: &str) -> Self {
390 Self { doc: Some(String::from(doc)), ..self }
391 }
392
393 #[must_use]
395 pub fn with_stated(self, stated: Stated) -> Self {
396 Self { stated: Some(stated), ..self }
397 }
398
399 #[must_use]
401 pub fn with_magic(self, bytes: &[u8]) -> Self {
402 Self { magic: Some(bytes.to_vec()), ..self }
403 }
404
405 #[must_use]
407 pub fn with_range(self, range: Range) -> Self {
408 Self { range: Some(range), ..self }
409 }
410}
411
412#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
414#[non_exhaustive]
415pub struct Range {
416 pub lo: Option<i64>,
418 pub hi: Option<i64>,
420}
421
422impl Range {
423 #[must_use]
425 pub fn new(lo: Option<i64>, hi: Option<i64>) -> Self {
426 Self { lo, hi }
427 }
428}