pub mod issues;
pub trait Contract: 'static {
const NAME: &'static str;
const VERSION: u32;
}
pub trait Event: 'static {
type Contract: Contract;
const NAME: &'static str;
}
pub trait Request: 'static {
type Contract: Contract;
const NAME: &'static str;
type Response: 'static;
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum MessageKind {
Event,
Request,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct MessageKey {
pub contract: &'static str,
pub version: u32,
pub message: &'static str,
pub kind: MessageKind,
}
impl MessageKey {
pub const fn event<E: Event>() -> Self {
Self {
contract: <E::Contract as Contract>::NAME,
version: <E::Contract as Contract>::VERSION,
message: E::NAME,
kind: MessageKind::Event,
}
}
pub const fn request<R: Request>() -> Self {
Self {
contract: <R::Contract as Contract>::NAME,
version: <R::Contract as Contract>::VERSION,
message: R::NAME,
kind: MessageKind::Request,
}
}
pub fn label(&self) -> String {
format!("{}@{}/{}", self.contract, self.version, self.message)
}
pub fn is_valid(&self) -> bool {
self.version >= 1 && valid_name(self.contract) && valid_name(self.message)
}
}
fn valid_name(name: &str) -> bool {
name.starts_with(|first: char| first.is_ascii_lowercase())
&& name
.bytes()
.all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit() || byte == b'-')
}
#[cfg(test)]
mod tests;