Skip to main content

harmos_operations/storage/
write_schema.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::POLICY;
22use harmos_domain::MemorySize;
23use harmos_domain::model::storage::codec::{Nested, product};
24use harmos_domain::model::storage::{MutationId, SchemaInput, StorageCursor};
25use harmos_domain::operations::ReplaySafety;
26#[cfg(feature = "runtime")]
27use harmos_storage::{ErasedCatalog, StorageManager};
28
29/* === Constants === */
30
31/* === Macros === */
32
33product!(WriteSchema {
34    mutation: Nested<MutationId> => 1,
35    schema: Nested<SchemaInput> => 2,
36});
37
38/* === Type Aliases === */
39
40/* === Structs === */
41
42// Register One Payload Schema, Exactly Once Per Mutation.
43#[harmos::operation(harmos_path = crate, resources = [StorageManager<ErasedCatalog>], codec = manual,
44    id = "storage.write_schema",
45    response = StorageCursor,
46    policy = POLICY,
47    visibility = Host,
48    replay = ReplaySafety::Idempotent)]
49#[derive(Clone, Debug, PartialEq, Eq)]
50pub struct WriteSchema {
51    pub mutation: MutationId,
52    pub schema: SchemaInput,
53}
54
55/* === Enums === */
56
57/* === Traits === */
58
59/* === Implementations === */
60
61impl MemorySize for WriteSchema {
62    fn memory_size(&self) -> usize {
63        std::mem::size_of::<Self>()
64            .saturating_add(self.mutation.memory_size().saturating_sub(std::mem::size_of_val(&self.mutation)))
65            .saturating_add(self.schema.memory_size().saturating_sub(std::mem::size_of_val(&self.schema)))
66    }
67}
68
69#[cfg(feature = "runtime")]
70mod execution {
71
72    // Standard Library Imports
73
74    // Third Party Imports
75
76    // Local Imports
77
78    use harmos_sdk::operations::Operation;
79
80    use harmos_storage::StorageCursor;
81
82    use crate::{Error, Result};
83
84    impl Operation for super::WriteSchema {
85        type Error = Error;
86
87        async fn run(self, context: harmos_sdk::Context<'_, Self>) -> Result<harmos_sdk::operations::Response<StorageCursor>> {
88            let storage = context.resource::<harmos_storage::StorageManager<harmos_storage::ErasedCatalog>>();
89
90            let result: ::core::result::Result<StorageCursor, Self::Error> = async move {
91                storage.write_schema_once(&self.mutation, self.schema.key.clone(), self.schema.graph_owner()).await.map_err(Into::into)
92            }
93            .await;
94            result.map(::core::convert::Into::into)
95        }
96
97        fn classify(error: &Error) -> harmos_sdk::operations::FailureKind {
98            crate::storage::classify(error)
99        }
100    }
101}
102
103/* === Public Functions === */
104
105/* === Private Functions === */
106
107/* === Tests === */