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 {
68                secs_since_epoch: input.value()?,
69                nanos_since_epoch: input.value()?,
70                before_epoch: input.value()?,
71            }
72            .timestamp()
73            .map_err(MessageError::borrowed)
74        })
75    }
76
77    type Native = SystemTime;
78    const KIND: Kind = Kind::Message {
79        definition: <Self as crate::schema::Described>::DEFINITION,
80    };
81}
82
83impl crate::schema::Described for Time {
84    const NAME: &'static str = <Self as Value>::NAME;
85    const FIELDS: &'static [Field] = FIELDS;
86    const MARKER: &'static [u8] = &crate::schema::declaration::encode::<
87        { crate::schema::declaration::encoded_len(<Self as crate::schema::Described>::NAME, Self::FIELDS) },
88    >(<Self as crate::schema::Described>::NAME, Self::FIELDS);
89}
90
91const FIELDS: &[Field] = &[
92    Field {
93        name: Cow::Borrowed("secs_since_epoch"),
94        tag: 1,
95        kind: Kind::U64,
96        cardinality: Cardinality::Required,
97    },
98    Field {
99        name: Cow::Borrowed("nanos_since_epoch"),
100        tag: 2,
101        kind: Kind::U32,
102        cardinality: Cardinality::Required,
103    },
104    Field {
105        name: Cow::Borrowed("before_epoch"),
106        tag: 3,
107        kind: Kind::Bool,
108        cardinality: Cardinality::Required,
109    },
110];
111
112/* === Public Functions === */
113
114/* === Private Functions === */
115
116/* === Tests === */