use byteorder::LittleEndian;
use zerocopy::U32;
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct Duration {
months: Months,
days: Days,
millis: Millis,
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct Months(U32<LittleEndian>);
impl Months {
pub fn new(months: u32) -> Self {
Self(U32::new(months))
}
}
impl From<Months> for u32 {
fn from(days: Months) -> Self {
days.0.get()
}
}
impl From<[u8; 4]> for Months {
fn from(bytes: [u8; 4]) -> Self {
Self(U32::from(bytes))
}
}
impl AsRef<[u8; 4]> for Months {
fn as_ref(&self) -> &[u8; 4] {
self.0.as_ref()
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct Days(U32<LittleEndian>);
impl Days {
pub fn new(days: u32) -> Self {
Self(U32::new(days))
}
}
impl From<Days> for u32 {
fn from(days: Days) -> Self {
days.0.get()
}
}
impl From<[u8; 4]> for Days {
fn from(bytes: [u8; 4]) -> Self {
Self(U32::from(bytes))
}
}
impl AsRef<[u8; 4]> for Days {
fn as_ref(&self) -> &[u8; 4] {
self.0.as_ref()
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct Millis(U32<LittleEndian>);
impl Millis {
pub fn new(millis: u32) -> Self {
Self(U32::new(millis))
}
}
impl From<Millis> for u32 {
fn from(days: Millis) -> Self {
days.0.get()
}
}
impl From<[u8; 4]> for Millis {
fn from(bytes: [u8; 4]) -> Self {
Self(U32::from(bytes))
}
}
impl AsRef<[u8; 4]> for Millis {
fn as_ref(&self) -> &[u8; 4] {
self.0.as_ref()
}
}
impl Duration {
pub fn new(months: Months, days: Days, millis: Millis) -> Self {
Self {
months,
days,
millis,
}
}
pub fn months(&self) -> Months {
self.months
}
pub fn days(&self) -> Days {
self.days
}
pub fn millis(&self) -> Millis {
self.millis
}
}
impl From<Duration> for [u8; 12] {
fn from(duration: Duration) -> Self {
let mut bytes = [0u8; 12];
bytes[0..4].copy_from_slice(duration.months.as_ref());
bytes[4..8].copy_from_slice(duration.days.as_ref());
bytes[8..12].copy_from_slice(duration.millis.as_ref());
bytes
}
}
impl From<[u8; 12]> for Duration {
fn from(bytes: [u8; 12]) -> Self {
Self {
months: Months::from([bytes[0], bytes[1], bytes[2], bytes[3]]),
days: Days::from([bytes[4], bytes[5], bytes[6], bytes[7]]),
millis: Millis::from([bytes[8], bytes[9], bytes[10], bytes[11]]),
}
}
}