roussillon_type_system/value/
byte.rs

1use std::cell::RefCell;
2use std::rc::Rc;
3use std::{u64, usize};
4use crate::parse::{parse_slice, Parsed};
5
6use crate::types::concept::Type;
7use crate::types::primitive::Primitive;
8use crate::value::concept::{DataValue, GetDataValue, ValueCell};
9
10#[derive(Copy, Clone, Debug, Default)]
11pub struct Byte(u8);
12impl Byte {
13    pub fn new(from: u8) -> Self { Self(from) }
14}
15
16impl GetDataValue<u8> for Byte {
17    fn get(&self) -> u8 { self.0 }
18    fn from_raw(raw: &[u8]) -> Self { Self(u8::from_be_bytes(raw.try_into().unwrap())) }
19}
20
21impl DataValue for Byte {
22    fn data_type(&self) -> Type { Primitive::Byte.to_rc() }
23    fn raw(&self) -> Vec<u8> { self.0.to_be_bytes().to_vec() }
24    fn set(&mut self, raw: &[u8]) { *self = Self::from_raw(raw) }
25}
26
27#[derive(Copy, Clone, Debug, Default)]
28pub struct Word(u16);
29impl Word {
30    pub fn new(from: u16) -> Self { Self(from) }
31}
32
33impl GetDataValue<u16> for Word {
34    fn get(&self) -> u16 { self.0 }
35    fn from_raw(raw: &[u8]) -> Self { Self(u16::from_be_bytes(raw.try_into().unwrap())) }
36}
37impl DataValue for Word {
38    fn data_type(&self) -> Type { Primitive::Bytes(2).to_rc() }
39    fn raw(&self) -> Vec<u8> { self.0.to_be_bytes().to_vec() }
40    fn set(&mut self, raw: &[u8]) { *self = Self::from_raw(raw) }
41}
42
43#[derive(Copy, Clone, Debug, Default)]
44pub struct Quad(u32);
45impl Quad {
46    pub fn new(from: u32) -> Self { Self(from) }
47}
48impl GetDataValue<u32> for Quad {
49    fn get(&self) -> u32 { self.0 }
50    fn from_raw(raw: &[u8]) -> Self { Self(u32::from_be_bytes(raw.try_into().unwrap())) }
51}
52impl DataValue for Quad {
53    fn data_type(&self) -> Type { Primitive::Bytes(4).to_rc() }
54    fn raw(&self) -> Vec<u8> { self.0.to_be_bytes().to_vec() }
55    fn set(&mut self, raw: &[u8]) { *self = Self::from_raw(raw) }
56}
57
58#[derive(Copy, Clone, Debug, Default)]
59pub struct Long(u64);
60impl Long {
61    pub fn new(from: u64) -> Self { Self(from) }
62}
63impl GetDataValue<u64> for Long {
64    fn get(&self) -> u64 { self.0 }
65    fn from_raw(raw: &[u8]) -> Self { Self(u64::from_be_bytes(raw.try_into().unwrap())) }
66}
67impl DataValue for Long {
68    fn data_type(&self) -> Type { Primitive::Bytes(8).to_rc() }
69    fn raw(&self) -> Vec<u8> { self.0.to_be_bytes().to_vec() }
70    fn set(&mut self, raw: &[u8]) { *self = Self::from_raw(raw) }
71}
72
73#[derive(Copy, Clone, Debug, Default)]
74pub struct Wide(u128);
75impl Wide {
76    pub fn new(from: u128) -> Self { Self(from) }
77}
78impl GetDataValue<u128> for Wide {
79    fn get(&self) -> u128 { self.0 }
80    fn from_raw(raw: &[u8]) -> Self { Self(u128::from_be_bytes(raw.try_into().unwrap())) }
81}
82impl DataValue for Wide {
83    fn data_type(&self) -> Type { Primitive::Bytes(16).to_rc() }
84    fn raw(&self) -> Vec<u8> { self.0.to_be_bytes().to_vec() }
85    fn set(&mut self, raw: &[u8]) { *self = Self::from_raw(raw) }
86}
87
88#[derive(Copy, Clone, Debug, Default)]
89pub struct Arch(usize);
90impl Arch {
91    pub fn new(from: usize) -> Self { Self(from) }
92    pub const fn size_of() -> usize { std::mem::size_of::<usize>() }
93}
94impl GetDataValue<usize> for Arch {
95    fn get(&self) -> usize { self.0 }
96    fn from_raw(raw: &[u8]) -> Self { Self(usize::from_be_bytes(raw.try_into().unwrap())) }
97}
98impl DataValue for Arch {
99    fn data_type(&self) -> Type { Primitive::Bytes(Self::size_of()).to_rc() }
100    fn raw(&self) -> Vec<u8> { self.0.to_be_bytes().to_vec() }
101    fn set(&mut self, raw: &[u8]) { *self = Self::from_raw(raw) }
102}
103
104#[derive(Clone, Debug)]
105pub enum Bytes {
106    Byte(Byte),
107    Word(Word),
108    Quad(Quad),
109    Long(Long),
110    Wide(Wide),
111    Arch(Arch),
112    Bytes(Vec<u8>, usize),
113}
114
115#[derive(Debug)]
116pub struct CannotCreateArchWithGivenSize(pub usize);
117
118impl Bytes {
119    pub fn parse(input: &[u8], size: usize) -> Parsed<Self> {
120        let (Some(raw), rest) = parse_slice(input, size) else { return (None, input); };
121        (Some(Self::from_raw(raw)), rest)
122    }
123
124    pub fn try_from_arch(raw: &[u8]) -> Result<Self, CannotCreateArchWithGivenSize> {
125        if raw.len() == Arch::size_of() {
126            Ok(Self::Arch(Arch::from_raw(raw)))
127        } else {
128            Err(CannotCreateArchWithGivenSize(raw.len()))
129        }
130    }
131
132    pub fn to_cell(self) -> ValueCell { Rc::new(RefCell::new(self)) }
133}
134impl GetDataValue<Vec<u8>> for Bytes {
135    fn get(&self) -> Vec<u8> {
136        match self {
137            Bytes::Byte(v) => vec![v.get()],
138            Bytes::Word(v) => v.get().to_be_bytes().to_vec(),
139            Bytes::Quad(v) => v.get().to_be_bytes().to_vec(),
140            Bytes::Long(v) => v.get().to_be_bytes().to_vec(),
141            Bytes::Wide(v) => v.get().to_be_bytes().to_vec(),
142            Bytes::Arch(v) => v.get().to_be_bytes().to_vec(),
143            Bytes::Bytes(b, _) => b.to_vec(),
144        }
145    }
146
147    fn from_raw(raw: &[u8]) -> Self {
148        if raw.len() == Arch::size_of() {
149            return Self::try_from_arch(raw).unwrap();
150        }
151        match raw.len() {
152            1 => Self::Byte(Byte::from_raw(raw)),
153            2 => Self::Word(Word::from_raw(raw)),
154            4 => Self::Quad(Quad::from_raw(raw)),
155            8 => Self::Long(Long::from_raw(raw)),
156            16 => Self::Wide(Wide::from_raw(raw)),
157            l => Self::Bytes(raw.to_vec(), l)
158        }
159    }
160}
161
162impl DataValue for Bytes {
163    fn data_type(&self) -> Type {
164        match self {
165            Bytes::Byte(_) => Primitive::Byte,
166            Bytes::Arch(_) => Primitive::Bytes(std::mem::size_of::<usize>()),
167            Bytes::Word(_) => Primitive::Bytes(2),
168            Bytes::Quad(_) => Primitive::Bytes(4),
169            Bytes::Long(_) => Primitive::Bytes(8),
170            Bytes::Wide(_) => Primitive::Bytes(16),
171            Bytes::Bytes(_, l) => Primitive::Bytes(*l),
172        }.to_rc()
173    }
174
175    fn raw(&self) -> Vec<u8> {
176        match self {
177            Bytes::Byte(b) => b.raw(),
178            Bytes::Arch(a) => a.raw(),
179            Bytes::Word(w) => w.raw(),
180            Bytes::Quad(q) => q.raw(),
181            Bytes::Long(l) => l.raw(),
182            Bytes::Wide(w) => w.raw(),
183            Bytes::Bytes(b, _) => b.to_vec(),
184        }
185    }
186
187    fn set(&mut self, raw: &[u8]) {
188        *self = Self::from_raw(raw);
189    }
190}