pub struct TypeId;
impl TypeId {
pub const NESTED_TYPE: u8 = 1;
pub const INT8: u8 = 2;
pub const UINT8: u8 = 3;
pub const INT16: u8 = 4;
pub const UINT16: u8 = 5;
pub const INT32: u8 = 6;
pub const UINT32: u8 = 7;
pub const INT64: u8 = 8;
pub const UINT64: u8 = 9;
pub const FLOAT32: u8 = 10;
pub const FLOAT64: u8 = 11;
pub const BOOL: u8 = 15;
pub const BYTE: u8 = 16;
pub const STRING: u8 = 17;
pub const WSTRING: u8 = 18;
pub const FIXED_STRING: u8 = 19;
pub const FIXED_WSTRING: u8 = 20;
pub const BOUNDED_STRING: u8 = 21;
pub const BOUNDED_WSTRING: u8 = 22;
pub const NESTED_TYPE_ARRAY: u8 = 49;
pub const INT8_ARRAY: u8 = 50;
pub const UINT8_ARRAY: u8 = 51;
pub const INT16_ARRAY: u8 = 52;
pub const UINT16_ARRAY: u8 = 53;
pub const INT32_ARRAY: u8 = 54;
pub const UINT32_ARRAY: u8 = 55;
pub const INT64_ARRAY: u8 = 56;
pub const UINT64_ARRAY: u8 = 57;
pub const FLOAT32_ARRAY: u8 = 58;
pub const FLOAT64_ARRAY: u8 = 59;
pub const BOOL_ARRAY: u8 = 63;
pub const STRING_ARRAY: u8 = 65;
pub const NESTED_TYPE_BOUNDED_SEQUENCE: u8 = 97;
pub const INT8_BOUNDED_SEQUENCE: u8 = 98;
pub const UINT8_BOUNDED_SEQUENCE: u8 = 99;
pub const INT16_BOUNDED_SEQUENCE: u8 = 100;
pub const UINT16_BOUNDED_SEQUENCE: u8 = 101;
pub const INT32_BOUNDED_SEQUENCE: u8 = 102;
pub const UINT32_BOUNDED_SEQUENCE: u8 = 103;
pub const INT64_BOUNDED_SEQUENCE: u8 = 104;
pub const UINT64_BOUNDED_SEQUENCE: u8 = 105;
pub const FLOAT32_BOUNDED_SEQUENCE: u8 = 106;
pub const FLOAT64_BOUNDED_SEQUENCE: u8 = 107;
pub const BOOL_BOUNDED_SEQUENCE: u8 = 111;
pub const STRING_BOUNDED_SEQUENCE: u8 = 113;
pub const NESTED_TYPE_UNBOUNDED_SEQUENCE: u8 = 145;
pub const INT8_UNBOUNDED_SEQUENCE: u8 = 146;
pub const UINT8_UNBOUNDED_SEQUENCE: u8 = 147;
pub const INT16_UNBOUNDED_SEQUENCE: u8 = 148;
pub const UINT16_UNBOUNDED_SEQUENCE: u8 = 149;
pub const INT32_UNBOUNDED_SEQUENCE: u8 = 150;
pub const UINT32_UNBOUNDED_SEQUENCE: u8 = 151;
pub const INT64_UNBOUNDED_SEQUENCE: u8 = 152;
pub const UINT64_UNBOUNDED_SEQUENCE: u8 = 153;
pub const FLOAT32_UNBOUNDED_SEQUENCE: u8 = 154;
pub const FLOAT64_UNBOUNDED_SEQUENCE: u8 = 155;
pub const BOOL_UNBOUNDED_SEQUENCE: u8 = 159;
pub const STRING_UNBOUNDED_SEQUENCE: u8 = 161;
pub const ARRAY_OFFSET: u8 = 48;
pub const BOUNDED_SEQUENCE_OFFSET: u8 = 96;
pub const UNBOUNDED_SEQUENCE_OFFSET: u8 = 144;
pub const fn is_nested(type_id: u8) -> bool {
type_id == Self::NESTED_TYPE
|| type_id == Self::NESTED_TYPE_ARRAY
|| type_id == Self::NESTED_TYPE_BOUNDED_SEQUENCE
|| type_id == Self::NESTED_TYPE_UNBOUNDED_SEQUENCE
}
pub const fn is_array(type_id: u8) -> bool {
type_id >= 49 && type_id <= 65
}
pub const fn is_bounded_sequence(type_id: u8) -> bool {
type_id >= 97 && type_id <= 113
}
pub const fn is_unbounded_sequence(type_id: u8) -> bool {
type_id >= 145 && type_id <= 161
}
pub const fn is_single(type_id: u8) -> bool {
type_id >= 1 && type_id <= 17
}
pub const fn base_type(type_id: u8) -> u8 {
if Self::is_unbounded_sequence(type_id) {
type_id - Self::UNBOUNDED_SEQUENCE_OFFSET
} else if Self::is_bounded_sequence(type_id) {
type_id - Self::BOUNDED_SEQUENCE_OFFSET
} else if Self::is_array(type_id) {
type_id - Self::ARRAY_OFFSET
} else {
type_id
}
}
pub const fn type_name(type_id: u8) -> Option<&'static str> {
match Self::base_type(type_id) {
Self::NESTED_TYPE => Some("nested"),
Self::INT8 => Some("int8"),
Self::UINT8 => Some("uint8"),
Self::INT16 => Some("int16"),
Self::UINT16 => Some("uint16"),
Self::INT32 => Some("int32"),
Self::UINT32 => Some("uint32"),
Self::INT64 => Some("int64"),
Self::UINT64 => Some("uint64"),
Self::FLOAT32 => Some("float32"),
Self::FLOAT64 => Some("float64"),
Self::BOOL => Some("bool"),
Self::STRING => Some("string"),
_ => None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_type_id_offsets() {
assert_eq!(TypeId::INT32_ARRAY, TypeId::INT32 + TypeId::ARRAY_OFFSET);
assert_eq!(
TypeId::INT32_BOUNDED_SEQUENCE,
TypeId::INT32 + TypeId::BOUNDED_SEQUENCE_OFFSET
);
assert_eq!(
TypeId::INT32_UNBOUNDED_SEQUENCE,
TypeId::INT32 + TypeId::UNBOUNDED_SEQUENCE_OFFSET
);
}
#[test]
fn test_is_nested() {
assert!(TypeId::is_nested(TypeId::NESTED_TYPE));
assert!(TypeId::is_nested(TypeId::NESTED_TYPE_ARRAY));
assert!(TypeId::is_nested(TypeId::NESTED_TYPE_BOUNDED_SEQUENCE));
assert!(TypeId::is_nested(TypeId::NESTED_TYPE_UNBOUNDED_SEQUENCE));
assert!(!TypeId::is_nested(TypeId::INT32));
}
#[test]
fn test_is_array() {
assert!(TypeId::is_array(TypeId::INT32_ARRAY));
assert!(TypeId::is_array(TypeId::STRING_ARRAY));
assert!(!TypeId::is_array(TypeId::INT32));
assert!(!TypeId::is_array(TypeId::INT32_UNBOUNDED_SEQUENCE));
}
#[test]
fn test_base_type() {
assert_eq!(TypeId::base_type(TypeId::INT32), TypeId::INT32);
assert_eq!(TypeId::base_type(TypeId::INT32_ARRAY), TypeId::INT32);
assert_eq!(
TypeId::base_type(TypeId::INT32_BOUNDED_SEQUENCE),
TypeId::INT32
);
assert_eq!(
TypeId::base_type(TypeId::INT32_UNBOUNDED_SEQUENCE),
TypeId::INT32
);
}
#[test]
fn test_type_name() {
assert_eq!(TypeId::type_name(TypeId::INT32), Some("int32"));
assert_eq!(TypeId::type_name(TypeId::INT32_ARRAY), Some("int32"));
assert_eq!(TypeId::type_name(TypeId::STRING), Some("string"));
assert_eq!(TypeId::type_name(TypeId::NESTED_TYPE), Some("nested"));
}
}