use chrono::{DateTime, NaiveDateTime, Utc}; use std::ops::{Deref, DerefMut}; include!("gen/mod.rs"); //region ProtoTimestamp pub struct ProtoTimestamp(pbjson_types::Timestamp); impl From for pbjson_types::Timestamp { fn from(value: ProtoTimestamp) -> Self { value.0 } } impl From for ProtoTimestamp { fn from(value: pbjson_types::Timestamp) -> Self { ProtoTimestamp(value) } } impl From> for ProtoTimestamp { fn from(dt: DateTime) -> Self { ProtoTimestamp(dt.into()) } } impl From for DateTime { // 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 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, } } }