Skip to main content

greentic_ext_runtime/
broker.rs

1use greentic_extension_sdk_contract::ExtensionKind;
2
3pub type BrokerResult<T> = Result<T, BrokerError>;
4
5#[derive(Debug, thiserror::Error)]
6pub enum BrokerError {
7    #[error("permission denied: {0}")]
8    PermissionDenied(String),
9
10    #[error("target extension not loaded: {0}")]
11    TargetNotLoaded(String),
12
13    #[error("function not found: {0}")]
14    FunctionNotFound(String),
15
16    #[error("max call depth exceeded")]
17    MaxDepthExceeded,
18
19    #[error("deadline exceeded")]
20    Deadline,
21}
22
23pub const MAX_DEPTH: u32 = 8;
24
25#[derive(Debug, Default)]
26pub struct Broker;
27
28impl Broker {
29    #[must_use]
30    pub const fn new() -> Self {
31        Self
32    }
33
34    pub fn check_permission(
35        &self,
36        caller_id: &str,
37        allowlist: &[String],
38        target_kind: ExtensionKind,
39    ) -> BrokerResult<()> {
40        if allowlist.iter().any(|k| k == target_kind.dir_name()) {
41            Ok(())
42        } else {
43            Err(BrokerError::PermissionDenied(format!(
44                "{caller_id} may not call {target_kind:?} extensions"
45            )))
46        }
47    }
48
49    pub fn check_depth(&self, depth: u32) -> BrokerResult<()> {
50        if depth >= MAX_DEPTH {
51            Err(BrokerError::MaxDepthExceeded)
52        } else {
53            Ok(())
54        }
55    }
56}