1#[derive(Clone, Copy, Debug, Eq, PartialEq)]
14pub struct SubType(pub(crate) u16);
15
16macro_rules! impl_try_from {
17 ($name:ident) => {
18 impl TryFrom<$name> for SubType {
19 type Error = crate::error::OutOfRangeError;
20
21 fn try_from(value: $name) -> Result<Self, Self::Error> {
22 if (0..4096).contains(&value) {
23 Ok(Self(value as u16))
24 } else {
25 Err(crate::error::OutOfRangeError)
26 }
27 }
28 }
29 };
30}
31
32impl_try_from!(u16);
33impl_try_from!(i16);
34impl_try_from!(u32);
35impl_try_from!(i32);
36impl_try_from!(u64);
37impl_try_from!(i64);
38
39impl From<u8> for SubType {
40 fn from(value: u8) -> Self {
41 Self(value as u16)
42 }
43}
44
45impl SubType {
46 pub const fn new(value: u16) -> Self {
53 if value < 4096 {
54 Self(value)
55 } else {
56 panic!("Number is out of range");
57 }
58 }
59
60 pub const fn value(&self) -> u16 {
62 self.0
63 }
64}