Skip to main content

harmos_domain/model/storage/objects/
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::{ObjectEntry, ObjectReceipt, ObjectVersionToken};
22use crate::MessageError;
23use crate::model::storage::codec::{Bytes, Nested, Value, product};
24use crate::model::storage::{ObjectKey, ObjectValue, SchemaKey, StorageCursor};
25
26/* === Constants === */
27
28/* === Macros === */
29
30product!(ObjectValue {
31    schema: Nested<SchemaKey> => 1,
32    item: Key => 2,
33    payload: Bytes => 3,
34});
35product!(ObjectEntry {
36    position: Nested<StorageCursor> => 1,
37    version: Nested<ObjectVersionToken> => 2,
38    value: Nested<ObjectValue> => 3,
39});
40product!(ObjectReceipt {
41    position: Nested<StorageCursor> => 1,
42    version: Nested<ObjectVersionToken> => 2,
43});
44
45/* === Type Aliases === */
46
47type Result<T> = std::result::Result<T, MessageError>;
48
49/* === Structs === */
50
51pub struct Key;
52
53/* === Enums === */
54
55/* === Traits === */
56
57/* === Implementations === */
58
59impl Value for Key {
60    const NAME: &'static str = concat!(module_path!(), "::", stringify!(Key));
61    crate::model::storage::codec::postcard_field!();
62    type Native = ObjectKey;
63    const KIND: crate::schema::declaration::Kind = crate::schema::declaration::Kind::String;
64}
65
66impl crate::__private::PostcardValue for ObjectKey {
67    fn write(&self, bytes: &mut Vec<u8>) {
68        crate::__private::PostcardValue::write(&self.as_str().len(), bytes);
69        bytes.extend_from_slice(self.as_str().as_bytes());
70    }
71
72    fn size(&self) -> usize {
73        self.as_str().len().saturating_add(crate::__private::PostcardValue::size(&self.as_str().len()))
74    }
75
76    fn memory(input: &mut crate::__private::PostcardReader<'_>) -> Result<usize> {
77        let text: &str = input.value()?;
78        if text.is_empty() {
79            return Err(MessageError::borrowed("empty object key"));
80        }
81        crate::__private::decoding_add(std::mem::size_of::<Self>(), text.len())
82    }
83
84    fn read(input: &mut crate::__private::PostcardReader<'_>) -> Result<Self> {
85        let text: &str = input.value()?;
86        if text.is_empty() {
87            return Err(MessageError::borrowed("empty object key"));
88        }
89        Self::from_owned(Box::<str>::from(text).into_string()).map_err(|_| MessageError::borrowed("empty object key"))
90    }
91}
92
93/* === Public Functions === */
94
95/* === Private Functions === */
96
97/* === Tests === */