peace_rt/cmds/
states_current_stored_display_cmd.rs

1use std::{fmt::Debug, marker::PhantomData};
2
3use peace_cmd_ctx::{CmdCtxSpsf, CmdCtxTypes};
4use peace_cmd_model::CmdOutcome;
5use peace_resource_rt::states::StatesCurrentStored;
6use peace_rt_model_core::output::OutputWrite;
7
8use crate::cmds::StatesCurrentReadCmd;
9
10/// Displays [`StatesCurrent`]s from storage.
11#[derive(Debug)]
12pub struct StatesCurrentStoredDisplayCmd<CmdCtxTypesT>(PhantomData<CmdCtxTypesT>);
13
14#[cfg(not(feature = "error_reporting"))]
15impl<CmdCtxTypesT> StatesCurrentStoredDisplayCmd<CmdCtxTypesT>
16where
17    CmdCtxTypesT: CmdCtxTypes,
18{
19    /// Displays [`StatesCurrentStored`]s from storage.
20    ///
21    /// [`StatesDiscoverCmd`] must have run prior to this command to read the
22    /// state.
23    ///
24    /// [`StatesDiscoverCmd`]: crate::StatesDiscoverCmd
25    pub async fn exec<'ctx>(
26        cmd_ctx: &mut CmdCtxSpsf<'ctx, CmdCtxTypesT>,
27    ) -> Result<
28        CmdOutcome<StatesCurrentStored, <CmdCtxTypesT as CmdCtxTypes>::AppError>,
29        <CmdCtxTypesT as CmdCtxTypes>::AppError,
30    >
31    where
32        CmdCtxTypesT: 'ctx,
33    {
34        let states_current_stored_result = StatesCurrentReadCmd::exec(cmd_ctx).await;
35        let output = cmd_ctx.output_mut();
36
37        match states_current_stored_result {
38            Ok(states_current_cmd_outcome) => {
39                if let Some(states_current_stored) = states_current_cmd_outcome.value() {
40                    output.present(states_current_stored).await?;
41                }
42                Ok(states_current_cmd_outcome)
43            }
44            Err(e) => {
45                output.write_err(&e).await?;
46                Err(e)
47            }
48        }
49    }
50}
51
52// Pending: <https://github.com/rust-lang/rust/issues/115590>
53#[cfg(feature = "error_reporting")]
54impl<CmdCtxTypesT> StatesCurrentStoredDisplayCmd<CmdCtxTypesT>
55where
56    CmdCtxTypesT: CmdCtxTypes,
57    <CmdCtxTypesT as CmdCtxTypes>::AppError: miette::Diagnostic,
58{
59    /// Displays [`StatesCurrentStored`]s from storage.
60    ///
61    /// [`StatesDiscoverCmd`] must have run prior to this command to read the
62    /// state.
63    ///
64    /// [`StatesDiscoverCmd`]: crate::StatesDiscoverCmd
65    pub async fn exec<'ctx>(
66        cmd_ctx: &mut CmdCtxSpsf<'ctx, CmdCtxTypesT>,
67    ) -> Result<
68        CmdOutcome<StatesCurrentStored, <CmdCtxTypesT as CmdCtxTypes>::AppError>,
69        <CmdCtxTypesT as CmdCtxTypes>::AppError,
70    >
71    where
72        CmdCtxTypesT: 'ctx,
73    {
74        let states_current_stored_result = StatesCurrentReadCmd::exec(cmd_ctx).await;
75        let output = cmd_ctx.output_mut();
76
77        match states_current_stored_result {
78            Ok(states_current_cmd_outcome) => {
79                if let Some(states_current_stored) = states_current_cmd_outcome.value() {
80                    output.present(states_current_stored).await?;
81                }
82                Ok(states_current_cmd_outcome)
83            }
84            Err(e) => {
85                output.write_err(&e).await?;
86                Err(e)
87            }
88        }
89    }
90}
91
92impl<CmdCtxTypesT> Default for StatesCurrentStoredDisplayCmd<CmdCtxTypesT> {
93    fn default() -> Self {
94        Self(PhantomData)
95    }
96}