francoisgib_webserver 1.0.3

HTTP Webserver
Documentation
use strum_macros::{Display, EnumString};

/// Represents standard HTTP methods.
///
/// This enum provides a type-safe way to represent HTTP methods, with support
/// for serialization and deserialization through the strum_macros crate.
///
/// # Features
/// - Implements Display for converting to strings (outputs uppercase method names).
/// - Implements EnumString for parsing from strings (accepts uppercase method names).
/// - Provides PartialEq for equality comparison between methods.
/// - Includes debug formatting with Debug.
///
/// # Variants
/// - GET: Retrieve a resource.
/// - POST: Submit data to a resource.
/// - DELETE: Remove a resource.
/// - PUT: Replace a resource.
/// - PATCH: Partially update a resource.
/// - HEAD: todo.
/// - OPTIONS: todo.
///
/// # Example
/// ```
/// use std::str::FromStr;
/// use francoisgib_webserver::http::methods::HttpMethod;
///
/// let method = HttpMethod::GET;
///
/// assert_eq!(method.to_string(), "GET");
///
/// let parsed = HttpMethod::from_str("POST").unwrap();
/// assert_eq!(parsed, HttpMethod::POST);
/// ```
///
/// # Notes
/// - String parsing is case-sensitive and requires uppercase input due to the
///   #[strum(serialize_all = "UPPERCASE")] attribute.
/// - Uses the strum_macros crate for string serialization and deserialization.
#[derive(Debug, Display, EnumString, PartialEq, Clone, Copy)]
#[strum(serialize_all = "UPPERCASE")]
pub enum HttpMethod {
    GET,
    POST,
    DELETE,
    PUT,
    PATCH,
    HEAD,
    // OPTIONS,
}

#[cfg(test)]
mod tests {
    use crate::http::methods::HttpMethod;
    use std::str::FromStr;
    use strum::ParseError;

    #[test]
    fn test_str_to_http_method() {
        assert_eq!(Ok(HttpMethod::GET), HttpMethod::from_str("GET"));
        assert_eq!(Ok(HttpMethod::POST), HttpMethod::from_str("POST"));
        assert_eq!(Ok(HttpMethod::DELETE), HttpMethod::from_str("DELETE"));
        assert_eq!(Ok(HttpMethod::PUT), HttpMethod::from_str("PUT"));
        assert_eq!(Ok(HttpMethod::PATCH), HttpMethod::from_str("PATCH"));
    }

    #[test]
    fn test_str_to_http_method_when_lowercase() {
        assert_eq!(
            Err(ParseError::VariantNotFound),
            HttpMethod::from_str("get")
        );
        assert_eq!(
            Err(ParseError::VariantNotFound),
            HttpMethod::from_str("post")
        );
        assert_eq!(
            Err(ParseError::VariantNotFound),
            HttpMethod::from_str("delete")
        );
        assert_eq!(
            Err(ParseError::VariantNotFound),
            HttpMethod::from_str("put")
        );
        assert_eq!(
            Err(ParseError::VariantNotFound),
            HttpMethod::from_str("patch")
        );
    }

    #[test]
    fn test_str_to_http_method_when_unknown_method() {
        assert_eq!(
            Err(ParseError::VariantNotFound),
            HttpMethod::from_str("unknown")
        );

        assert_eq!(
            Err(ParseError::VariantNotFound),
            HttpMethod::from_str("UNKNOWN")
        );
    }
}