use crate::*;
impl Default for Protocol {
fn default() -> Self {
Self::Unknown(String::new())
}
}
impl Protocol {
pub fn new() -> Self {
Self::default()
}
pub fn is_http(&self) -> bool {
self.to_owned() == Self::HTTP.to_owned()
}
pub fn is_https(&self) -> bool {
self.to_owned() == Self::HTTPS.to_owned()
}
pub fn get_port(&self) -> u16 {
match self {
Self::HTTP => 80,
Self::HTTPS => 443,
Self::Unknown(_) => 80,
}
}
}
impl Display for Protocol {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let res: &str = match self {
Self::HTTP => HTTP_LOWERCASE,
Self::HTTPS => HTTPS_LOWERCASE,
Self::Unknown(protocol) => protocol,
};
write!(f, "{}", res)
}
}
impl FromStr for Protocol {
type Err = &'static str;
fn from_str(data: &str) -> Result<Self, Self::Err> {
match data.to_ascii_lowercase().as_str() {
HTTP_LOWERCASE => Ok(Self::HTTP),
HTTPS_LOWERCASE => Ok(Self::HTTPS),
_ => Ok(Self::Unknown(data.to_string())),
}
}
}