use core::fmt;
use enum_ordinalize::Ordinalize;
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Tag(u32);
impl Tag {
pub const fn new(value: u32) -> Self {
assert!(value < (1u32 << 24));
Self(value << 8)
}
pub const fn value(self) -> u32 {
self.0 >> 8
}
pub const fn bytes(self) -> [u8; 3] {
let [a, b, c, _] = self.0.to_be_bytes();
[a, b, c]
}
pub const fn with_type(self, r#type: Type) -> TagType {
TagType::new(self, r#type)
}
}
impl From<TagType> for Tag {
fn from(value: TagType) -> Self {
value.tag()
}
}
impl fmt::Display for Tag {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "0x{:06X}", self.value())
}
}
impl fmt::Debug for Tag {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Tag(0x{:06X})", self.value())
}
}
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Ordinalize)]
#[repr(u8)]
pub enum Type {
Structure = 0x01,
Integer = 0x02,
LongInteger = 0x03,
BigInteger = 0x04,
Enumeration = 0x05,
Boolean = 0x06,
TextString = 0x07,
ByteString = 0x08,
DateTime = 0x09,
Interval = 0x0A,
}
impl From<TagType> for Type {
fn from(value: TagType) -> Self {
value.r#type()
}
}
impl fmt::Display for Type {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Type::Structure => "Structure",
Type::Integer => "Integer",
Type::LongInteger => "LongInteger",
Type::BigInteger => "BigInteger",
Type::Enumeration => "Enumeration",
Type::Boolean => "Boolean",
Type::TextString => "TextString",
Type::ByteString => "ByteString",
Type::DateTime => "DateTime",
Type::Interval => "Interval",
})
}
}
impl fmt::Debug for Type {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct TagType(u32);
impl TagType {
pub const fn new(tag: Tag, r#type: Type) -> Self {
Self(tag.0 | r#type as u32)
}
pub const fn split(self) -> (Tag, Type) {
(self.tag(), self.r#type())
}
pub const fn tag(self) -> Tag {
Tag(self.0 & 0xFFFFFF00)
}
pub const fn r#type(self) -> Type {
unsafe { core::mem::transmute(self.0 as u8) }
}
pub const fn value(self) -> u32 {
self.0
}
pub const fn bytes(self) -> [u8; 4] {
self.0.to_be_bytes()
}
pub const fn parse(bytes: [u8; 4]) -> Self {
Self(u32::from_be_bytes(bytes))
}
}
impl From<(Tag, Type)> for TagType {
fn from((tag, r#type): (Tag, Type)) -> Self {
Self::new(tag, r#type)
}
}
impl From<TagType> for (Tag, Type) {
fn from(value: TagType) -> Self {
value.split()
}
}
impl fmt::Display for TagType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} {}", self.r#type(), self.tag())
}
}
impl fmt::Debug for TagType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "TagType({:?}, {:?})", self.tag(), self.r#type())
}
}
#[cfg(feature = "proptest")]
mod proptest {
use std::ops::Range;
use proptest::prelude::*;
use super::{Tag, Type};
impl Arbitrary for Tag {
type Parameters = ();
type Strategy = prop::strategy::Map<Range<u32>, fn(u32) -> Tag>;
fn arbitrary_with((): Self::Parameters) -> Self::Strategy {
(0..1 << 24).prop_map(Tag::new)
}
}
impl Arbitrary for Type {
type Parameters = ();
type Strategy = prop::strategy::Map<Range<u8>, fn(u8) -> Type>;
fn arbitrary_with((): Self::Parameters) -> Self::Strategy {
(0x01..0x0B).prop_map(|x| unsafe { core::mem::transmute(x) })
}
}
}