webdav 0.1.0

Types for WebDAV
Documentation
#[derive(Debug, Clone, PartialEq, Hash)]
pub enum Header {
    Dav,
    /// https://datatracker.ietf.org/doc/html/rfc4918#section-10.2
    Depth,
    /// https://datatracker.ietf.org/doc/html/rfc4918#section-10.3
    Destination,
    /// https://datatracker.ietf.org/doc/html/rfc4918#section-10.4
    If,
    /// https://datatracker.ietf.org/doc/html/rfc4918#section-10.5
    LockToken,
    /// https://datatracker.ietf.org/doc/html/rfc4918#section-10.6
    Overwrite,
    /// https://datatracker.ietf.org/doc/html/rfc4918#section-10.7
    Timeout,
    Http(http::HeaderName),
}

impl std::fmt::Display for Header {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let s = match self {
            Self::Dav => "DAV",
            Self::Depth => "DEPTH",
            Self::Destination => "DESTINATION",
            Self::If => "IF",
            Self::LockToken => "LOCK-TOKEN",
            Self::Overwrite => "OVERWRITE",
            Self::Timeout => "TIMEOUT",
            Self::Http(header) => {
                return write!(f, "{header}");
            }
        };

        f.write_str(s)
    }
}

impl std::str::FromStr for Header {
    type Err = crate::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let method = match s.to_ascii_uppercase().as_str() {
            "DAV" => Self::Dav,
            "DEPTH" => Self::Depth,
            "DESTINATION" => Self::Destination,
            "IF" => Self::If,
            "LOCK-TOKEN" => Self::LockToken,
            "TIMEOUT" => Self::Timeout,
            _ => s
                .parse()
                .map(Self::Http)
                .map_err(|_| crate::Error::InvalidHeader(s.to_string()))?,
        };

        Ok(method)
    }
}

impl From<http::HeaderName> for Header {
    fn from(value: http::HeaderName) -> Self {
        Self::Http(value)
    }
}

impl TryFrom<Header> for http::HeaderName {
    type Error = http::header::InvalidHeaderName;

    fn try_from(value: Header) -> Result<Self, Self::Error> {
        let name = match value {
            Header::Http(header_name) => return Ok(header_name),
            _ => value.to_string(),
        };

        name.parse()
    }
}