1pub mod dwn; pub mod identity;
6pub mod protocols;
7pub mod vc; pub use identity::{DIDDocument, DIDManager, IdentityManager, Web5Error, Web5Result, DID};
12pub use protocols::{ProtocolDefinition, ProtocolHandler, ProtocolManager};
13
14use std::collections::HashMap;
15
16#[derive(Clone, Debug)]
18pub struct Web5Config {
19 pub enabled: bool,
21 pub did_method: String,
23 pub dwn_url: Option<String>,
25 pub use_local_storage: bool,
27}
28
29impl Default for Web5Config {
30 fn default() -> Self {
31 Self {
32 enabled: true,
33 did_method: "ion".to_string(),
34 dwn_url: None,
35 use_local_storage: true,
36 }
37 }
38}
39
40pub struct Web5Manager {
43 config: Web5Config,
45 did_manager: identity::DIDManager,
47 protocol_manager: protocols::ProtocolManager,
49}
50
51impl Web5Manager {
52 pub fn new(config: Web5Config) -> Web5Result<Self> {
54 let did_manager = identity::DIDManager::new(&config.did_method);
55 let protocol_manager = protocols::ProtocolManager::new();
56
57 Ok(Self {
58 config,
59 did_manager,
60 protocol_manager,
61 })
62 }
63
64 pub fn did_manager(&self) -> &identity::DIDManager {
66 &self.did_manager
67 }
68
69 pub fn protocol_manager(&self) -> &protocols::ProtocolManager {
71 &self.protocol_manager
72 }
73
74 pub fn initialize(&mut self) -> Web5Result<()> {
76 let profile_handler = protocols::ProfileProtocolHandler::new();
78 self.protocol_manager
79 .register_protocol(Box::new(profile_handler))?;
80
81 if self.config.use_local_storage && self.did_manager.get_default_did()?.is_none() {
83 let did = self.did_manager.create_did()?;
84 self.did_manager.set_default_did(&did.id)?
85 }
86
87 Ok(())
88 }
89
90 pub fn status(&self) -> Web5Result<Web5Status> {
92 let did_count = self.did_manager.dids()?.len();
93 let protocol_count = self.protocol_manager.get_all_protocols().len();
94
95 Ok(Web5Status {
96 enabled: self.config.enabled,
97 did_count,
98 protocol_count,
99 dwn_connected: self.config.dwn_url.is_some(),
100 })
101 }
102
103 pub fn get_metrics(&self) -> Web5Result<HashMap<String, String>> {
105 let mut metrics = HashMap::new();
106 metrics.insert(
107 "dids".to_string(),
108 self.did_manager.dids()?.len().to_string(),
109 );
110 metrics.insert(
111 "protocols".to_string(),
112 self.protocol_manager.get_all_protocols().len().to_string(),
113 );
114 metrics.insert(
115 "dwn_connected".to_string(),
116 self.config.dwn_url.is_some().to_string(),
117 );
118
119 Ok(metrics)
120 }
121}
122
123#[derive(Clone, Debug)]
125pub struct Web5Status {
126 pub enabled: bool,
128 pub did_count: usize,
130 pub protocol_count: usize,
132 pub dwn_connected: bool,
134}
135
136#[cfg(test)]
137mod tests {
138 use super::*;
140
141 #[test]
142 fn test_web5_manager_creation() -> Result<(), Box<dyn std::error::Error>> {
143 let config = Web5Config::default();
144 let manager = Web5Manager::new(config)?;
145
146 assert!(manager.config.enabled);
147 assert_eq!(manager.config.did_method, "ion");
148 Ok(())
149 }
150
151 #[test]
152 fn test_web5_status() -> Result<(), Box<dyn std::error::Error>> {
153 let config = Web5Config::default();
154 let manager = Web5Manager::new(config)?;
155
156 let status = manager.status()?;
157 assert!(status.enabled);
158 assert_eq!(status.did_count, 0);
159 assert_eq!(status.protocol_count, 0);
160 assert!(!status.dwn_connected);
161 Ok(())
162 }
163}