Skip to main content

harmos_domain/
schema.rs

1/* ***
2 *
3 * Harmos
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * ***
8 */
9
10// TODO: Describe this module's responsibility.
11
12/* === Modules === */
13
14pub mod declaration;
15pub mod graph;
16mod payload;
17
18/* === Re-Exports === */
19
20pub use declaration::{Field, Kind};
21pub use payload::{Event, PathElement, PayloadError, Scalar, Validator, VisitError};
22
23/* === Imports === */
24
25// Standard Library Imports
26use std::borrow::Cow;
27// Third Party Imports
28use serde::{Deserialize, Serialize};
29// Local Imports
30
31/* === Constants === */
32
33pub const MAX_DEPTH: usize = 32;
34pub const MAX_NAME_BYTES: usize = 256;
35
36/* === Macros === */
37
38// Scalar Wrapper Payloads, With Their Fixed Field-One Identity
39macro_rules! wrapper {
40    ($($ty:ty => $kind:ident),* $(,)?) => {$(
41        impl Described for $ty {
42            const NAME: &'static str = concat!("harmos::", stringify!($ty));
43            const FIELDS: &'static [graph::Field<declaration::Definition>] = &[graph::Field {
44                name: Cow::Borrowed("value"),
45                tag: 1,
46                kind: graph::Kind::$kind,
47                cardinality: Cardinality::Required,
48            }];
49            const MARKER: &'static [u8] = &declaration::encode::<{
50                declaration::encoded_len(Self::NAME, Self::FIELDS)
51            }>(Self::NAME, Self::FIELDS);
52        }
53    )*};
54}
55
56wrapper!(bool => Bool, u32 => U32, u64 => U64, i32 => I32, i64 => I64, f32 => F32, f64 => F64, String => String, Vec<u8> => Bytes);
57
58/* === Type Aliases === */
59
60/* === Structs === */
61
62/* === Enums === */
63
64#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
65#[serde(rename_all = "snake_case")]
66pub enum Cardinality {
67    Required,
68    Optional,
69    Repeated,
70    Variant,
71}
72
73#[derive(Debug, thiserror::Error)]
74pub enum Error {
75    #[error(transparent)]
76    Graph(#[from] graph::Error),
77}
78
79/* === Traits === */
80
81// A Static Message Shape, Independent Of Payload Encoding Or Construction.
82pub trait Described: 'static {
83    const NAME: &'static str;
84    const FIELDS: &'static [graph::Field<declaration::Definition>];
85    const MARKER: &'static [u8];
86
87    const DEFAULT_VARIANT: Option<usize> = Some(0);
88
89    const DEFINITION: declaration::Definition = declaration::Definition::of::<Self>();
90}
91
92/* === Implementations === */
93
94impl Described for () {
95    const NAME: &'static str = "harmos::unit";
96    const FIELDS: &'static [graph::Field<declaration::Definition>] = &[];
97    const MARKER: &'static [u8] = &declaration::encode::<{ declaration::encoded_len(Self::NAME, Self::FIELDS) }>(Self::NAME, Self::FIELDS);
98}
99
100/* === Public Functions === */
101
102/* === Private Functions === */
103
104fn diagnostic_name(name: &str) -> String {
105    if name.len() <= MAX_NAME_BYTES {
106        return name.to_owned();
107    }
108    let mut end = MAX_NAME_BYTES - 3;
109    while !name.is_char_boundary(end) {
110        end -= 1;
111    }
112    let mut result = String::with_capacity(MAX_NAME_BYTES);
113    result.push_str(&name[..end]);
114    result.push_str("...");
115    result
116}
117
118/* === Tests === */