Skip to main content

harmos_operations/storage/
abandon_file.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::{FileProgress, FileSession, MutationId};
25use harmos_domain::operations::ReplaySafety;
26#[cfg(feature = "runtime")]
27use harmos_storage::{ErasedCatalog, StorageManager};
28
29/* === Constants === */
30
31/* === Macros === */
32
33product!(AbandonFile {
34    mutation: Nested<MutationId> => 1,
35    session: Nested<FileSession> => 2,
36});
37
38/* === Type Aliases === */
39
40/* === Structs === */
41
42// Abandon A Producing File Session, Exactly Once Per Mutation.
43#[harmos::operation(harmos_path = crate, resources = [StorageManager<ErasedCatalog>], codec = manual,
44    id = "storage.abandon_file",
45    response = FileProgress,
46    policy = POLICY,
47    visibility = Host,
48    replay = ReplaySafety::Idempotent)]
49#[derive(Clone, Debug, PartialEq, Eq)]
50pub struct AbandonFile {
51    pub mutation: MutationId,
52    pub session: FileSession,
53}
54
55/* === Enums === */
56
57/* === Traits === */
58
59/* === Implementations === */
60
61impl MemorySize for AbandonFile {
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.session.memory_size().saturating_sub(std::mem::size_of_val(&self.session)))
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    use harmos_storage::FileProgress;
80
81    use crate::{Error, Result};
82
83    impl Operation for super::AbandonFile {
84        type Error = Error;
85
86        #[tracing::instrument(target = "harmos::storage", name = "storage.operation.abandon_file", level = "trace", skip_all)]
87        async fn run(self, context: harmos_sdk::Context<'_, Self>) -> Result<harmos_sdk::operations::Response<FileProgress>> {
88            let storage = context.resource::<harmos_storage::StorageManager<harmos_storage::ErasedCatalog>>();
89
90            let result: ::core::result::Result<FileProgress, Self::Error> =
91                async move { storage.abandon_file_once(&self.mutation, &self.session).await.map_err(Into::into) }.await;
92            result.map(::core::convert::Into::into)
93        }
94
95        fn classify(error: &Error) -> harmos_sdk::operations::FailureKind {
96            crate::storage::classify(error)
97        }
98    }
99}
100
101/* === Public Functions === */
102
103/* === Private Functions === */
104
105/* === Tests === */