Skip to main content

dekopon_shell/
proposal.rs

1use serde_json::Value;
2
3#[must_use]
4pub struct CommandProposal {
5    pub capability: String,
6    pub input: Value,
7    pub secret_use: Option<dekopon_core::SecretUseProposal>,
8    pub report: Option<CommandReport>,
9}
10impl CommandProposal {
11    pub fn new(
12        capability: impl Into<String>,
13        input: Value,
14        secret_use: Option<dekopon_core::SecretUseProposal>,
15    ) -> Self {
16        Self {
17            capability: capability.into(),
18            input,
19            secret_use,
20            report: None,
21        }
22    }
23}
24#[derive(Clone, Copy, Debug)]
25pub enum CommandReportOutcome {
26    Succeeded,
27    Denied,
28    Failed,
29    Cancelled,
30}
31#[must_use]
32pub struct CommandReport {
33    finish: Option<Box<dyn FnOnce(CommandReportOutcome) + Send + Sync>>,
34}
35impl CommandReport {
36    pub fn new(finish: impl FnOnce(CommandReportOutcome) + Send + Sync + 'static) -> Self {
37        Self {
38            finish: Some(Box::new(finish)),
39        }
40    }
41    pub fn complete(mut self, outcome: CommandReportOutcome) {
42        if let Some(finish) = self.finish.take() {
43            finish(outcome);
44        }
45    }
46}
47impl std::fmt::Debug for CommandReport {
48    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
49        formatter.write_str("CommandReport")
50    }
51}
52impl Drop for CommandReport {
53    fn drop(&mut self) {
54        if let Some(finish) = self.finish.take() {
55            finish(CommandReportOutcome::Failed);
56        }
57    }
58}