hanzo_did/
service.rs

1//! W3C DID Service Endpoints
2
3use serde::{Deserialize, Serialize};
4use serde_json::Value;
5use std::collections::HashMap;
6
7/// Service endpoint in a DID document
8#[derive(Debug, Clone, Serialize, Deserialize)]
9#[serde(rename_all = "camelCase")]
10pub struct Service {
11    /// Service ID
12    pub id: String,
13
14    /// Service type(s)
15    #[serde(rename = "type")]
16    pub type_: ServiceType,
17
18    /// Service endpoint(s)
19    pub service_endpoint: ServiceEndpoint,
20
21    /// Additional properties
22    #[serde(flatten)]
23    pub properties: HashMap<String, Value>,
24}
25
26/// Service types
27#[derive(Debug, Clone, Serialize, Deserialize)]
28#[serde(untagged)]
29pub enum ServiceType {
30    Single(String),
31    Multiple(Vec<String>),
32}
33
34/// Service endpoints
35#[derive(Debug, Clone, Serialize, Deserialize)]
36#[serde(untagged)]
37pub enum ServiceEndpoint {
38    Uri(String),
39    Multiple(Vec<String>),
40    Map(HashMap<String, Value>),
41}
42
43impl Service {
44    /// Create a new service
45    pub fn new(id: String, type_: String, endpoint: String) -> Self {
46        Self {
47            id,
48            type_: ServiceType::Single(type_),
49            service_endpoint: ServiceEndpoint::Uri(endpoint),
50            properties: HashMap::new(),
51        }
52    }
53
54    /// Create a Hanzo node service
55    pub fn hanzo_node(did: &str, endpoint: String) -> Self {
56        Self::new(
57            format!("{did}#hanzo-node"),
58            "HanzoNode".to_string(),
59            endpoint,
60        )
61    }
62
63    /// Create an LLM provider service
64    pub fn llm_provider(did: &str, endpoint: String) -> Self {
65        Self::new(
66            format!("{did}#llm-provider"),
67            "LLMProvider".to_string(),
68            endpoint,
69        )
70    }
71
72    /// Create a messaging service
73    pub fn messaging(did: &str, endpoint: String) -> Self {
74        Self::new(
75            format!("{did}#messaging"),
76            "MessagingService".to_string(),
77            endpoint,
78        )
79    }
80}