1use std::collections::BTreeMap;
4
5use serde::{Deserialize, Serialize};
6
7use crate::coroutine::Value;
8
9#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
11pub struct LayerId(pub String);
12
13impl From<&str> for LayerId {
14 fn from(value: &str) -> Self {
15 Self(value.to_string())
16 }
17}
18
19pub trait GuardLayer {
21 type Resource: Clone;
23 type Evidence: Clone;
25
26 fn open_(&mut self, layer: &LayerId) -> Result<(Self::Resource, Self::Evidence), String>;
32
33 fn close(&mut self, layer: &LayerId, evidence: Self::Evidence) -> Result<(), String>;
39
40 fn encode_evidence(evidence: &Self::Evidence) -> Result<Value, String>;
46
47 fn decode_evidence(value: &Value) -> Result<Self::Evidence, String>;
53
54 #[allow(non_snake_case)]
60 fn encodeEvidence(evidence: &Self::Evidence) -> Result<Value, String> {
61 Self::encode_evidence(evidence)
62 }
63
64 #[allow(non_snake_case)]
70 fn decodeEvidence(value: &Value) -> Result<Self::Evidence, String> {
71 Self::decode_evidence(value)
72 }
73}
74
75#[derive(Debug, Clone, Default, Serialize, Deserialize)]
77pub struct InMemoryGuardLayer {
78 pub resources: BTreeMap<LayerId, Value>,
80}
81
82impl GuardLayer for InMemoryGuardLayer {
83 type Resource = Value;
84 type Evidence = Value;
85
86 fn open_(&mut self, layer: &LayerId) -> Result<(Self::Resource, Self::Evidence), String> {
87 let resource = self
88 .resources
89 .get(layer)
90 .cloned()
91 .ok_or_else(|| format!("unknown guard layer {}", layer.0))?;
92 Ok((resource.clone(), resource))
93 }
94
95 fn close(&mut self, layer: &LayerId, evidence: Self::Evidence) -> Result<(), String> {
96 if !self.resources.contains_key(layer) {
97 return Err(format!("unknown guard layer {}", layer.0));
98 }
99 let _ = evidence;
102 Ok(())
103 }
104
105 fn encode_evidence(evidence: &Self::Evidence) -> Result<Value, String> {
106 Ok(evidence.clone())
107 }
108
109 fn decode_evidence(value: &Value) -> Result<Self::Evidence, String> {
110 Ok(value.clone())
111 }
112}