From c546e2ef6c69bb1c6a86093f3cc7b2dab20d6ac4 Mon Sep 17 00:00:00 2001 From: Max Audron Date: Sat, 25 Apr 2020 19:35:29 +0200 Subject: finish parsing of primitive types --- src/protocol/primitive/datetime.rs | 93 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 src/protocol/primitive/datetime.rs (limited to 'src/protocol/primitive/datetime.rs') diff --git a/src/protocol/primitive/datetime.rs b/src/protocol/primitive/datetime.rs new file mode 100644 index 0000000..688a022 --- /dev/null +++ b/src/protocol/primitive/datetime.rs @@ -0,0 +1,93 @@ +use crate::protocol::primitive::deserialize::Deserialize; +use crate::protocol::primitive::serialize::Serialize; + +#[derive(Clone, Debug, std::cmp::PartialEq)] +pub struct DateTime { + julian_day: i32, // Day in Julian calendar, unknown if signed or unsigned + millis_of_day: i32, // Milliseconds since start of day + zone: u8, // Timezone of DateTime, 0x00 is local, 0x01 is UTC +} + +impl Serialize for DateTime { + fn serialize(&self) -> Result, failure::Error> { + let mut values: Vec = Vec::new(); + + values.append(&mut i32::serialize(&self.julian_day)?); + values.append(&mut i32::serialize(&self.millis_of_day)?); + values.append(&mut u8::serialize(&(self.zone))?); + + Ok(values) + } +} + +impl Deserialize for DateTime { + fn parse(b: &[std::primitive::u8]) -> Result<(std::primitive::usize, Self), failure::Error> + where + Self: Sized, + { + let (_, julian_day) = i32::parse(&b[0..4])?; + let (_, millis_of_day) = i32::parse(&b[4..8])?; + let (_, zone) = u8::parse(&b[8..9])?; + + return Ok(( + 9, + DateTime { + julian_day, + millis_of_day, + zone, + }, + )); + } +} + +#[derive(Clone, Debug, std::cmp::PartialEq)] +pub struct Date { + julian_day: i32, // Day in Julian calendar, unknown if signed or unsigned +} + +impl Serialize for Date { + fn serialize(&self) -> Result, failure::Error> { + let mut values: Vec = Vec::new(); + + values.append(&mut i32::serialize(&self.julian_day)?); + + Ok(values) + } +} + +impl Deserialize for Date { + fn parse(b: &[std::primitive::u8]) -> Result<(std::primitive::usize, Self), failure::Error> + where + Self: Sized, + { + let (_, julian_day) = i32::parse(&b[0..4])?; + + return Ok((9, Date { julian_day })); + } +} + +#[derive(Clone, Debug, std::cmp::PartialEq)] +pub struct Time { + millis_of_day: i32, // Milliseconds since start of day +} + +impl Serialize for Time { + fn serialize(&self) -> Result, failure::Error> { + let mut values: Vec = Vec::new(); + + values.append(&mut i32::serialize(&self.millis_of_day)?); + + Ok(values) + } +} + +impl Deserialize for Time { + fn parse(b: &[std::primitive::u8]) -> Result<(std::primitive::usize, Self), failure::Error> + where + Self: Sized, + { + let (_, millis_of_day) = i32::parse(&b[0..4])?; + + return Ok((4, Time { millis_of_day })); + } +} -- cgit v1.2.3