interplane.Get Omega
Read in your language

interplane-mv

INTERPLANE: open interoperability protocol between AI models and agent runtimes. Models express intent. Runtimes retain authority.

Brought by drakestapleton

branch main Branches Tags History

interplane-mv / rust / crates / interplane-core / src / version.rs

72 lines · 2194 bytes Raw Blame History
1//! Protocol version parsing and the major check.
2use crate::types::ErrorCode;
3use std::fmt;
4
5/// The version this implementation writes.
6pub const PROTOCOL_VERSION: &str = "0.1";
7/// The one MAJOR this implementation accepts.
8pub const SUPPORTED_MAJOR: u32 = 0;
9
10/// `MAJOR.MINOR`.
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub struct ProtocolVersion {
13 pub major: u32,
14 pub minor: u32,
15}
16
17impl ProtocolVersion {
18 /// Parse `^[0-9]+\.[0-9]+$`. Anything else is `malformed_envelope`.
19 pub fn parse(s: &str) -> Result<Self, ErrorCode> {
20 let (a, b) = s.split_once('.').ok_or(ErrorCode::MalformedEnvelope)?;
21 let digits = |x: &str| !x.is_empty() && x.bytes().all(|c| c.is_ascii_digit());
22 if !digits(a) || !digits(b) {
23 return Err(ErrorCode::MalformedEnvelope);
24 }
25 Ok(Self {
26 major: a.parse().map_err(|_| ErrorCode::MalformedEnvelope)?,
27 minor: b.parse().map_err(|_| ErrorCode::MalformedEnvelope)?,
28 })
29 }
30
31 /// A receiver implements exactly one MAJOR; any other is `unsupported_version`.
32 pub fn check_major(&self) -> Result<(), ErrorCode> {
33 if self.major == SUPPORTED_MAJOR {
34 Ok(())
35 } else {
36 Err(ErrorCode::UnsupportedVersion)
37 }
38 }
39}
40
41impl fmt::Display for ProtocolVersion {
42 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43 write!(f, "{}.{}", self.major, self.minor)
44 }
45}
46
47#[cfg(test)]
48mod tests {
49 use super::*;
50 #[test]
51 fn parse_ok_and_bad() {
52 assert_eq!(
53 ProtocolVersion::parse("0.1").unwrap(),
54 ProtocolVersion { major: 0, minor: 1 }
55 );
56 for bad in ["", "1", "a.b", "1.", ".1", "1.2.3", "-1.0", "0.1 "] {
57 assert_eq!(
58 ProtocolVersion::parse(bad),
59 Err(ErrorCode::MalformedEnvelope),
60 "{bad}"
61 );
62 }
63 }
64 #[test]
65 fn major_mismatch_rejected() {
66 assert_eq!(
67 ProtocolVersion::parse("2.0").unwrap().check_major(),
68 Err(ErrorCode::UnsupportedVersion)
69 );
70 assert!(ProtocolVersion::parse("0.9").unwrap().check_major().is_ok());
71 }
72}