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 decisions;
16pub mod github;
17pub mod issues;
18
19/// A named, versioned interface between boards.
20pub trait Contract: 'static {
21 /// Stable lower-case name, such as `issues`: ASCII letters, digits and
22 /// hyphens, starting with a letter.
23 const NAME: &'static str;
24 /// Starts at 1. A breaking change is a new version with its own types.
25 const VERSION: u32;
26}
27
28/// A notification that something happened. Delivered at most once to every
29/// running board that subscribes to it, with no answer and no replay.
30pub trait Event: 'static {
31 type Contract: Contract;
32 /// The event's name within its contract, such as `changed`.
33 const NAME: &'static str;
34}
35
36/// A question with a typed answer, routed to the contract's one responder.
37pub trait Request: 'static {
38 type Contract: Contract;
39 /// The request's name within its contract, such as `resolve`.
40 const NAME: &'static str;
41 type Response: 'static;
42}
43
44/// Whether a message is an [`Event`] or a [`Request`].
45#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
46pub enum MessageKind {
47 Event,
48 Request,
49}
50
51/// The identity of one message type: contract, version, name and kind.
52#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
53#[non_exhaustive]
54pub struct MessageKey {
55 pub contract: &'static str,
56 pub version: u32,
57 pub message: &'static str,
58 pub kind: MessageKind,
59}
60
61impl MessageKey {
62 pub const fn event<E: Event>() -> Self {
63 Self {
64 contract: <E::Contract as Contract>::NAME,
65 version: <E::Contract as Contract>::VERSION,
66 message: E::NAME,
67 kind: MessageKind::Event,
68 }
69 }
70
71 pub const fn request<R: Request>() -> Self {
72 Self {
73 contract: <R::Contract as Contract>::NAME,
74 version: <R::Contract as Contract>::VERSION,
75 message: R::NAME,
76 kind: MessageKind::Request,
77 }
78 }
79
80 /// `issues@1/resolve`: the contract, its version and the message, as
81 /// logs, errors and the documented inventory show it.
82 pub fn label(&self) -> String {
83 format!("{}@{}/{}", self.contract, self.version, self.message)
84 }
85
86 /// Whether the contract and message names are well formed and the
87 /// version is at least 1.
88 pub fn is_valid(&self) -> bool {
89 self.version >= 1 && valid_name(self.contract) && valid_name(self.message)
90 }
91}
92
93fn valid_name(name: &str) -> bool {
94 name.starts_with(|first: char| first.is_ascii_lowercase())
95 && name
96 .bytes()
97 .all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit() || byte == b'-')
98}
99
100#[cfg(test)]
101mod tests;