ironwork_rt/lir/
collating.rs1use crate::{codec_enum, codec_struct};
5
6#[derive(Clone, Debug, PartialEq, Eq)]
9pub enum Collating {
10 Native,
12 Sequence(Sequence),
13}
14
15#[derive(Clone, Debug, PartialEq, Eq)]
18pub struct Sequence {
19 pub positions: Box<[u8; 256]>,
20 pub characters: Vec<u8>,
21 pub high_value: u8,
23 pub low_value: u8,
24}
25
26codec_enum!(Collating { Native = 0, Sequence(sequence) = 1 });
27codec_struct!(Sequence { positions, characters, high_value, low_value } check sequence_valid);
28
29fn sequence_valid(s: &Sequence) -> Result<(), String> {
31 let count = s.characters.len();
32 if let Some(b) = (0..=255u8).find(|&b| usize::from(s.positions[usize::from(b)]) >= count) {
33 return Err(format!("X'{b:02X}' at position {} of a sequence of {count}", s.positions[usize::from(b)]));
34 }
35 if let Some((k, &c)) = s.characters.iter().enumerate().find(|&(k, &c)| usize::from(s.positions[usize::from(c)]) != k) {
36 return Err(format!("character {k} of the sequence, X'{c:02X}', is at position {}", s.positions[usize::from(c)]));
37 }
38 let at = |b: u8| usize::from(s.positions[usize::from(b)]);
39 if at(s.high_value) + 1 != count || at(s.low_value) != 0 {
40 return Err(format!("HIGH-VALUE X'{:02X}' or LOW-VALUE X'{:02X}' is not at the sequence's end", s.high_value, s.low_value));
41 }
42 Ok(())
43}