From edc5985b1744dcffb8bf1b41ce273508573589fe Mon Sep 17 00:00:00 2001 From: Anshul Gupta Date: Mon, 15 Aug 2022 15:24:06 -0700 Subject: Adds rust touchpad scraper Implements meet, events, and swimmers api in touchpad live. Also implements protobuf generation into rust crate. --- rust/proto/src/lib.rs | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 rust/proto/src/lib.rs (limited to 'rust/proto/src') 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 { + 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 for ProtoTimestamp { + fn into(self) -> pbjson_types::Timestamp { + self.0 + } +} + +impl From> for ProtoTimestamp { + fn from(dt: DateTime) -> 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 + } +} -- cgit v1.2.3