Skip to main content

ma_core/
service.rs

1//! Service trait for ma endpoint protocol handlers.
2//!
3//! A `Service` is analogous to an entry in `/etc/services`: a named protocol
4//! on a ma endpoint. Register services on an `MaEndpoint` to handle incoming
5//! connections on their protocol.
6
7/// Trait that all ma services must implement.
8///
9/// Each service declares its protocol identifier and provides a handler for
10/// incoming connections. Built-in services ship with ma-core; applications
11/// add custom services via this trait.
12///
13/// # Examples
14///
15/// ```
16/// use ma_core::Service;
17///
18/// struct MyService;
19///
20/// impl Service for MyService {
21///     fn protocol(&self) -> &[u8] { b"/ma/my-service/0.0.1" }
22/// }
23/// ```
24pub trait Service: Send + Sync {
25    /// The protocol identifier for this service.
26    fn protocol(&self) -> &[u8];
27}
28
29// ─── Well-known protocol constants (ma-core scope) ──────────────────────────
30
31pub const INBOX_PROTOCOL_ID: &str = "/ma/inbox/0.0.1";
32pub const RPC_PROTOCOL_ID: &str = "/ma/rpc/0.0.1";
33pub const IPFS_PROTOCOL_ID: &str = "/ma/ipfs/0.0.1";
34pub const CRUD_PROTOCOL_ID: &str = "/ma/crud/0.0.1";
35
36// ─── Message types (routing / dispatch category) ────────────────────────────
37
38pub const MESSAGE_TYPE_BROADCAST: &str = "application/vnd.ma.broadcast";
39pub const MESSAGE_TYPE_CHAT: &str = "application/vnd.ma.chat";
40pub const MESSAGE_TYPE_EMOTE: &str = "application/vnd.ma.emote";
41pub const MESSAGE_TYPE_MESSAGE: &str = "application/vnd.ma.message";
42/// DID-document publish request — transmits an IPNS secret key so the
43/// publisher can sign/publish the caller's `did:ma` document. Requires the
44/// `identity-publish` ACL capability (see `ma_core::acl::CAP_IDENTITY_PUBLISH`).
45pub const MESSAGE_TYPE_IDENTITY_PUBLISH_REQUEST: &str =
46    "application/vnd.ma.identity.publish.request";
47/// Generic IPFS content-store request — fire-and-forget. Requires the
48/// `ipfs` ACL capability (see `ma_core::acl::CAP_IPFS`).
49pub const MESSAGE_TYPE_IPFS_REQUEST: &str = "application/vnd.ma.ipfs.request";
50pub const MESSAGE_TYPE_DOC: &str = "application/vnd.ma.doc";
51pub const MESSAGE_TYPE_RPC: &str = "application/vnd.ma.rpc.request";
52pub const MESSAGE_TYPE_RPC_REPLY: &str = "application/vnd.ma.rpc.reply";
53
54// ─── CRUD message types (/ma/crud/0.0.1) ────────────────────────────────────
55//
56// Operation is encoded in the CBOR payload, not the message type:
57//   GET:    [":get",    ":path"]
58//   SET:    [":path",   value]        value = scalar or "/ipfs/…", "/ipns/…", "/ipld/…"
59//   DELETE: [":delete", ":path"]
60
61pub const MESSAGE_TYPE_CRUD: &str = "application/vnd.ma.crud.request";
62pub const MESSAGE_TYPE_CRUD_REPLY: &str = "application/vnd.ma.crud.reply";
63
64// ─── Content types (inner payload format) ───────────────────────────────────
65
66pub const CONTENT_TYPE_CBOR: &str = "application/cbor";
67/// CBOR term — either a bare atom (`:ok`, `:pong`) or a tuple (CBOR array whose first element
68/// is a dispatchable atom, e.g. `[:ok, data]` or `[:error, reason]`).
69/// Used as `contentType` for RPC and CRUD messages.
70pub const CONTENT_TYPE_TERM: &str = "application/vnd.ma.term";
71/// Raw CBOR data payload — e.g. an `EntityNode` struct or a `Vec<String>` names list.
72/// The `+cbor` suffix follows RFC 6838 §4.2.8 structured-syntax conventions.
73pub const CONTENT_TYPE_TERM_CBOR: &str = "application/vnd.ma.term+cbor";
74/// Inline YAML string — the CBOR payload is a text string containing a
75/// UTF-8 YAML document.  Suitable for config values (scalars, sequences,
76/// mappings) that do not need to be stored as separate IPFS objects.
77pub const CONTENT_TYPE_TERM_YAML: &str = "application/vnd.ma.term+yaml";