use serde::{Deserialize, Serialize};
use thiserror::Error;
use crate::header_string::StaticHeaderString;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[derive(Deserialize, Serialize)]
#[serde(try_from = "&str", into = "&'static str")]
pub enum Protocol {
Http,
Https
}
impl From<Protocol> for &'static str {
fn from(value: Protocol) -> Self {
match value {
Protocol::Http => "http",
Protocol::Https => "https"
}
}
}
#[derive(Clone, Debug, Error)]
#[error("Protocol must be either http or https")]
pub struct InvalidProtocol;
impl TryFrom<&str> for Protocol {
type Error = InvalidProtocol;
fn try_from(value: &str) -> Result<Self, InvalidProtocol> {
value.parse()
}
}
impl std::str::FromStr for Protocol {
type Err = InvalidProtocol;
fn from_str(value: &str) -> Result<Self, InvalidProtocol> {
if value == "http" {
Ok(Protocol::Http)
} else if value == "https" {
Ok(Protocol::Https)
} else {
Err(InvalidProtocol)
}
}
}
const HTTP_SCHEME: StaticHeaderString = StaticHeaderString::from_static("http://");
const HTTPS_SCHEME: StaticHeaderString = StaticHeaderString::from_static("https://");
impl Protocol {
pub fn is_secure(&self) -> bool {
self == &Protocol::Https
}
pub fn url_prefix(&self) -> StaticHeaderString {
use Protocol::*;
match self {
&Http => HTTP_SCHEME,
&Https => HTTPS_SCHEME
}
}
pub fn default_port(&self) -> u16 {
use Protocol::*;
match self {
&Http => 80,
&Https => 443
}
}
}