ma_core/endpoint.rs
1//! Endpoint trait.
2//!
3//! [`MaEndpoint`] defines the shared interface for all ma transport endpoints.
4//! See [`crate::iroh`] for the iroh-backed implementation.
5
6use async_trait::async_trait;
7
8use crate::error::Result;
9use crate::inbox::Inbox;
10use crate::service::INBOX_PROTOCOL_ID;
11use did_ma::Message;
12
13/// Default inbox capacity for services.
14pub const DEFAULT_INBOX_CAPACITY: usize = 256;
15
16/// Default protocol ID for unqualified send/request calls.
17pub const DEFAULT_DELIVERY_PROTOCOL_ID: &str = INBOX_PROTOCOL_ID;
18
19/// Shared interface for ma transport endpoints.
20///
21/// Each implementation provides inbox/outbox
22/// messaging and advertises its registered services for DID documents.
23#[async_trait]
24pub trait MaEndpoint: Send + Sync {
25 /// The endpoint's public identifier (hex string).
26 fn id(&self) -> String;
27
28 /// Register a service protocol and return an [`Inbox`] for receiving messages.
29 ///
30 /// Implementations should ensure the service is reachable for inbound delivery
31 /// once it has been registered, so callers do not need a second explicit
32 /// "listen" step in the common case.
33 fn service(&mut self, protocol: &str) -> Inbox<Message>;
34
35 /// Return service strings for all registered protocols.
36 ///
37 /// Each entry is suitable for inclusion in a DID document's `ma.services` array.
38 fn services(&self) -> Vec<String>;
39
40 /// Return service strings as a JSON array value.
41 fn services_json(&self) -> serde_json::Value {
42 serde_json::Value::Array(
43 self.services()
44 .into_iter()
45 .map(serde_json::Value::String)
46 .collect(),
47 )
48 }
49
50 /// Fire-and-forget to a target on a specific protocol.
51 async fn send_to(&self, target: &str, protocol: &str, message: &Message) -> Result<()>;
52
53 /// Fire-and-forget to a target on the default inbox protocol.
54 async fn send(&self, target: &str, message: &Message) -> Result<()> {
55 self.send_to(target, DEFAULT_DELIVERY_PROTOCOL_ID, message)
56 .await
57 }
58}