Skip to main content

harmos_operations/lifecycle/
shutdown.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 harmos_domain::{MemorySize, schema::Described};
22
23/* === Constants === */
24
25/* === Macros === */
26
27harmos_domain::postcard_product!(Shutdown {});
28
29/* === Type Aliases === */
30
31/* === Structs === */
32
33#[harmos::operation(harmos_path = crate, codec = manual, id = "harmos.shutdown", response = (), policy = super::POLICY, )]
34#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
35pub struct Shutdown {}
36
37#[cfg(feature = "lifecycle")]
38pub struct Control {
39    #[cfg(feature = "lifecycle")]
40    instance: Box<dyn harmos_sdk::operations::lifecycle::LifecycleInstance>,
41}
42
43/* === Enums === */
44
45/* === Traits === */
46
47/* === Implementations === */
48
49impl Described for Shutdown {
50    const NAME: &'static str = <harmos_domain::model::operations::lifecycle::ShutdownSchema as Described>::NAME;
51    const FIELDS: &'static [harmos_domain::schema::declaration::Field] =
52        <harmos_domain::model::operations::lifecycle::ShutdownSchema as Described>::FIELDS;
53    const MARKER: &'static [u8] = <harmos_domain::model::operations::lifecycle::ShutdownSchema as Described>::MARKER;
54}
55
56impl MemorySize for Shutdown {
57    fn memory_size(&self) -> usize {
58        std::mem::size_of::<Self>()
59    }
60}
61
62#[cfg(feature = "lifecycle")]
63impl harmos_sdk::operations::Carrier for Control {
64    type Declaration = Shutdown;
65    type Error = super::ControlError;
66    type StreamEndpoints = ();
67    async fn bind(context: crate::ExecutionContext) -> Result<Self, Self::Error> {
68        let access = context.resource::<harmos_sdk::operations::lifecycle::LifecycleAccess>()?;
69        Ok(Self {
70            instance: access.0.clone().begin(super::Action::Shutdown),
71        })
72    }
73    async fn attempt(
74        &mut self, context: crate::ExecutionContext, request: &mut Shutdown, _: &mut (),
75    ) -> Result<crate::Response<()>, Self::Error> {
76        let _ = &request;
77        self.instance.run(context, &[], &[], &[]).await.map_err(super::ControlError::Access)?;
78        Ok(().into())
79    }
80    async fn cleanup(&mut self) -> Result<(), Self::Error> {
81        self.instance.cleanup().await.map_err(super::ControlError::Access)
82    }
83}
84
85#[cfg(feature = "lifecycle")]
86impl crate::Executable for Shutdown {
87    type Carrier = Control;
88}
89
90/* === Public Functions === */
91
92/* === Private Functions === */
93
94/* === Tests === */