kodumaro-http-cli 0.5.5

Kodumaro HTTP CLI inspired by HTTPie
Documentation
use clap::ValueEnum;
use reqwest::Method;

#[derive(Clone, Copy, Debug, PartialEq, ValueEnum)]
pub enum Verb {
    #[value(aliases = ["CONNECT", "Connect"])]
    CONNECT,
    #[value(aliases = ["DELETE", "Delete"])]
    DELETE,
    #[value(aliases = ["GET", "Get"])]
    GET,
    #[value(aliases = ["HEAD", "Head"])]
    HEAD,
    #[value(aliases = ["OPTIONS", "Options"])]
    OPTIONS,
    #[value(aliases = ["PATCH", "Patch"])]
    PATCH,
    #[value(aliases = ["POST", "Post"])]
    POST,
    #[value(aliases = ["PUT", "Put"])]
    PUT,
    #[value(aliases = ["TRACE", "Trace"])]
    TRACE,
}

impl From<Verb> for Method {

    fn from(verb: Verb) -> Self {
        match verb {
            Verb::CONNECT => Self::CONNECT,
            Verb::DELETE => Self::DELETE,
            Verb::GET => Self::GET,
            Verb::HEAD => Self::HEAD,
            Verb::OPTIONS => Self::OPTIONS,
            Verb::PATCH => Self::PATCH,
            Verb::POST => Self::POST,
            Verb::PUT => Self::PUT,
            Verb::TRACE => Self::TRACE,
        }
    }
}

#[allow(non_snake_case)]
#[cfg(test)]
mod tests {

    use clap::Parser;
    use super::*;

    #[derive(Parser)]
    struct TestParam {
        #[arg(value_enum)]
        verb: Verb,
    }

    // CONNECT

    #[test]
    fn test_CONNECT() -> Result<(), Box<dyn std::error::Error>> {
        let params = TestParam::try_parse_from(["test", "CONNECT"])?;
        assert_eq!(Verb::CONNECT, params.verb);
        Ok(())
    }

    #[test]
    fn test_Connect() -> Result<(), Box<dyn std::error::Error>> {
        let params = TestParam::try_parse_from(["test", "Connect"])?;
        assert_eq!(Verb::CONNECT, params.verb);
        Ok(())
    }

    #[test]
    fn test_connect() -> Result<(), Box<dyn std::error::Error>> {
        let params = TestParam::try_parse_from(["test", "connect"])?;
        assert_eq!(Verb::CONNECT, params.verb);
        Ok(())
    }

    #[test]
    fn test_connect_to_reqwest() {
        let method: Method = Verb::CONNECT.into();
        assert_eq!(Method::CONNECT, method);
    }

    // DELETE

    #[test]
    fn test_DELETE() -> Result<(), Box<dyn std::error::Error>> {
        let params = TestParam::try_parse_from(["test", "DELETE"])?;
        assert_eq!(Verb::DELETE, params.verb);
        Ok(())
    }

    #[test]
    fn test_Delete() -> Result<(), Box<dyn std::error::Error>> {
        let params = TestParam::try_parse_from(["test", "Delete"])?;
        assert_eq!(Verb::DELETE, params.verb);
        Ok(())
    }

    #[test]
    fn test_delete() -> Result<(), Box<dyn std::error::Error>> {
        let params = TestParam::try_parse_from(["test", "delete"])?;
        assert_eq!(Verb::DELETE, params.verb);
        Ok(())
    }

    #[test]
    fn test_delete_to_reqwest() {
        let method: Method = Verb::DELETE.into();
        assert_eq!(Method::DELETE, method);
    }

    // GET

    #[test]
    fn test_GET() -> Result<(), Box<dyn std::error::Error>> {
        let params = TestParam::try_parse_from(["test", "GET"])?;
        assert_eq!(Verb::GET, params.verb);
        Ok(())
    }

    #[test]
    fn test_Get() -> Result<(), Box<dyn std::error::Error>> {
        let params = TestParam::try_parse_from(["test", "Get"])?;
        assert_eq!(Verb::GET, params.verb);
        Ok(())
    }

    #[test]
    fn test_get() -> Result<(), Box<dyn std::error::Error>> {
        let params = TestParam::try_parse_from(["test", "get"])?;
        assert_eq!(Verb::GET, params.verb);
        Ok(())
    }

    #[test]
    fn test_get_to_reqwest() {
        let method: Method = Verb::GET.into();
        assert_eq!(Method::GET, method);
    }

    // HEAD

    #[test]
    fn test_HEAD() -> Result<(), Box<dyn std::error::Error>> {
        let params = TestParam::try_parse_from(["test", "HEAD"])?;
        assert_eq!(Verb::HEAD, params.verb);
        Ok(())
    }

    #[test]
    fn test_Head() -> Result<(), Box<dyn std::error::Error>> {
        let params = TestParam::try_parse_from(["test", "Head"])?;
        assert_eq!(Verb::HEAD, params.verb);
        Ok(())
    }

    #[test]
    fn test_head() -> Result<(), Box<dyn std::error::Error>> {
        let params = TestParam::try_parse_from(["test", "head"])?;
        assert_eq!(Verb::HEAD, params.verb);
        Ok(())
    }

    #[test]
    fn test_head_to_reqwest() {
        let method: Method = Verb::HEAD.into();
        assert_eq!(Method::HEAD, method);
    }

    // OPTIONS

    #[test]
    fn test_OPTIONS() -> Result<(), Box<dyn std::error::Error>> {
        let params = TestParam::try_parse_from(["test", "OPTIONS"])?;
        assert_eq!(Verb::OPTIONS, params.verb);
        Ok(())
    }

    #[test]
    fn test_Options() -> Result<(), Box<dyn std::error::Error>> {
        let params = TestParam::try_parse_from(["test", "Options"])?;
        assert_eq!(Verb::OPTIONS, params.verb);
        Ok(())
    }

    #[test]
    fn test_options() -> Result<(), Box<dyn std::error::Error>> {
        let params = TestParam::try_parse_from(["test", "options"])?;
        assert_eq!(Verb::OPTIONS, params.verb);
        Ok(())
    }

    #[test]
    fn test_options_to_reqwest() {
        let method: Method = Verb::OPTIONS.into();
        assert_eq!(Method::OPTIONS, method);
    }

    // PATCH

    #[test]
    fn test_PATCH() -> Result<(), Box<dyn std::error::Error>> {
        let params = TestParam::try_parse_from(["test", "PATCH"])?;
        assert_eq!(Verb::PATCH, params.verb);
        Ok(())
    }

    #[test]
    fn test_Patch() -> Result<(), Box<dyn std::error::Error>> {
        let params = TestParam::try_parse_from(["test", "Patch"])?;
        assert_eq!(Verb::PATCH, params.verb);
        Ok(())
    }

    #[test]
    fn test_patch() -> Result<(), Box<dyn std::error::Error>> {
        let params = TestParam::try_parse_from(["test", "patch"])?;
        assert_eq!(Verb::PATCH, params.verb);
        Ok(())
    }

    #[test]
    fn test_patch_to_reqwest() {
        let method: Method = Verb::PATCH.into();
        assert_eq!(Method::PATCH, method);
    }

    // POST

    #[test]
    fn test_POST() -> Result<(), Box<dyn std::error::Error>> {
        let params = TestParam::try_parse_from(["test", "POST"])?;
        assert_eq!(Verb::POST, params.verb);
        Ok(())
    }

    #[test]
    fn test_Post() -> Result<(), Box<dyn std::error::Error>> {
        let params = TestParam::try_parse_from(["test", "Post"])?;
        assert_eq!(Verb::POST, params.verb);
        Ok(())
    }

    #[test]
    fn test_post() -> Result<(), Box<dyn std::error::Error>> {
        let params = TestParam::try_parse_from(["test", "post"])?;
        assert_eq!(Verb::POST, params.verb);
        Ok(())
    }

    #[test]
    fn test_post_to_reqwest() {
        let method: Method = Verb::POST.into();
        assert_eq!(Method::POST, method);
    }

    // PUT

    #[test]
    fn test_PUT() -> Result<(), Box<dyn std::error::Error>> {
        let params = TestParam::try_parse_from(["test", "PUT"])?;
        assert_eq!(Verb::PUT, params.verb);
        Ok(())
    }

    #[test]
    fn test_Put() -> Result<(), Box<dyn std::error::Error>> {
        let params = TestParam::try_parse_from(["test", "Put"])?;
        assert_eq!(Verb::PUT, params.verb);
        Ok(())
    }

    #[test]
    fn test_put() -> Result<(), Box<dyn std::error::Error>> {
        let params = TestParam::try_parse_from(["test", "put"])?;
        assert_eq!(Verb::PUT, params.verb);
        Ok(())
    }

    #[test]
    fn test_put_to_reqwest() {
        let method: Method = Verb::PUT.into();
        assert_eq!(Method::PUT, method);
    }

    // TRACE

    #[test]
    fn test_TRACE() -> Result<(), Box<dyn std::error::Error>> {
        let params = TestParam::try_parse_from(["test", "TRACE"])?;
        assert_eq!(Verb::TRACE, params.verb);
        Ok(())
    }

    #[test]
    fn test_Trace() -> Result<(), Box<dyn std::error::Error>> {
        let params = TestParam::try_parse_from(["test", "Trace"])?;
        assert_eq!(Verb::TRACE, params.verb);
        Ok(())
    }

    #[test]
    fn test_trace() -> Result<(), Box<dyn std::error::Error>> {
        let params = TestParam::try_parse_from(["test", "trace"])?;
        assert_eq!(Verb::TRACE, params.verb);
        Ok(())
    }

    #[test]
    fn test_trace_to_reqwest() {
        let method: Method = Verb::TRACE.into();
        assert_eq!(Method::TRACE, method);
    }
}