Skip to main content

hooch_http/
shared.rs

1//! Defines the supported HTTP versions and conversion logic.
2//!
3//! This module provides the `HttpVersion` enum to represent HTTP protocol versions
4//! and implements conversions from raw bytes and to string representations.
5
6/// Represents the supported HTTP protocol versions.
7#[derive(Debug, Copy, Clone, PartialEq, Eq)]
8pub enum HttpVersion {
9    /// HTTP version 1.1
10    OnePointOne,
11}
12
13impl From<&[u8]> for HttpVersion {
14    /// Converts a byte slice into an `HttpVersion` enum.
15    ///
16    /// # Panics
17    /// Panics if the provided byte slice does not correspond to a supported HTTP version.
18    fn from(value: &[u8]) -> Self {
19        match value {
20            b"HTTP/1.1" => HttpVersion::OnePointOne,
21            _ => panic!("Unsupported version"),
22        }
23    }
24}
25
26impl From<HttpVersion> for &'static str {
27    /// Converts an `HttpVersion` enum into its corresponding string representation.
28    fn from(value: HttpVersion) -> Self {
29        match value {
30            HttpVersion::OnePointOne => "HTTP/1.1",
31        }
32    }
33}
34
35/// Supported HTTP methods.
36#[derive(Debug, Copy, Clone, PartialEq, Eq)]
37pub enum HttpMethod {
38    GET,
39    HEAD,
40    OPTIONS,
41    POST,
42    PUT,
43    PATCH,
44    DELETE,
45}
46
47impl From<&[u8]> for HttpMethod {
48    /// Convert raw bytes (e.g., b"GET") to an `HttpMethod` enum variant.
49    fn from(value: &[u8]) -> Self {
50        match value {
51            b"GET" => HttpMethod::GET,
52            b"HEAD" => HttpMethod::HEAD,
53            b"OPTIONS" => HttpMethod::OPTIONS,
54            b"POST" => HttpMethod::POST,
55            b"PUT" => HttpMethod::PUT,
56            b"PATCH" => HttpMethod::PATCH,
57            b"DELETE" => HttpMethod::DELETE,
58            _ => panic!("Unknown HTTP method"),
59        }
60    }
61}