webdav 0.1.0

Types for WebDAV
Documentation
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub struct Method(Inner);

impl Method {
    pub const PROPFIND: Method = Method(Inner::Propfind);
    pub const PROPPATCH: Method = Method(Inner::Proppatch);
    pub const MKCOL: Method = Method(Inner::Mkcol);
    pub const COPY: Method = Method(Inner::Copy);
    pub const MOVE: Method = Method(Inner::Move);
    pub const LOCK: Method = Method(Inner::Lock);
    pub const UNLOCK: Method = Method(Inner::Unlock);

    pub const GET: Method = Method(Inner::Http(http::Method::GET));
    pub const POST: Method = Method(Inner::Http(http::Method::POST));
    pub const PUT: Method = Method(Inner::Http(http::Method::PUT));
    pub const DELETE: Method = Method(Inner::Http(http::Method::DELETE));
    pub const HEAD: Method = Method(Inner::Http(http::Method::HEAD));
    pub const OPTIONS: Method = Method(Inner::Http(http::Method::OPTIONS));
    pub const CONNECT: Method = Method(Inner::Http(http::Method::CONNECT));
    pub const PATCH: Method = Method(Inner::Http(http::Method::PATCH));
    pub const TRACE: Method = Method(Inner::Http(http::Method::TRACE));
}

#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub enum Inner {
    /// https://datatracker.ietf.org/doc/html/rfc4918#section-9.1
    Propfind,
    /// https://datatracker.ietf.org/doc/html/rfc4918#section-9.2
    Proppatch,
    /// https://datatracker.ietf.org/doc/html/rfc4918#section-9.3
    Mkcol,
    /// https://datatracker.ietf.org/doc/html/rfc4918#section-9.8
    Copy,
    /// https://datatracker.ietf.org/doc/html/rfc4918#section-9.9
    Move,
    /// https://datatracker.ietf.org/doc/html/rfc4918#section-9.10
    Lock,
    /// https://datatracker.ietf.org/doc/html/rfc4918#section-9.11
    Unlock,
    Http(http::Method),
}

impl std::fmt::Display for Method {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let s = match &self.0 {
            Inner::Propfind => "PROPFIND",
            Inner::Proppatch => "PROPPATCH",
            Inner::Mkcol => "MKCOL",
            Inner::Copy => "COPY",
            Inner::Move => "MOVE",
            Inner::Lock => "LOCK",
            Inner::Unlock => "UNLOCK",
            Inner::Http(method) => {
                return write!(f, "{method}");
            }
        };

        f.write_str(s)
    }
}

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

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let method = match s.to_ascii_uppercase().as_str() {
            "PROPFIND" => Self(Inner::Propfind),
            "PROPPATCH" => Self(Inner::Proppatch),
            "MKCOL" => Self(Inner::Mkcol),
            "COPY" => Self(Inner::Copy),
            "MOVE" => Self(Inner::Move),
            "LOCK" => Self(Inner::Lock),
            "UNLOCK" => Self(Inner::Unlock),
            _ => s
                .parse()
                .map(|x| Self(Inner::Http(x)))
                .map_err(|_| crate::Error::InvalidMethod(s.to_string()))?,
        };

        Ok(method)
    }
}

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

impl std::ops::Deref for Method {
    type Target = http::Method;

    fn deref(&self) -> &Self::Target {
        match self.0 {
            Inner::Propfind => todo!(),
            Inner::Proppatch => todo!(),
            Inner::Mkcol => todo!(),
            Inner::Copy => todo!(),
            Inner::Move => todo!(),
            Inner::Lock => todo!(),
            Inner::Unlock => todo!(),
            Inner::Http(ref method) => method,
        }
    }
}