diff options
Diffstat (limited to 'rust/proto/src')
-rw-r--r-- | rust/proto/src/lib.rs | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/rust/proto/src/lib.rs b/rust/proto/src/lib.rs new file mode 100644 index 0000000..1a16c15 --- /dev/null +++ b/rust/proto/src/lib.rs @@ -0,0 +1,77 @@ +use chrono::{DateTime, TimeZone, Utc}; +use std::ops::{Deref, DerefMut}; + +include!("gen/mod.rs"); + +impl From<&str> for touchpad::common::v1::Gender { + fn from(s: &str) -> Self { + match s { + "Male" => Self::Male, + "Female" => Self::Female, + _ => Self::Unspecified, + } + } +} + +impl From<&str> for touchpad::common::v1::Stroke { + fn from(s: &str) -> Self { + match s { + "Fly" => Self::Fly, + "Back" => Self::Back, + "Breast" => Self::Breast, + "Free" => Self::Free, + "Medley" => Self::Medley, + _ => Self::Unspecified, + } + } +} + +impl From<&str> for touchpad::common::v1::EventTimeResult { + fn from(s: &str) -> Self { + match s { + "DQ" => Self::Dq, + "DNS" => Self::Dns, + "SCR" => Self::Scr, + _ => Self::Unspecified, + } + } +} + +pub struct ProtoTimestamp(pbjson_types::Timestamp); + +impl ProtoTimestamp { + const TOUCHPAD_DATE_FORMAT: &'static str = "%Y-%m-%d"; + + pub fn from_touchpad(str: &str) -> Result<Self, chrono::format::ParseError> { + let date = chrono::NaiveDate::parse_from_str(str, ProtoTimestamp::TOUCHPAD_DATE_FORMAT)?; + let datetime = chrono::NaiveDateTime::new(date, chrono::NaiveTime::from_hms(0, 0, 0)); + + Ok(Utc.from_utc_datetime(&datetime).into()) + } +} + +impl Into<pbjson_types::Timestamp> for ProtoTimestamp { + fn into(self) -> pbjson_types::Timestamp { + self.0 + } +} + +impl From<DateTime<Utc>> for ProtoTimestamp { + fn from(dt: DateTime<Utc>) -> Self { + ProtoTimestamp(dt.into()) + } +} + +impl Deref for ProtoTimestamp { + type Target = pbjson_types::Timestamp; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl DerefMut for ProtoTimestamp { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} |