Skip to main content

systemprompt_models/a2a/
transport.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize, PartialEq, Eq)]
4pub enum TransportProtocol {
5    #[serde(rename = "JSONRPC")]
6    #[default]
7    JsonRpc,
8    #[serde(rename = "GRPC")]
9    Grpc,
10    #[serde(rename = "HTTP+JSON")]
11    HttpJson,
12}
13
14impl From<TransportProtocol> for String {
15    fn from(transport: TransportProtocol) -> Self {
16        match transport {
17            TransportProtocol::JsonRpc => "JSONRPC".to_string(),
18            TransportProtocol::Grpc => "GRPC".to_string(),
19            TransportProtocol::HttpJson => "HTTP+JSON".to_string(),
20        }
21    }
22}
23
24impl std::str::FromStr for TransportProtocol {
25    type Err = anyhow::Error;
26
27    fn from_str(s: &str) -> Result<Self, Self::Err> {
28        match s {
29            "JSONRPC" => Ok(Self::JsonRpc),
30            "GRPC" => Ok(Self::Grpc),
31            "HTTP+JSON" => Ok(Self::HttpJson),
32            _ => Err(anyhow::anyhow!("Invalid transport protocol: {}", s)),
33        }
34    }
35}
36
37impl std::fmt::Display for TransportProtocol {
38    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39        match self {
40            Self::JsonRpc => write!(f, "JSONRPC"),
41            Self::Grpc => write!(f, "GRPC"),
42            Self::HttpJson => write!(f, "HTTP+JSON"),
43        }
44    }
45}
46
47impl TransportProtocol {
48    pub const fn as_str(&self) -> &'static str {
49        match self {
50            Self::JsonRpc => "JSONRPC",
51            Self::Grpc => "GRPC",
52            Self::HttpJson => "HTTP+JSON",
53        }
54    }
55}