Skip to main content

harmos_domain/model/storage/data/
codec.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
17
18// Third Party Imports
19
20// Local Imports
21use super::{RecordInput, StoredRecord, StoredRow, StreamInput};
22use crate::model::storage::codec::{Bytes, Id, Nested, Optional, Time, Unsigned, Version, product};
23use crate::model::storage::{SchemaKey, StorageCursor};
24
25/* === Constants === */
26
27/* === Macros === */
28
29product!(SchemaKey { id: Id => 1, version: Version => 2, payload_encoding: Encoding => 3 });
30product!(RecordInput {
31    schema: Nested<SchemaKey> => 1,
32    scope: Optional<Id> => 2,
33    item: Id => 3,
34    source: Optional<Id> => 4,
35    source_time: Optional<Time> => 5,
36    payload: Bytes => 6,
37});
38product!(StreamInput {
39    record: Nested<RecordInput> => 1,
40    stream: Id => 2,
41    sequence: Unsigned => 3,
42});
43product!(StoredRecord<Vec<u8>> {
44    position: Nested<StorageCursor> => 1,
45    schema: Nested<SchemaKey> => 2,
46    scope: Optional<Id> => 3,
47    item: Id => 4,
48    source: Optional<Id> => 5,
49    source_time: Optional<Time> => 6,
50    payload: Bytes => 7,
51});
52product!(StoredRow<Vec<u8>> {
53    record: Nested<StoredRecord<Vec<u8>>> => 1,
54    stream: Id => 2,
55    sequence: Unsigned => 3,
56});
57
58/* === Type Aliases === */
59
60/* === Structs === */
61
62struct Encoding;
63
64/* === Enums === */
65
66/* === Traits === */
67
68/* === Implementations === */
69
70impl crate::model::storage::codec::Value for Encoding {
71    const NAME: &'static str = concat!(module_path!(), "::", stringify!(Encoding));
72    type Native = crate::model::storage::PayloadEncoding;
73    const KIND: crate::schema::declaration::Kind = crate::schema::declaration::Kind::U64;
74    fn postcard_write(value: &Self::Native, bytes: &mut Vec<u8>) {
75        crate::__private::PostcardValue::write(&(*value as u64), bytes);
76    }
77
78    fn postcard_size(_: &Self::Native) -> usize {
79        1
80    }
81
82    fn postcard_memory(input: &mut crate::__private::PostcardReader<'_>) -> Result<usize, crate::MessageError> {
83        Self::postcard_read(input).map(|_| 0)
84    }
85
86    fn postcard_read(input: &mut crate::__private::PostcardReader<'_>) -> Result<Self::Native, crate::MessageError> {
87        encoding(input.value()?)
88    }
89}
90
91/* === Public Functions === */
92
93/* === Private Functions === */
94
95fn encoding(value: u64) -> Result<crate::model::storage::PayloadEncoding, crate::MessageError> {
96    match value {
97        0 => Ok(crate::model::storage::PayloadEncoding::ProtobufV1),
98        1 => Ok(crate::model::storage::PayloadEncoding::PostcardV1),
99        _ => Err(crate::MessageError::borrowed("unknown persisted payload encoding")),
100    }
101}
102
103/* === Tests === */