Skip to main content

harmos_domain/
inventory.rs

1/* ***
2 *
3 * Harmos
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * ***
8 */
9
10/* === Modules === */
11
12pub mod contracts;
13pub mod contributions;
14mod declaration;
15pub mod listeners;
16pub mod observers;
17pub mod operations;
18pub mod publications;
19
20/* === Re-Exports === */
21
22pub use contributions::Section;
23pub use declaration::{CarrierSection, Declaration, DeclarationError, Facility, RestartPolicy};
24
25/* === Imports === */
26
27// Standard Library Imports
28
29// Third Party Imports
30use serde::Deserialize;
31// Local Imports
32
33/* === Constants === */
34
35/* === Macros === */
36
37/* === Type Aliases === */
38
39/* === Structs === */
40
41#[derive(Clone, Debug, PartialEq, Eq, Deserialize)]
42#[serde(deny_unknown_fields)]
43pub struct StreamDeclaration {
44    pub metadata: crate::Metadata,
45
46    #[serde(default)]
47    pub row_metadata: Vec<crate::StreamField>,
48
49    pub id: String,
50    pub schema: u32,
51    pub window: usize,
52    pub row: crate::schema::graph::Schema,
53}
54
55/* === Enums === */
56
57/* === Traits === */
58
59/* === Implementations === */
60
61/* === Public Functions === */
62
63pub(crate) const fn number<const N: usize>(output: &mut [u8; N], cursor: &mut usize, value: u64) {
64    let width = digits(value);
65    let mut remaining = value;
66    let mut position = width;
67    while position > 0 {
68        position -= 1;
69        output[*cursor + position] = b'0' + (remaining % 10) as u8;
70        remaining /= 10;
71    }
72    *cursor += width;
73}
74
75pub(crate) const fn digits(mut value: u64) -> usize {
76    let mut len = 1;
77    while value >= 10 {
78        value /= 10;
79        len += 1;
80    }
81    len
82}
83
84pub(crate) const fn quoted_len(value: &str) -> usize {
85    let mut len = 2;
86    let mut index = 0;
87    while index < value.len() {
88        len += match value.as_bytes()[index] {
89            b'"' | b'\\' => 2,
90            0..=31 => 6,
91            _ => 1,
92        };
93        index += 1;
94    }
95    len
96}
97
98pub(crate) const fn quoted<const N: usize>(output: &mut [u8; N], cursor: &mut usize, value: &str) {
99    write(output, cursor, b"\"");
100    let mut index = 0;
101    while index < value.len() {
102        let byte = value.as_bytes()[index];
103        match byte {
104            b'"' | b'\\' => {
105                output[*cursor] = b'\\';
106                *cursor += 1;
107                output[*cursor] = byte;
108                *cursor += 1;
109            }
110            0..=31 => {
111                write(output, cursor, b"\\u00");
112                output[*cursor] = b"0123456789abcdef"[(byte >> 4) as usize];
113                output[*cursor + 1] = b"0123456789abcdef"[(byte & 15) as usize];
114                *cursor += 2;
115            }
116            _ => {
117                output[*cursor] = byte;
118                *cursor += 1;
119            }
120        }
121        index += 1;
122    }
123    write(output, cursor, b"\"");
124}
125
126pub(crate) const fn write<const N: usize>(output: &mut [u8; N], cursor: &mut usize, value: &[u8]) {
127    let mut index = 0;
128    while index < value.len() {
129        output[*cursor] = value[index];
130        *cursor += 1;
131        index += 1;
132    }
133}
134
135/* === Private Functions === */
136
137fn retain_schema(
138    schema: crate::schema::graph::Schema, settings: &mut crate::config::SchemaSettings,
139) -> Result<crate::schema::graph::Schema, crate::schema::graph::Error> {
140    use crate::MemorySize;
141    use crate::schema::graph::Error;
142    let preparation = schema.preparation(settings)?;
143    settings.preparation_bytes =
144        settings.preparation_bytes.get().checked_sub(schema.memory_size()).and_then(std::num::NonZeroUsize::new).ok_or(Error::Budget)?;
145    // Resolve Named Definitions And Validate The Result Under One Shared Work
146    // Allowance Across Every Contract, Source And Publication In The Artifact.
147    settings.preparation_work = preparation
148        .work
149        .checked_mul(2)
150        .and_then(|work| settings.preparation_work.get().checked_sub(work))
151        .and_then(std::num::NonZeroUsize::new)
152        .ok_or(Error::Budget)?;
153    Ok(schema)
154}
155
156/* === Tests === */