pub mod common;
pub mod ddbc;
pub mod frbc;
pub mod id;
pub mod ombc;
pub mod pebc;
pub mod ppbc;
pub mod time;
pub use common::*;
pub use id::{Id, InvalidId};
pub use time::{Duration, InvalidTimestamp, TimeOutOfRange, Timestamp};
use alloc::borrow::{Cow, ToOwned};
use alloc::string::String;
use serde::{Deserialize, Serialize};
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct ProtocolVersion(Cow<'static, str>);
impl ProtocolVersion {
pub const V1_0_0: Self = Self::from_static("1.0.0");
pub const V0_0_2_BETA: Self = Self::from_static("0.0.2-beta");
pub const V1_0_0_CONNECT: Self = Self::from_static("v1.0.0");
pub const V0_0_2_BETA_CONNECT: Self = Self::from_static("v0.0.2-beta");
#[must_use]
pub fn new(s: impl Into<String>) -> Self {
Self(Cow::Owned(s.into()))
}
#[must_use]
pub const fn from_static(s: &'static str) -> Self {
Self(Cow::Borrowed(s))
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
#[must_use]
pub fn canonical(&self) -> &str {
self.0.strip_prefix('v').unwrap_or(&self.0)
}
#[must_use]
pub fn matches(&self, other: &Self) -> bool {
self.canonical() == other.canonical()
}
#[must_use]
pub fn wire_profile(&self) -> Option<WireProfile> {
match self.canonical() {
"1.0.0" => Some(WireProfile::V1_0_0),
"0.0.2-beta" => Some(WireProfile::V0_0_2Beta),
_ => None,
}
}
#[must_use]
pub fn is_supported(&self) -> bool {
self.wire_profile().is_some()
}
}
impl From<&str> for ProtocolVersion {
fn from(s: &str) -> Self {
Self(Cow::Owned(s.to_owned()))
}
}
impl From<String> for ProtocolVersion {
fn from(s: String) -> Self {
Self(Cow::Owned(s))
}
}
impl core::fmt::Display for ProtocolVersion {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(&self.0)
}
}
impl core::fmt::Debug for ProtocolVersion {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "ProtocolVersion({:?})", self.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
#[non_exhaustive]
pub enum WireProfile {
#[default]
V1_0_0,
V0_0_2Beta,
}
impl WireProfile {
pub const ALL: [Self; 2] = [Self::V1_0_0, Self::V0_0_2Beta];
#[must_use]
pub const fn version_str(self) -> &'static str {
match self {
Self::V1_0_0 => "1.0.0",
Self::V0_0_2Beta => "0.0.2-beta",
}
}
#[must_use]
pub const fn connect_version_str(self) -> &'static str {
match self {
Self::V1_0_0 => "v1.0.0",
Self::V0_0_2Beta => "v0.0.2-beta",
}
}
#[must_use]
pub const fn version(self) -> ProtocolVersion {
match self {
Self::V1_0_0 => ProtocolVersion::V1_0_0,
Self::V0_0_2Beta => ProtocolVersion::V0_0_2_BETA,
}
}
#[must_use]
pub const fn connect_version(self) -> ProtocolVersion {
match self {
Self::V1_0_0 => ProtocolVersion::V1_0_0_CONNECT,
Self::V0_0_2Beta => ProtocolVersion::V0_0_2_BETA_CONNECT,
}
}
#[must_use]
pub const fn ddbc_system_description_carries_demand_rate(self) -> bool {
matches!(self, Self::V0_0_2Beta)
}
#[must_use]
pub const fn all() -> &'static [Self] {
&[Self::V1_0_0, Self::V0_0_2Beta]
}
}
impl core::fmt::Display for WireProfile {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(self.version_str())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_constants_are_the_strings_the_two_specifications_write() {
assert_eq!(ProtocolVersion::V1_0_0.as_str(), "1.0.0");
assert_eq!(ProtocolVersion::V1_0_0_CONNECT.as_str(), "v1.0.0");
assert_eq!(ProtocolVersion::V0_0_2_BETA.as_str(), "0.0.2-beta");
assert_eq!(ProtocolVersion::V0_0_2_BETA_CONNECT.as_str(), "v0.0.2-beta");
assert!(ProtocolVersion::V1_0_0.matches(&ProtocolVersion::V1_0_0_CONNECT));
assert!(ProtocolVersion::V0_0_2_BETA.matches(&ProtocolVersion::V0_0_2_BETA_CONNECT));
assert!(!ProtocolVersion::V1_0_0.matches(&ProtocolVersion::V0_0_2_BETA));
for profile in WireProfile::all() {
assert_eq!(profile.version().as_str(), profile.version_str());
assert_eq!(
profile.connect_version().as_str(),
profile.connect_version_str()
);
}
assert_eq!(
serde_json::to_string(&ProtocolVersion::V0_0_2_BETA).unwrap(),
"\"0.0.2-beta\""
);
assert_eq!(
serde_json::from_str::<ProtocolVersion>("\"v1.0.0\"").unwrap(),
ProtocolVersion::V1_0_0_CONNECT
);
}
#[test]
fn version_strings_map_to_profiles_exactly() {
for (s, expected) in [
("1.0.0", Some(WireProfile::V1_0_0)),
("v1.0.0", Some(WireProfile::V1_0_0)),
("0.0.2-beta", Some(WireProfile::V0_0_2Beta)),
("v0.0.2-beta", Some(WireProfile::V0_0_2Beta)),
("1.0.1", None),
("1.1", None),
("", None),
("latest", None),
] {
assert_eq!(ProtocolVersion::new(s).wire_profile(), expected, "{s:?}");
}
}
#[test]
fn the_two_specifications_spell_one_version_two_ways_and_both_are_understood() {
let connect = ProtocolVersion::new("v1.0.0");
let handshake = ProtocolVersion::new("1.0.0");
assert!(connect.matches(&handshake));
assert!(handshake.matches(&connect));
assert_eq!(connect.canonical(), "1.0.0");
assert_eq!(handshake.canonical(), "1.0.0");
assert_eq!(connect.as_str(), "v1.0.0");
assert_eq!(handshake.as_str(), "1.0.0");
assert_eq!(WireProfile::V1_0_0.version_str(), "1.0.0");
assert_eq!(WireProfile::V1_0_0.connect_version_str(), "v1.0.0");
assert_eq!(WireProfile::V0_0_2Beta.version_str(), "0.0.2-beta");
assert_eq!(WireProfile::V0_0_2Beta.connect_version_str(), "v0.0.2-beta");
for profile in WireProfile::all() {
assert!(profile.version().matches(&profile.connect_version()));
assert_eq!(profile.connect_version().wire_profile(), Some(*profile));
}
}
#[test]
fn tolerating_the_prefix_is_not_tolerating_a_range() {
for s in [
"1.0",
"1",
"v1",
"1.0.1",
"^1.0.0",
"1.0.0-rc1",
"vv1.0.0",
"",
] {
assert_eq!(ProtocolVersion::new(s).wire_profile(), None, "{s:?}");
assert!(
!ProtocolVersion::new(s).matches(&ProtocolVersion::new("1.0.0")),
"{s:?} must not match 1.0.0"
);
}
}
#[test]
fn only_the_beta_profile_carries_the_ddbc_demand_rate() {
assert!(WireProfile::V0_0_2Beta.ddbc_system_description_carries_demand_rate());
assert!(!WireProfile::V1_0_0.ddbc_system_description_carries_demand_rate());
}
}