1use std::sync::Arc;
10
11use secret_rules::audit::ScopeRef;
12use subc_protocol::scope::ScopeStamp;
13
14use super::wire::Coverage;
15
16#[derive(Clone, PartialEq, Eq)]
19pub struct SecretScope {
20 stamp: Arc<ScopeStamp>,
21 owner: String,
22 kind: String,
23 reference: String,
24}
25
26impl SecretScope {
27 pub fn from_stamp(stamp: &ScopeStamp) -> Self {
29 Self {
30 stamp: Arc::new(stamp.clone()),
31 owner: render_owner(&stamp.owner),
35 kind: match stamp.kind {
36 subc_protocol::scope::ScopeKind::Head => "head",
37 subc_protocol::scope::ScopeKind::Worker => "worker",
38 subc_protocol::scope::ScopeKind::Ephemeral => "ephemeral",
39 subc_protocol::scope::ScopeKind::Federated => "federated",
40 _ => "unsupported",
41 }
42 .to_string(),
43 reference: stamp.scope_ref.clone(),
44 }
45 }
46
47 pub fn stamp(&self) -> &ScopeStamp {
49 &self.stamp
50 }
51
52 pub fn owner(&self) -> &str {
54 &self.owner
55 }
56
57 pub fn kind(&self) -> &str {
59 &self.kind
60 }
61
62 pub fn reference(&self) -> &str {
64 &self.reference
65 }
66
67 pub fn to_scope_ref(&self) -> ScopeRef {
69 ScopeRef {
70 owner: self.owner.clone(),
71 kind: self.kind.clone(),
72 reference: self.reference.clone(),
73 }
74 }
75
76 pub fn coverage(&self) -> Coverage {
80 Coverage::Scope
81 }
82}
83
84fn render_owner(owner: &subc_protocol::Principal) -> String {
87 match owner {
88 subc_protocol::Principal::Reserved { module_id } => format!("reserved:{module_id}"),
89 subc_protocol::Principal::Direct => "direct".to_string(),
90 subc_protocol::Principal::Unverified => "unverified".to_string(),
91 }
92}
93
94impl std::hash::Hash for SecretScope {
95 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
96 self.owner.hash(state);
99 self.kind.hash(state);
100 self.reference.hash(state);
101 }
102}
103
104impl std::fmt::Debug for SecretScope {
105 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
106 f.debug_struct("SecretScope")
107 .field("owner", &self.owner)
108 .field("kind", &self.kind)
109 .field("ref", &self.reference)
110 .finish()
111 }
112}
113
114impl std::fmt::Display for SecretScope {
115 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
116 write!(f, "{}:{}:{}", self.owner, self.kind, self.reference)
117 }
118}