Skip to main content

subspace_contracts/
lib.rs

1//! Board-to-board contracts. A contract is an interface that no board owns:
2//! a stable name, a version and the typed messages boards exchange through
3//! it. A board that publishes or answers a contract and a board that listens
4//! to or asks it both depend on this crate, never on each other, so either
5//! can be absent or disabled.
6//!
7//! Two kinds of message exist. An [`Event`] announces that something happened
8//! and expects no answer. A [`Request`] asks the one board that responds to
9//! the contract for an immediate answer from the state it holds. Messages
10//! carry identities and small summaries, never credentials or bulk content.
11//!
12//! The broker that routes messages, and the declarations boards make at
13//! registration, live in board-kit (`subspace_dashboard::contracts`).
14
15pub mod issues;
16
17/// A named, versioned interface between boards.
18pub trait Contract: 'static {
19    /// Stable lower-case name, such as `issues`: ASCII letters, digits and
20    /// hyphens, starting with a letter.
21    const NAME: &'static str;
22    /// Starts at 1. A breaking change is a new version with its own types.
23    const VERSION: u32;
24}
25
26/// A notification that something happened. Delivered at most once to every
27/// running board that subscribes to it, with no answer and no replay.
28pub trait Event: 'static {
29    type Contract: Contract;
30    /// The event's name within its contract, such as `changed`.
31    const NAME: &'static str;
32}
33
34/// A question with a typed answer, routed to the contract's one responder.
35pub trait Request: 'static {
36    type Contract: Contract;
37    /// The request's name within its contract, such as `resolve`.
38    const NAME: &'static str;
39    type Response: 'static;
40}
41
42/// Whether a message is an [`Event`] or a [`Request`].
43#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
44pub enum MessageKind {
45    Event,
46    Request,
47}
48
49/// The identity of one message type: contract, version, name and kind.
50#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
51#[non_exhaustive]
52pub struct MessageKey {
53    pub contract: &'static str,
54    pub version: u32,
55    pub message: &'static str,
56    pub kind: MessageKind,
57}
58
59impl MessageKey {
60    pub const fn event<E: Event>() -> Self {
61        Self {
62            contract: <E::Contract as Contract>::NAME,
63            version: <E::Contract as Contract>::VERSION,
64            message: E::NAME,
65            kind: MessageKind::Event,
66        }
67    }
68
69    pub const fn request<R: Request>() -> Self {
70        Self {
71            contract: <R::Contract as Contract>::NAME,
72            version: <R::Contract as Contract>::VERSION,
73            message: R::NAME,
74            kind: MessageKind::Request,
75        }
76    }
77
78    /// `issues@1/resolve`: the contract, its version and the message, as
79    /// logs, errors and the documented inventory show it.
80    pub fn label(&self) -> String {
81        format!("{}@{}/{}", self.contract, self.version, self.message)
82    }
83
84    /// Whether the contract and message names are well formed and the
85    /// version is at least 1.
86    pub fn is_valid(&self) -> bool {
87        self.version >= 1 && valid_name(self.contract) && valid_name(self.message)
88    }
89}
90
91fn valid_name(name: &str) -> bool {
92    name.starts_with(|first: char| first.is_ascii_lowercase())
93        && name
94            .bytes()
95            .all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit() || byte == b'-')
96}
97
98#[cfg(test)]
99mod tests;