Skip to main content

harmos_domain/model/storage/
identities.rs

1/* ***
2 *
3 * Harmos
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * ***
8 */
9
10// Stored Stream And Object Identities The Paged Catalog Lists.
11
12/* === Modules === */
13
14/* === Re-Exports === */
15
16/* === Imports === */
17
18// Standard Library Imports
19
20// Third Party Imports
21use serde::{Deserialize, Serialize};
22// Local Imports
23use super::{ObjectKey, SchemaKey, StorageCursor};
24use crate::Identifier;
25
26/* === Constants === */
27
28/* === Macros === */
29
30/* === Type Aliases === */
31
32/* === Structs === */
33
34#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
35pub struct IdentitySelection {
36    pub kind: IdentityKind,
37    pub schema: Option<Identifier>,
38}
39
40/* === Enums === */
41
42#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
43#[serde(rename_all = "snake_case")]
44pub enum IdentityKind {
45    Streams,
46    Objects,
47}
48
49#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
50#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
51pub enum StoredIdentity {
52    Stream { schema: SchemaKey, scope: Option<Identifier>, item: Identifier, stream: Identifier, first: StorageCursor },
53    Object { schema: SchemaKey, key: ObjectKey, first: StorageCursor },
54}
55
56/* === Traits === */
57
58/* === Implementations === */
59
60impl StoredIdentity {
61    pub fn first(&self) -> &StorageCursor {
62        match self {
63            Self::Stream { first, .. } | Self::Object { first, .. } => first,
64        }
65    }
66
67    pub fn schema(&self) -> &SchemaKey {
68        match self {
69            Self::Stream { schema, .. } | Self::Object { schema, .. } => schema,
70        }
71    }
72
73    pub fn kind(&self) -> IdentityKind {
74        match self {
75            Self::Stream { .. } => IdentityKind::Streams,
76            Self::Object { .. } => IdentityKind::Objects,
77        }
78    }
79}
80
81impl IdentitySelection {
82    pub fn matches(&self, identity: &StoredIdentity) -> bool {
83        identity.kind() == self.kind && self.schema.as_ref().is_none_or(|schema| schema == &identity.schema().id)
84    }
85}
86
87/* === Public Functions === */
88
89/* === Private Functions === */
90
91/* === Tests === */