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 _ => "u64",
124 },
125 Scalar::I(n) => match n {
126 0..=8 => "i8",
127 9..=16 => "i16",
128 17..=32 => "i32",
129 _ => "i64",
130 },
131 }
132 }
133}
134
135#[derive(Debug, Clone, PartialEq, Eq, Hash)]
139#[non_exhaustive]
140pub struct By {
141 pub field: String,
143 pub scale: u32,
145 pub offset: i32,
147}
148
149impl By {
150 #[must_use]
152 pub fn field(field: &str) -> Self {
153 Self { field: String::from(field), scale: 1, offset: 0 }
154 }
155
156 #[must_use]
158 pub fn new(field: &str, scale: u32, offset: i32) -> Self {
159 Self { field: String::from(field), scale, offset }
160 }
161}
162
163#[derive(Debug, Clone, PartialEq, Eq, Hash)]
167#[non_exhaustive]
168pub enum Len {
169 Bytes(usize),
171 Field {
173 by: By,
175 cap: Option<usize>,
177 },
178 Until {
180 terminator: u8,
182 },
183 Fill,
185}
186
187#[derive(Debug, Clone, PartialEq, Eq, Hash)]
189#[non_exhaustive]
190pub enum Kind {
191 Scalar(Scalar),
193 Array(Scalar, Vec<usize>),
195 Codec {
197 ty: String,
199 bits: u64,
201 },
202 Nested {
204 ty: String,
206 bytes: usize,
208 },
209 NestedArray {
211 ty: String,
213 bytes: usize,
215 len: usize,
217 },
218 Var {
220 ty: String,
222 },
223 Msg {
225 ty: String,
227 boxed: bool,
229 with: Vec<String>,
231 },
232 Checksum {
236 repr: Scalar,
238 algorithm: String,
240 over: Coverage,
242 },
243 Text {
245 len: Len,
247 codec: Option<String>,
249 },
250}
251
252impl Kind {
253 #[must_use]
257 pub fn bytes(n: usize) -> Self {
258 Kind::array(Scalar::U(8), n)
259 }
260
261 #[must_use]
263 pub fn array(s: Scalar, len: usize) -> Self {
264 Kind::Array(s, vec![len])
265 }
266
267 #[must_use]
269 pub fn checksum(repr: Scalar, algorithm: &str, over: Coverage) -> Self {
270 Kind::Checksum { repr, algorithm: String::from(algorithm), over }
271 }
272
273 #[must_use]
275 pub fn bits(&self) -> Option<u64> {
276 Some(match self {
277 Kind::Scalar(s) | Kind::Checksum { repr: s, .. } => s.bits(),
278 Kind::Array(s, dims) => s.bits() * dims.iter().product::<usize>() as u64,
279 Kind::Codec { bits, .. } => *bits,
280 Kind::Nested { bytes, .. } => *bytes as u64 * 8,
281 Kind::NestedArray { bytes, len, .. } => (*bytes * *len) as u64 * 8,
282 Kind::Text { len: Len::Bytes(n), .. } => *n as u64 * 8,
283 Kind::Var { .. } | Kind::Msg { .. } | Kind::Text { .. } => return None,
284 })
285 }
286
287 pub(crate) fn width(&self) -> u64 {
289 self.bits().unwrap_or(0)
290 }
291
292 #[must_use]
294 pub fn is_integer(&self) -> bool {
295 match self {
296 Kind::Scalar(Scalar::U(_) | Scalar::I(_)) | Kind::Var { .. } | Kind::Codec { .. } => {
297 true
298 }
299 Kind::Scalar(Scalar::F32 | Scalar::F64 | Scalar::Bool)
300 | Kind::Array(..)
301 | Kind::Nested { .. }
302 | Kind::NestedArray { .. }
303 | Kind::Msg { .. }
304 | Kind::Text { .. }
305 | Kind::Checksum { .. } => false,
306 }
307 }
308
309 #[must_use]
311 pub fn describe(&self) -> &'static str {
312 match self {
313 Kind::Scalar(Scalar::U(_) | Scalar::I(_)) => "an integer",
314 Kind::Scalar(Scalar::F32 | Scalar::F64) => "a float",
315 Kind::Scalar(Scalar::Bool) => "a bool",
316 Kind::Array(Scalar::U(8), _) => "a run of raw bytes",
317 Kind::Array(..) => "an array",
318 Kind::Codec { .. } => "a `FieldCodec` value",
319 Kind::Nested { .. } => "a nested layout",
320 Kind::NestedArray { .. } => "an array of nested layouts",
321 Kind::Var { .. } => "a self-delimiting value",
322 Kind::Msg { .. } => "a nested message",
323 Kind::Checksum { .. } => "a check value over the bytes it covers",
324 Kind::Text { .. } => "text",
325 }
326 }
327}
328
329#[derive(Debug, Clone, PartialEq, Eq)]
331#[non_exhaustive]
332pub struct Field {
333 pub name: String,
335 pub kind: Kind,
337 pub doc: Option<String>,
339 pub stated: Option<Stated>,
341 pub magic: Option<Vec<u8>>,
343 pub range: Option<Range>,
345 pub visibility: Option<String>,
350}
351
352impl Field {
353 #[must_use]
355 pub fn new(name: &str, kind: Kind) -> Self {
356 Self {
357 name: String::from(name),
358 kind,
359 doc: None,
360 stated: None,
361 magic: None,
362 range: None,
363 visibility: None,
364 }
365 }
366
367 #[must_use]
369 pub fn with_visibility(self, visibility: &str) -> Self {
370 Self { visibility: Some(String::from(visibility)), ..self }
371 }
372
373 #[must_use]
375 pub fn with_doc(self, doc: &str) -> Self {
376 Self { doc: Some(String::from(doc)), ..self }
377 }
378
379 #[must_use]
381 pub fn with_stated(self, stated: Stated) -> Self {
382 Self { stated: Some(stated), ..self }
383 }
384
385 #[must_use]
387 pub fn with_magic(self, bytes: &[u8]) -> Self {
388 Self { magic: Some(bytes.to_vec()), ..self }
389 }
390
391 #[must_use]
393 pub fn with_range(self, range: Range) -> Self {
394 Self { range: Some(range), ..self }
395 }
396}
397
398#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
400#[non_exhaustive]
401pub struct Range {
402 pub lo: Option<i64>,
404 pub hi: Option<i64>,
406}
407
408impl Range {
409 #[must_use]
411 pub fn new(lo: Option<i64>, hi: Option<i64>) -> Self {
412 Self { lo, hi }
413 }
414}