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