Skip to main content

harmos_operations/storage/
capture_file.rs

1/* ***
2 *
3 * Harmos
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * ***
8 */
9
10// CaptureFile Declaration.
11
12/* === Modules === */
13
14/* === Re-Exports === */
15
16/* === Imports === */
17
18// Standard Library Imports
19
20// Third Party Imports
21
22// Local Imports
23use super::IO_POLICY;
24use harmos_domain::MemorySize;
25use harmos_domain::model::storage::codec::{Code, Nested, product};
26use harmos_domain::model::storage::files::{ByteRange, CaptureConsistency, FileMetadata, FileReference, FileSource};
27use harmos_domain::operations::ReplaySafety;
28#[cfg(feature = "runtime")]
29use harmos_storage::{ErasedCatalog, StorageManager};
30
31/* === Constants === */
32
33/* === Macros === */
34
35product!(CaptureFile {
36    source: Nested<FileSource> => 1,
37    range: Nested<ByteRange> => 2,
38    metadata: Nested<FileMetadata> => 3,
39    consistency: Code<CaptureConsistency> => 4,
40});
41
42/* === Type Aliases === */
43
44/* === Structs === */
45
46#[harmos::operation(harmos_path = crate, resources = [StorageManager<ErasedCatalog>], codec = manual,
47    id = "storage.capture_file",
48    response = FileReference,
49    policy = IO_POLICY,
50    visibility = Shared,
51    replay = ReplaySafety::Unknown,
52    grpc(package = "harmos.storage.v1", service =  "Storage", method =  "CaptureFile"),
53    grant = Self::grant_for)]
54#[derive(Clone, Debug, PartialEq, Eq)]
55pub struct CaptureFile {
56    pub source: FileSource,
57    pub range: ByteRange,
58    pub metadata: FileMetadata,
59    pub consistency: CaptureConsistency,
60}
61
62/* === Enums === */
63
64/* === Traits === */
65
66/* === Implementations === */
67
68impl CaptureFile {
69    fn grant_for(request: &CaptureFile) -> Option<harmos_domain::plugin::Grant> {
70        request.source.grant_scope().map(|scope| harmos_domain::plugin::Grant::of(<Self as harmos_domain::OperationDeclaration>::ID, scope))
71    }
72}
73
74impl MemorySize for CaptureFile {
75    fn memory_size(&self) -> usize {
76        std::mem::size_of::<Self>()
77            .saturating_add(self.source.memory_size().saturating_sub(std::mem::size_of_val(&self.source)))
78            .saturating_add(self.range.memory_size().saturating_sub(std::mem::size_of_val(&self.range)))
79            .saturating_add(self.metadata.memory_size().saturating_sub(std::mem::size_of_val(&self.metadata)))
80            .saturating_add(self.consistency.memory_size().saturating_sub(std::mem::size_of_val(&self.consistency)))
81    }
82}
83
84#[cfg(feature = "runtime")]
85mod execution {
86
87    // CaptureFile Runtime Handler.
88
89    // Standard Library Imports
90
91    // Third Party Imports
92
93    // Local Imports
94    use crate::{Error, Result};
95    use harmos_domain::model::storage::files::FileReference;
96    use harmos_sdk::operations::Operation;
97
98    impl Operation for super::CaptureFile {
99        type Error = Error;
100
101        #[tracing::instrument(target = "harmos::storage", name = "storage.operation.capture_file", level = "trace", skip_all)]
102        async fn run(self, context: harmos_sdk::Context<'_, Self>) -> Result<harmos_sdk::operations::Response<FileReference>> {
103            let storage = context.resource::<harmos_storage::StorageManager<harmos_storage::ErasedCatalog>>();
104
105            Ok(storage
106                .capture_file(harmos_storage::FileCapture {
107                    source: self.source.clone(),
108                    range: self.range,
109                    metadata: self.metadata.clone(),
110                    consistency: self.consistency,
111                })
112                .await?
113                .into())
114        }
115
116        fn classify(error: &Error) -> harmos_sdk::operations::FailureKind {
117            crate::storage::classify(error)
118        }
119    }
120}
121
122/* === Public Functions === */
123
124/* === Private Functions === */
125
126/* === Tests === */