|
| 1 | +use std::fmt; |
| 2 | +use std::str::FromStr; |
| 3 | + |
| 4 | +use serde::{Deserialize, Serialize}; |
| 5 | + |
| 6 | +/// A2A wire methods. The serde wire form is the canonical slash-delimited |
| 7 | +/// string (e.g. `message/send`), matching `as_str` / `Display` / `FromStr` |
| 8 | +/// so the same value round-trips across JSON and string parsing without |
| 9 | +/// silently switching representation. |
| 10 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] |
| 11 | +pub enum A2aMethod { |
| 12 | + #[serde(rename = "message/send")] |
| 13 | + MessageSend, |
| 14 | + #[serde(rename = "message/stream")] |
| 15 | + MessageStream, |
| 16 | + #[serde(rename = "tasks/get")] |
| 17 | + TasksGet, |
| 18 | + #[serde(rename = "tasks/list")] |
| 19 | + TasksList, |
| 20 | + #[serde(rename = "tasks/cancel")] |
| 21 | + TasksCancel, |
| 22 | + #[serde(rename = "tasks/resubscribe")] |
| 23 | + TasksResubscribe, |
| 24 | + #[serde(rename = "tasks/pushNotificationConfig/set")] |
| 25 | + PushNotificationSet, |
| 26 | + #[serde(rename = "tasks/pushNotificationConfig/get")] |
| 27 | + PushNotificationGet, |
| 28 | + #[serde(rename = "tasks/pushNotificationConfig/list")] |
| 29 | + PushNotificationList, |
| 30 | + #[serde(rename = "tasks/pushNotificationConfig/delete")] |
| 31 | + PushNotificationDelete, |
| 32 | + #[serde(rename = "agent/getAuthenticatedExtendedCard")] |
| 33 | + AgentCard, |
| 34 | +} |
| 35 | + |
| 36 | +impl A2aMethod { |
| 37 | + pub fn as_str(&self) -> &'static str { |
| 38 | + match self { |
| 39 | + Self::MessageSend => "message/send", |
| 40 | + Self::MessageStream => "message/stream", |
| 41 | + Self::TasksGet => "tasks/get", |
| 42 | + Self::TasksList => "tasks/list", |
| 43 | + Self::TasksCancel => "tasks/cancel", |
| 44 | + Self::TasksResubscribe => "tasks/resubscribe", |
| 45 | + Self::PushNotificationSet => "tasks/pushNotificationConfig/set", |
| 46 | + Self::PushNotificationGet => "tasks/pushNotificationConfig/get", |
| 47 | + Self::PushNotificationList => "tasks/pushNotificationConfig/list", |
| 48 | + Self::PushNotificationDelete => "tasks/pushNotificationConfig/delete", |
| 49 | + Self::AgentCard => "agent/getAuthenticatedExtendedCard", |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + pub fn all() -> &'static [Self] { |
| 54 | + &[ |
| 55 | + Self::MessageSend, |
| 56 | + Self::MessageStream, |
| 57 | + Self::TasksGet, |
| 58 | + Self::TasksList, |
| 59 | + Self::TasksCancel, |
| 60 | + Self::TasksResubscribe, |
| 61 | + Self::PushNotificationSet, |
| 62 | + Self::PushNotificationGet, |
| 63 | + Self::PushNotificationList, |
| 64 | + Self::PushNotificationDelete, |
| 65 | + Self::AgentCard, |
| 66 | + ] |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +impl fmt::Display for A2aMethod { |
| 71 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 72 | + f.write_str(self.as_str()) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] |
| 77 | +#[error("unknown A2A method: {value}")] |
| 78 | +pub struct ParseA2aMethodError { |
| 79 | + pub value: String, |
| 80 | +} |
| 81 | + |
| 82 | +impl FromStr for A2aMethod { |
| 83 | + type Err = ParseA2aMethodError; |
| 84 | + |
| 85 | + fn from_str(value: &str) -> Result<Self, Self::Err> { |
| 86 | + Self::all() |
| 87 | + .iter() |
| 88 | + .copied() |
| 89 | + .find(|method| method.as_str() == value) |
| 90 | + .ok_or_else(|| ParseA2aMethodError { |
| 91 | + value: value.to_owned(), |
| 92 | + }) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +#[cfg(test)] |
| 97 | +mod tests { |
| 98 | + use super::*; |
| 99 | + |
| 100 | + #[test] |
| 101 | + fn roundtrip_display_parse() { |
| 102 | + for method in A2aMethod::all() { |
| 103 | + let parsed = method.as_str().parse::<A2aMethod>().unwrap(); |
| 104 | + assert_eq!(parsed, *method); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + #[test] |
| 109 | + fn serde_uses_canonical_wire_strings() { |
| 110 | + let method = A2aMethod::MessageSend; |
| 111 | + let wire = serde_json::to_string(&method).expect("serialize"); |
| 112 | + assert_eq!(wire, "\"message/send\""); |
| 113 | + let back: A2aMethod = serde_json::from_str(&wire).expect("deserialize"); |
| 114 | + assert_eq!(back, method); |
| 115 | + } |
| 116 | + |
| 117 | + #[test] |
| 118 | + fn parse_error_includes_unknown_value() { |
| 119 | + let err = "not/real".parse::<A2aMethod>().unwrap_err(); |
| 120 | + assert_eq!(err.value, "not/real"); |
| 121 | + assert!(err.to_string().contains("not/real")); |
| 122 | + } |
| 123 | +} |
0 commit comments