use std::fmt::Display;
use serde::Deserialize;
use serde::Serialize;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Hash, Default)]
#[serde(rename_all = "lowercase")]
pub enum TransportProtocol {
#[default]
Tcp,
Udp,
}
impl std::str::FromStr for TransportProtocol {
type Err = color_eyre::eyre::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"tcp" => Ok(Self::Tcp),
"udp" => Ok(Self::Udp),
_ => Err(color_eyre::eyre::eyre!("Invalid transport protocol {s}")),
}
}
}
impl TransportProtocol {
pub fn to_lowercase(&self) -> &'static str {
match self {
Self::Tcp => "tcp",
Self::Udp => "udp",
}
}
}
impl Display for TransportProtocol {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.to_lowercase())
}
}