summaryrefslogtreecommitdiff
path: root/rust/proto/src/lib.rs
blob: 3733b99bbba0bcdd28debe45e405675839359f30 (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
78
79
80
81
82
use chrono::{DateTime, NaiveDateTime, Utc};
use std::ops::{Deref, DerefMut};

include!("gen/mod.rs");

//region ProtoTimestamp

pub struct ProtoTimestamp(pbjson_types::Timestamp);

impl From<ProtoTimestamp> for pbjson_types::Timestamp {
    fn from(value: ProtoTimestamp) -> Self {
        value.0
    }
}

impl From<pbjson_types::Timestamp> for ProtoTimestamp {
    fn from(value: pbjson_types::Timestamp) -> Self {
        ProtoTimestamp(value)
    }
}

impl From<DateTime<Utc>> for ProtoTimestamp {
    fn from(dt: DateTime<Utc>) -> Self {
        ProtoTimestamp(dt.into())
    }
}

impl From<ProtoTimestamp> for DateTime<Utc> {
    // Will silently underflow if nanos is less than 0, but should panic anyway since the
    // nanos will be out of range. TryInto by pbjson_types is useless.
    fn from(value: ProtoTimestamp) -> Self {
        Self::from_utc(value.into(), Utc)
    }
}

impl From<ProtoTimestamp> for NaiveDateTime {
    // Will silently underflow if nanos is less than 0, but should panic anyway since the
    // nanos will be out of range. TryInto by pbjson_types is useless.
    fn from(value: ProtoTimestamp) -> Self {
        let pbjson_types::Timestamp { seconds, nanos } = value.0;

        NaiveDateTime::from_timestamp(seconds, nanos as u32)
    }
}

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
    }
}

//endregion

impl touchpad::common::v1::Gender {
    /// Converts ISO/IEC 5129 integer into gender
    /// Reference: https://en.wikipedia.org/wiki/ISO/IEC_5218
    pub fn from_iso_5218(value: u8) -> Self {
        match value {
            0 => Self::Unspecified,
            1 => Self::Male,
            2 => Self::Female,
            9 => Self::Unspecified,
            _ => Self::Unspecified,
        }
    }

    pub fn to_iso_5218(self) -> u8 {
        match self {
            Self::Unspecified => 0,
            Self::Male => 1,
            Self::Female => 2,
        }
    }
}