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 did_ma::Message;
11
12/// Default inbox capacity for services.
13pub const DEFAULT_INBOX_CAPACITY: usize = 256;
14
15/// Default protocol ID for unqualified send/request calls.
16pub const DEFAULT_DELIVERY_PROTOCOL_ID: &str = "/ma/inbox/0.0.1";
17
18/// Shared interface for ma transport endpoints.
19///
20/// Each implementation provides inbox/outbox
21/// messaging and advertises its registered services for DID documents.
22#[async_trait]
23pub trait MaEndpoint: Send + Sync {
24 /// The endpoint's public identifier (hex string).
25 fn id(&self) -> String;
26
27 /// Register a service protocol and return an [`Inbox`] for receiving messages.
28 fn service(&mut self, protocol: &str) -> Inbox<Message>;
29
30 /// Return service strings for all registered protocols.
31 ///
32 /// Each entry is suitable for inclusion in a DID document's `ma.services` array.
33 fn services(&self) -> Vec<String>;
34
35 /// Return service strings as a JSON array value.
36 fn services_json(&self) -> serde_json::Value {
37 serde_json::Value::Array(
38 self.services()
39 .into_iter()
40 .map(serde_json::Value::String)
41 .collect(),
42 )
43 }
44
45 /// Fire-and-forget to a target on a specific protocol.
46 async fn send_to(&self, target: &str, protocol: &str, message: &Message) -> Result<()>;
47
48 /// Fire-and-forget to a target on the default inbox protocol.
49 async fn send(&self, target: &str, message: &Message) -> Result<()> {
50 self.send_to(target, DEFAULT_DELIVERY_PROTOCOL_ID, message)
51 .await
52 }
53}