|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +//! OCSF `http_method` enum — the 9 OCSF-defined HTTP methods. |
| 5 | +
|
| 6 | +use serde::{Deserialize, Serialize}; |
| 7 | + |
| 8 | +/// HTTP method as defined in the OCSF v1.7.0 `http_request` object schema. |
| 9 | +/// |
| 10 | +/// The 9 standard methods are typed variants. Non-standard methods use |
| 11 | +/// `Other(String)`. |
| 12 | +#[derive(Debug, Clone, PartialEq, Eq, Hash)] |
| 13 | +pub enum HttpMethod { |
| 14 | + /// OPTIONS |
| 15 | + Options, |
| 16 | + /// GET |
| 17 | + Get, |
| 18 | + /// HEAD |
| 19 | + Head, |
| 20 | + /// POST |
| 21 | + Post, |
| 22 | + /// PUT |
| 23 | + Put, |
| 24 | + /// DELETE |
| 25 | + Delete, |
| 26 | + /// TRACE |
| 27 | + Trace, |
| 28 | + /// CONNECT |
| 29 | + Connect, |
| 30 | + /// PATCH |
| 31 | + Patch, |
| 32 | + /// Non-standard method. |
| 33 | + Other(String), |
| 34 | +} |
| 35 | + |
| 36 | +impl HttpMethod { |
| 37 | + /// Return the canonical uppercase string representation. |
| 38 | + #[must_use] |
| 39 | + pub fn as_str(&self) -> &str { |
| 40 | + match self { |
| 41 | + Self::Options => "OPTIONS", |
| 42 | + Self::Get => "GET", |
| 43 | + Self::Head => "HEAD", |
| 44 | + Self::Post => "POST", |
| 45 | + Self::Put => "PUT", |
| 46 | + Self::Delete => "DELETE", |
| 47 | + Self::Trace => "TRACE", |
| 48 | + Self::Connect => "CONNECT", |
| 49 | + Self::Patch => "PATCH", |
| 50 | + Self::Other(s) => s, |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +impl std::str::FromStr for HttpMethod { |
| 56 | + type Err = std::convert::Infallible; |
| 57 | + |
| 58 | + /// Parse a method string into a typed variant (case-insensitive). |
| 59 | + fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 60 | + Ok(match s.to_uppercase().as_str() { |
| 61 | + "OPTIONS" => Self::Options, |
| 62 | + "GET" => Self::Get, |
| 63 | + "HEAD" => Self::Head, |
| 64 | + "POST" => Self::Post, |
| 65 | + "PUT" => Self::Put, |
| 66 | + "DELETE" => Self::Delete, |
| 67 | + "TRACE" => Self::Trace, |
| 68 | + "CONNECT" => Self::Connect, |
| 69 | + "PATCH" => Self::Patch, |
| 70 | + _ => Self::Other(s.to_string()), |
| 71 | + }) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +impl Serialize for HttpMethod { |
| 76 | + fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { |
| 77 | + serializer.serialize_str(self.as_str()) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +impl<'de> Deserialize<'de> for HttpMethod { |
| 82 | + fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> { |
| 83 | + let s = String::deserialize(deserializer)?; |
| 84 | + Ok(s.parse().unwrap()) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +impl std::fmt::Display for HttpMethod { |
| 89 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 90 | + f.write_str(self.as_str()) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +#[cfg(test)] |
| 95 | +mod tests { |
| 96 | + use super::*; |
| 97 | + |
| 98 | + #[test] |
| 99 | + fn test_from_str_standard_methods() { |
| 100 | + assert_eq!("GET".parse::<HttpMethod>().unwrap(), HttpMethod::Get); |
| 101 | + assert_eq!("get".parse::<HttpMethod>().unwrap(), HttpMethod::Get); |
| 102 | + assert_eq!("Post".parse::<HttpMethod>().unwrap(), HttpMethod::Post); |
| 103 | + assert_eq!("DELETE".parse::<HttpMethod>().unwrap(), HttpMethod::Delete); |
| 104 | + assert_eq!( |
| 105 | + "CONNECT".parse::<HttpMethod>().unwrap(), |
| 106 | + HttpMethod::Connect |
| 107 | + ); |
| 108 | + assert_eq!("PATCH".parse::<HttpMethod>().unwrap(), HttpMethod::Patch); |
| 109 | + } |
| 110 | + |
| 111 | + #[test] |
| 112 | + fn test_from_str_non_standard() { |
| 113 | + let method: HttpMethod = "PROPFIND".parse().unwrap(); |
| 114 | + assert_eq!(method, HttpMethod::Other("PROPFIND".to_string())); |
| 115 | + assert_eq!(method.as_str(), "PROPFIND"); |
| 116 | + } |
| 117 | + |
| 118 | + #[test] |
| 119 | + fn test_json_roundtrip() { |
| 120 | + let method = HttpMethod::Get; |
| 121 | + let json = serde_json::to_value(&method).unwrap(); |
| 122 | + assert_eq!(json, serde_json::json!("GET")); |
| 123 | + |
| 124 | + let deserialized: HttpMethod = serde_json::from_value(json).unwrap(); |
| 125 | + assert_eq!(deserialized, HttpMethod::Get); |
| 126 | + } |
| 127 | + |
| 128 | + #[test] |
| 129 | + fn test_json_roundtrip_other() { |
| 130 | + let method = HttpMethod::Other("PROPFIND".to_string()); |
| 131 | + let json = serde_json::to_value(&method).unwrap(); |
| 132 | + assert_eq!(json, serde_json::json!("PROPFIND")); |
| 133 | + |
| 134 | + let deserialized: HttpMethod = serde_json::from_value(json).unwrap(); |
| 135 | + assert_eq!(deserialized, HttpMethod::Other("PROPFIND".to_string())); |
| 136 | + } |
| 137 | +} |
0 commit comments