@@ -6,7 +6,7 @@ use std::collections::{BTreeMap, HashMap};
66use crate :: SchemaRef ;
77
88/// HTTP method
9- #[ derive( Debug , Clone , PartialEq , Eq , Hash , Serialize , Deserialize ) ]
9+ #[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash , Serialize , Deserialize ) ]
1010#[ serde( rename_all = "UPPERCASE" ) ]
1111pub enum HttpMethod {
1212 Get ,
@@ -19,18 +19,35 @@ pub enum HttpMethod {
1919 Trace ,
2020}
2121
22- impl From < & str > for HttpMethod {
23- fn from ( value : & str ) -> Self {
22+ impl std:: fmt:: Display for HttpMethod {
23+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
24+ match self {
25+ Self :: Get => write ! ( f, "GET" ) ,
26+ Self :: Post => write ! ( f, "POST" ) ,
27+ Self :: Put => write ! ( f, "PUT" ) ,
28+ Self :: Patch => write ! ( f, "PATCH" ) ,
29+ Self :: Delete => write ! ( f, "DELETE" ) ,
30+ Self :: Head => write ! ( f, "HEAD" ) ,
31+ Self :: Options => write ! ( f, "OPTIONS" ) ,
32+ Self :: Trace => write ! ( f, "TRACE" ) ,
33+ }
34+ }
35+ }
36+
37+ impl TryFrom < & str > for HttpMethod {
38+ type Error = String ;
39+
40+ fn try_from ( value : & str ) -> Result < Self , Self :: Error > {
2441 match value. to_uppercase ( ) . as_str ( ) {
25- "GET" => HttpMethod :: Get ,
26- "POST" => HttpMethod :: Post ,
27- "PUT" => HttpMethod :: Put ,
28- "PATCH" => HttpMethod :: Patch ,
29- "DELETE" => HttpMethod :: Delete ,
30- "HEAD" => HttpMethod :: Head ,
31- "OPTIONS" => HttpMethod :: Options ,
32- "TRACE" => HttpMethod :: Trace ,
33- _ => HttpMethod :: Get , // default value
42+ "GET" => Ok ( Self :: Get ) ,
43+ "POST" => Ok ( Self :: Post ) ,
44+ "PUT" => Ok ( Self :: Put ) ,
45+ "PATCH" => Ok ( Self :: Patch ) ,
46+ "DELETE" => Ok ( Self :: Delete ) ,
47+ "HEAD" => Ok ( Self :: Head ) ,
48+ "OPTIONS" => Ok ( Self :: Options ) ,
49+ "TRACE" => Ok ( Self :: Trace ) ,
50+ other => Err ( format ! ( "unknown HTTP method: {other}" ) ) ,
3451 }
3552 }
3653}
@@ -137,7 +154,7 @@ pub struct Header {
137154 pub schema : Option < SchemaRef > ,
138155}
139156
140- /// OpenAPI Operation definition
157+ /// ` OpenAPI` Operation definition
141158#[ derive( Debug , Clone , Serialize , Deserialize ) ]
142159#[ serde( rename_all = "camelCase" ) ]
143160pub struct Operation {
@@ -221,7 +238,8 @@ impl PathItem {
221238 }
222239
223240 /// Get an operation for a specific HTTP method
224- pub fn get_operation ( & self , method : & HttpMethod ) -> Option < & Operation > {
241+ #[ must_use]
242+ pub const fn get_operation ( & self , method : & HttpMethod ) -> Option < & Operation > {
225243 match method {
226244 HttpMethod :: Get => self . get . as_ref ( ) ,
227245 HttpMethod :: Post => self . post . as_ref ( ) ,
@@ -277,15 +295,14 @@ mod tests {
277295 #[ case( "trace" , HttpMethod :: Trace ) ]
278296 #[ case( "Trace" , HttpMethod :: Trace ) ]
279297 fn test_http_method_from_str ( #[ case] input : & str , #[ case] expected : HttpMethod ) {
280- let result = HttpMethod :: from ( input) ;
298+ let result = HttpMethod :: try_from ( input) . unwrap ( ) ;
281299 assert_eq ! ( result, expected) ;
282300 }
283301
284302 #[ test]
285303 fn test_http_method_from_invalid_str ( ) {
286- // Invalid method should default to Get
287- let result = HttpMethod :: from ( "INVALID" ) ;
288- assert_eq ! ( result, HttpMethod :: Get ) ;
304+ let result = HttpMethod :: try_from ( "INVALID" ) ;
305+ assert ! ( result. is_err( ) ) ;
289306 }
290307
291308 #[ test]
@@ -523,6 +540,19 @@ mod tests {
523540 ) ;
524541 }
525542
543+ #[ rstest]
544+ #[ case( HttpMethod :: Get , "GET" ) ]
545+ #[ case( HttpMethod :: Post , "POST" ) ]
546+ #[ case( HttpMethod :: Put , "PUT" ) ]
547+ #[ case( HttpMethod :: Patch , "PATCH" ) ]
548+ #[ case( HttpMethod :: Delete , "DELETE" ) ]
549+ #[ case( HttpMethod :: Head , "HEAD" ) ]
550+ #[ case( HttpMethod :: Options , "OPTIONS" ) ]
551+ #[ case( HttpMethod :: Trace , "TRACE" ) ]
552+ fn test_http_method_display ( #[ case] method : HttpMethod , #[ case] expected : & str ) {
553+ assert_eq ! ( method. to_string( ) , expected) ;
554+ }
555+
526556 #[ test]
527557 fn test_http_method_equality ( ) {
528558 let method1 = HttpMethod :: Get ;
@@ -536,7 +566,7 @@ mod tests {
536566 #[ test]
537567 fn test_http_method_clone ( ) {
538568 let method = HttpMethod :: Get ;
539- let cloned = method. clone ( ) ;
569+ let cloned = method;
540570 assert_eq ! ( method, cloned) ;
541571 }
542572
0 commit comments