use std::{
borrow::Cow,
fmt::{self, Display, Formatter},
};
use compose_spec_macros::{DeserializeTryFromString, SerializeDisplay};
use crate::impl_from_str;
#[derive(SerializeDisplay, DeserializeTryFromString, Debug, Clone, PartialEq, Eq)]
pub enum EndpointMode {
VIp,
DnsRR,
Other(String),
}
impl EndpointMode {
const VIP: &'static str = "vip";
const DNS_RR: &'static str = "dnsrr";
pub fn parse<T>(endpoint_mode: T) -> Self
where
T: AsRef<str> + Into<String>,
{
match endpoint_mode.as_ref() {
Self::VIP => Self::VIp,
Self::DNS_RR => Self::DnsRR,
_ => Self::Other(endpoint_mode.into()),
}
}
#[must_use]
pub const fn is_vip(&self) -> bool {
matches!(self, Self::VIp)
}
#[must_use]
pub const fn is_dnsrr(&self) -> bool {
matches!(self, Self::DnsRR)
}
#[must_use]
pub fn as_str(&self) -> &str {
match self {
Self::VIp => Self::VIP,
Self::DnsRR => Self::DNS_RR,
Self::Other(other) => other,
}
}
}
impl_from_str!(EndpointMode);
impl AsRef<str> for EndpointMode {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl Display for EndpointMode {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl From<EndpointMode> for String {
fn from(value: EndpointMode) -> Self {
match value {
EndpointMode::VIp | EndpointMode::DnsRR => value.as_str().to_owned(),
EndpointMode::Other(string) => string,
}
}
}
impl From<EndpointMode> for Cow<'static, str> {
fn from(value: EndpointMode) -> Self {
match value {
EndpointMode::VIp => Self::Borrowed(EndpointMode::VIP),
EndpointMode::DnsRR => Self::Borrowed(EndpointMode::DNS_RR),
EndpointMode::Other(other) => Self::Owned(other),
}
}
}