Skip to main content

harmos_domain/operations/host/
payloads.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 crate::model::storage::codec::{Bytes, Code, Id, Index, Optional, Text, coded, product};
22use crate::plugin::Grant;
23use crate::{Identifier, MemorySize};
24
25/* === Constants === */
26
27/* === Macros === */
28
29product!(Catalog {
30    kind: Code<CatalogKind> => 1,
31    schema: Optional<Id> => 2,
32    after: Optional<Text> => 3,
33    rows: Optional<Index> => 4,
34    bytes: Optional<Index> => 5,
35});
36
37product!(CatalogPage { entries: Bytes => 1, next: Optional<Text> => 2 });
38
39coded!(CatalogKind { Operations, Schemas, Streams, Objects });
40
41/* === Type Aliases === */
42
43/* === Structs === */
44
45#[derive(Clone, Debug, PartialEq, Eq)]
46pub struct Catalog {
47    pub kind: CatalogKind,
48    pub schema: Option<Identifier>,
49    pub after: Option<String>,
50    pub rows: Option<usize>,
51    pub bytes: Option<usize>,
52}
53
54#[derive(Clone, Debug, Default, PartialEq, Eq)]
55pub struct CatalogPage {
56    pub entries: Vec<u8>,
57    pub next: Option<String>,
58}
59
60/* === Enums === */
61
62#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
63pub enum CatalogKind {
64    #[default]
65    Operations,
66
67    Schemas,
68    Streams,
69    Objects,
70}
71
72/* === Traits === */
73
74/* === Implementations === */
75
76impl Catalog {
77    // Stored Data Follows The Storage Read Grant Grammar; Operation Discovery
78    // Keeps Its Per-Contract Conditional Access.
79    pub fn required_grant(&self) -> Option<Grant> {
80        let (family, all) = match self.kind {
81            CatalogKind::Operations => return None,
82            CatalogKind::Schemas => ("schemas", "schemas:*"),
83            CatalogKind::Streams => ("streams", "streams:*"),
84            CatalogKind::Objects => ("objects", "objects:*"),
85        };
86        Some(Grant::of("host.catalog", self.schema.as_ref().map_or_else(|| all.to_owned(), |schema| format!("{family}:schema:{schema}"))))
87    }
88}
89
90impl MemorySize for Catalog {
91    fn memory_size(&self) -> usize {
92        std::mem::size_of::<Self>()
93            .saturating_add(self.schema.as_ref().map_or(0, |schema| schema.as_str().len()))
94            .saturating_add(self.after.as_ref().map_or(0, String::capacity))
95    }
96}
97
98impl MemorySize for CatalogPage {
99    fn memory_size(&self) -> usize {
100        std::mem::size_of::<Self>().saturating_add(self.entries.capacity()).saturating_add(self.next.as_ref().map_or(0, String::capacity))
101    }
102}
103
104/* === Public Functions === */
105
106/* === Private Functions === */
107
108/* === Tests === */