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