binn_rs/
subtype.rs

1/// Subtype of some type.
2///
3/// Sub types in binn are defined as numbers in range from 0 to 4096 (excluding 4096).
4///
5/// Used to create values of user-defined types:
6/// ```
7/// use binn_rs::{SubType, Value};
8///
9/// const CUSTOM_TEXT: SubType = SubType::new(7);
10///
11/// let value = Value::UserText(CUSTOM_TEXT, "some text that means something");
12/// ```
13#[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    /// Creates new sub type from given value
47    ///
48    /// # Panics
49    ///
50    /// Panics if value is greater than 4095 so it can't
51    /// be a valid subtype
52    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    /// Returns numeric representation of this sub type
61    pub const fn value(&self) -> u16 {
62        self.0
63    }
64}