runar_serializer/
traits.rs1use anyhow::Result;
2use serde::{Deserialize, Serialize};
3use std::collections::HashMap;
4use std::fmt::Debug;
5use std::sync::Arc;
6
7pub use runar_keys::mobile::EnvelopeEncryptedData;
11pub use runar_keys::EnvelopeCrypto;
12
13pub type KeyStore = dyn EnvelopeCrypto;
17
18#[derive(Debug, Clone, Serialize, Deserialize)]
32pub struct LabelKeyInfo {
33 pub profile_public_keys: Vec<Vec<u8>>,
34 pub network_id: Option<String>,
35}
36
37#[derive(Serialize, Deserialize, Debug, Clone)]
43pub struct KeyMappingConfig {
44 pub label_mappings: HashMap<String, LabelKeyInfo>,
46}
47
48pub trait LabelResolver: Send + Sync {
50 fn resolve_label_info(&self, label: &str) -> Result<Option<LabelKeyInfo>>;
52
53 fn available_labels(&self) -> Vec<String>;
55
56 fn can_resolve(&self, label: &str) -> bool;
58
59 fn clone_box(&self) -> Box<dyn LabelResolver>;
61}
62
63impl LabelResolver for Box<dyn LabelResolver> {
65 fn resolve_label_info(&self, label: &str) -> Result<Option<LabelKeyInfo>> {
66 self.as_ref().resolve_label_info(label)
67 }
68
69 fn available_labels(&self) -> Vec<String> {
70 self.as_ref().available_labels()
71 }
72
73 fn can_resolve(&self, label: &str) -> bool {
74 self.as_ref().can_resolve(label)
75 }
76
77 fn clone_box(&self) -> Box<dyn LabelResolver> {
78 self.as_ref().clone_box()
79 }
80}
81
82pub struct ConfigurableLabelResolver {
84 config: KeyMappingConfig,
86}
87
88impl ConfigurableLabelResolver {
89 pub fn new(config: KeyMappingConfig) -> Self {
90 Self { config }
91 }
92}
93
94impl LabelResolver for ConfigurableLabelResolver {
95 fn resolve_label_info(&self, label: &str) -> Result<Option<LabelKeyInfo>> {
96 Ok(self.config.label_mappings.get(label).cloned())
97 }
98
99 fn available_labels(&self) -> Vec<String> {
100 self.config.label_mappings.keys().cloned().collect()
101 }
102
103 fn can_resolve(&self, label: &str) -> bool {
104 self.config.label_mappings.contains_key(label)
105 }
106
107 fn clone_box(&self) -> Box<dyn LabelResolver> {
108 Box::new(ConfigurableLabelResolver {
109 config: self.config.clone(),
110 })
111 }
112}
113
114pub trait RunarEncryptable {}
116
117pub trait RunarEncrypt: RunarEncryptable {
119 type Encrypted: RunarDecrypt<Decrypted = Self> + Serialize;
120
121 fn encrypt_with_keystore(
122 &self,
123 keystore: &Arc<KeyStore>,
124 resolver: &dyn LabelResolver,
125 ) -> Result<Self::Encrypted>;
126}
127
128pub trait RunarDecrypt {
130 type Decrypted: RunarEncrypt<Encrypted = Self>;
131
132 fn decrypt_with_keystore(&self, keystore: &Arc<KeyStore>) -> Result<Self::Decrypted>;
133}
134
135#[derive(Clone)]
144pub struct SerializationContext {
145 pub keystore: Arc<KeyStore>,
146 pub resolver: Arc<dyn LabelResolver>,
147 pub network_id: String,
148 pub profile_public_key: Option<Vec<u8>>,
149}