Skip to main content

harmos_domain/model/storage/codec/
timestamp.rs

1/* ***
2 *
3 * Harmos
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * ***
8 */
9
10/* === Modules === */
11
12/* === Re-Exports === */
13
14/* === Imports === */
15
16// Standard Library Imports
17use std::borrow::Cow;
18use std::time::SystemTime;
19
20// Third Party Imports
21
22// Local Imports
23use super::Value;
24use crate::MessageError;
25use crate::model::storage::time::Wire;
26use crate::schema::{Cardinality, Field, Kind};
27
28/* === Constants === */
29
30/* === Macros === */
31
32/* === Type Aliases === */
33
34/* === Structs === */
35
36pub struct Time;
37
38/* === Enums === */
39
40/* === Traits === */
41
42/* === Implementations === */
43
44impl Value for Time {
45    const NAME: &'static str = concat!(module_path!(), "::", stringify!(Time));
46    fn postcard_write(value: &SystemTime, bytes: &mut Vec<u8>) {
47        use crate::__private::PostcardValue;
48        let wire = Wire::from(*value);
49        wire.secs_since_epoch.write(bytes);
50        wire.nanos_since_epoch.write(bytes);
51        wire.before_epoch.write(bytes);
52    }
53
54    fn postcard_size(value: &SystemTime) -> usize {
55        use crate::__private::PostcardValue;
56        let wire = Wire::from(*value);
57        wire.secs_since_epoch.size() + wire.nanos_since_epoch.size() + 1
58    }
59
60    fn postcard_memory(input: &mut crate::__private::PostcardReader<'_>) -> Result<usize, MessageError> {
61        Self::postcard_read(input)?;
62        Ok(0)
63    }
64
65    fn postcard_read(input: &mut crate::__private::PostcardReader<'_>) -> Result<SystemTime, MessageError> {
66        input.nested(|input| {
67            Wire { secs_since_epoch: input.value()?, nanos_since_epoch: input.value()?, before_epoch: input.value()? }
68                .timestamp()
69                .map_err(MessageError::borrowed)
70        })
71    }
72
73    type Native = SystemTime;
74    const KIND: Kind = Kind::Message { definition: <Self as crate::schema::Described>::DEFINITION };
75}
76
77impl crate::schema::Described for Time {
78    const NAME: &'static str = <Self as Value>::NAME;
79    const FIELDS: &'static [Field] = FIELDS;
80    const MARKER: &'static [u8] = &crate::schema::declaration::encode::<
81        { crate::schema::declaration::encoded_len(<Self as crate::schema::Described>::NAME, Self::FIELDS) },
82    >(<Self as crate::schema::Described>::NAME, Self::FIELDS);
83}
84
85const FIELDS: &[Field] = &[
86    Field { name: Cow::Borrowed("secs_since_epoch"), tag: 1, kind: Kind::U64, cardinality: Cardinality::Required },
87    Field { name: Cow::Borrowed("nanos_since_epoch"), tag: 2, kind: Kind::U32, cardinality: Cardinality::Required },
88    Field { name: Cow::Borrowed("before_epoch"), tag: 3, kind: Kind::Bool, cardinality: Cardinality::Required },
89];
90
91/* === Public Functions === */
92
93/* === Private Functions === */
94
95/* === Tests === */