diff options
Diffstat (limited to '')
-rw-r--r-- | rust/proto/src/lib.rs | 54 |
1 files changed, 50 insertions, 4 deletions
diff --git a/rust/proto/src/lib.rs b/rust/proto/src/lib.rs index 1a8199b..3733b99 100644 --- a/rust/proto/src/lib.rs +++ b/rust/proto/src/lib.rs @@ -1,4 +1,4 @@ -use chrono::{DateTime, Utc}; +use chrono::{DateTime, NaiveDateTime, Utc}; use std::ops::{Deref, DerefMut}; include!("gen/mod.rs"); @@ -7,9 +7,15 @@ include!("gen/mod.rs"); pub struct ProtoTimestamp(pbjson_types::Timestamp); -impl Into<pbjson_types::Timestamp> for ProtoTimestamp { - fn into(self) -> pbjson_types::Timestamp { - self.0 +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) } } @@ -19,6 +25,24 @@ impl From<DateTime<Utc>> for ProtoTimestamp { } } +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; @@ -34,3 +58,25 @@ impl DerefMut for ProtoTimestamp { } //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, + } + } +}
\ No newline at end of file |