Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/interplane-mv/blob/main/rust/crates/interplane-core/src/macros.rs # interplane-mv INTERPLANE: open interoperability protocol between AI models and agent runtimes. Models express intent. Runtimes retain authority. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `interplane-mv / rust / crates / interplane-core / src / macros.rs` 56 lines ยท 2406 bytes Raw Blame History 1 /// A string enum that keeps unknown values as `Unknown(String)`. 2 macro_rules! string_enum { 3 ($(#[$m:meta])* $name:ident { $($var:ident => $s:literal),+ $(,)? }) => { 4 $(#[$m])* 5 #[derive(Debug, Clone, PartialEq, Eq, Hash)] 6 pub enum $name { 7 $($var,)+ 8 /// A value this implementation does not know. 9 Unknown(String), 10 } 11 impl $name { 12 /// The wire string. 13 pub fn as_str(&self) -> &str { 14 match self { $(Self::$var => $s,)+ Self::Unknown(s) => s } 15 } 16 /// Parse a wire string; unknown strings become `Unknown`. 17 pub fn parse(s: &str) -> Self { 18 match s { $($s => Self::$var,)+ other => Self::Unknown(other.to_string()) } 19 } 20 /// True unless this is `Unknown`. 21 pub fn is_known(&self) -> bool { !matches!(self, Self::Unknown(_)) } 22 } 23 impl std::fmt::Display for $name { 24 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str(self.as_str()) } 25 } 26 impl serde::Serialize for $name { 27 fn serialize(&self, s: S) -> Result { s.serialize_str(self.as_str()) } 28 } 29 impl<'de> serde::Deserialize<'de> for $name { 30 fn deserialize>(d: D) -> Result { 31 let s = ::deserialize(d)?; 32 Ok(Self::parse(&s)) 33 } 34 } 35 }; 36 } 37 38 /// A unit type that serializes to a constant `kind` string and refuses any other. 39 macro_rules! const_kind { 40 ($(#[$m:meta])* $name:ident, $lit:literal) => { 41 $(#[$m])* 42 #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] 43 pub struct $name; 44 impl serde::Serialize for $name { 45 fn serialize(&self, s: S) -> Result { s.serialize_str($lit) } 46 } 47 impl<'de> serde::Deserialize<'de> for $name { 48 fn deserialize>(d: D) -> Result { 49 let s = ::deserialize(d)?; 50 if s == $lit { Ok($name) } else { 51 Err(serde::de::Error::custom(format!("expected kind {:?}, got {:?}", $lit, s))) 52 } 53 } 54 } 55 }; 56 }