summaryrefslogtreecommitdiff
path: root/rust/proto/src/lib.rs
blob: 1a16c15ce1d5fb69e61fd214186755af086512b5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
    }
}