@@ -8,7 +8,9 @@ use bytes::Bytes;
88use http:: { header, StatusCode } ;
99use http_body_util:: Full ;
1010use rustapi_core:: { ApiError , FromRequestParts , IntoResponse , Request , Response } ;
11- use rustapi_openapi:: { MediaType , Operation , OperationModifier , ResponseModifier , ResponseSpec , SchemaRef } ;
11+ use rustapi_openapi:: {
12+ MediaType , Operation , OperationModifier , ResponseModifier , ResponseSpec , SchemaRef ,
13+ } ;
1214use serde:: Serialize ;
1315use std:: collections:: HashMap ;
1416
@@ -94,12 +96,19 @@ impl AcceptHeader {
9496 ( part. to_string ( ) , 1.0 )
9597 } ;
9698
97- Some ( MediaTypeEntry { media_type, quality } )
99+ Some ( MediaTypeEntry {
100+ media_type,
101+ quality,
102+ } )
98103 } )
99104 . collect ( ) ;
100105
101106 // Sort by quality (descending)
102- entries. sort_by ( |a, b| b. quality . partial_cmp ( & a. quality ) . unwrap_or ( std:: cmp:: Ordering :: Equal ) ) ;
107+ entries. sort_by ( |a, b| {
108+ b. quality
109+ . partial_cmp ( & a. quality )
110+ . unwrap_or ( std:: cmp:: Ordering :: Equal )
111+ } ) ;
103112
104113 // Determine preferred format
105114 let preferred = Self :: determine_format ( & entries) ;
@@ -114,23 +123,23 @@ impl AcceptHeader {
114123 fn determine_format ( entries : & [ MediaTypeEntry ] ) -> OutputFormat {
115124 for entry in entries {
116125 let mt = entry. media_type . to_lowercase ( ) ;
117-
126+
118127 // Check for TOON
119128 if mt == TOON_CONTENT_TYPE || mt == TOON_CONTENT_TYPE_TEXT {
120129 return OutputFormat :: Toon ;
121130 }
122-
131+
123132 // Check for JSON
124133 if mt == JSON_CONTENT_TYPE || mt == "application/json" || mt == "text/json" {
125134 return OutputFormat :: Json ;
126135 }
127-
136+
128137 // Wildcard accepts anything, default to JSON
129138 if mt == "*/*" || mt == "application/*" || mt == "text/*" {
130139 return OutputFormat :: Json ;
131140 }
132141 }
133-
142+
134143 // Default to JSON
135144 OutputFormat :: Json
136145 }
@@ -139,7 +148,10 @@ impl AcceptHeader {
139148 pub fn accepts_toon ( & self ) -> bool {
140149 self . media_types . iter ( ) . any ( |e| {
141150 let mt = e. media_type . to_lowercase ( ) ;
142- mt == TOON_CONTENT_TYPE || mt == TOON_CONTENT_TYPE_TEXT || mt == "*/*" || mt == "application/*"
151+ mt == TOON_CONTENT_TYPE
152+ || mt == TOON_CONTENT_TYPE_TEXT
153+ || mt == "*/*"
154+ || mt == "application/*"
143155 } )
144156 }
145157
@@ -160,7 +172,7 @@ impl FromRequestParts for AcceptHeader {
160172 . and_then ( |v| v. to_str ( ) . ok ( ) )
161173 . map ( AcceptHeader :: parse)
162174 . unwrap_or_default ( ) ;
163-
175+
164176 Ok ( accept)
165177 }
166178}
@@ -235,32 +247,28 @@ impl<T> Negotiate<T> {
235247impl < T : Serialize > IntoResponse for Negotiate < T > {
236248 fn into_response ( self ) -> Response {
237249 match self . format {
238- OutputFormat :: Json => {
239- match serde_json:: to_vec ( & self . data ) {
240- Ok ( body) => http:: Response :: builder ( )
241- . status ( StatusCode :: OK )
242- . header ( header:: CONTENT_TYPE , JSON_CONTENT_TYPE )
243- . body ( Full :: new ( Bytes :: from ( body) ) )
244- . unwrap ( ) ,
245- Err ( err) => {
246- let error = ApiError :: internal ( format ! ( "JSON serialization error: {}" , err) ) ;
247- error. into_response ( )
248- }
250+ OutputFormat :: Json => match serde_json:: to_vec ( & self . data ) {
251+ Ok ( body) => http:: Response :: builder ( )
252+ . status ( StatusCode :: OK )
253+ . header ( header:: CONTENT_TYPE , JSON_CONTENT_TYPE )
254+ . body ( Full :: new ( Bytes :: from ( body) ) )
255+ . unwrap ( ) ,
256+ Err ( err) => {
257+ let error = ApiError :: internal ( format ! ( "JSON serialization error: {}" , err) ) ;
258+ error. into_response ( )
249259 }
250- }
251- OutputFormat :: Toon => {
252- match toon_format:: encode_default ( & self . data ) {
253- Ok ( body) => http:: Response :: builder ( )
254- . status ( StatusCode :: OK )
255- . header ( header:: CONTENT_TYPE , TOON_CONTENT_TYPE )
256- . body ( Full :: new ( Bytes :: from ( body) ) )
257- . unwrap ( ) ,
258- Err ( err) => {
259- let error = ApiError :: internal ( format ! ( "TOON serialization error: {}" , err) ) ;
260- error. into_response ( )
261- }
260+ } ,
261+ OutputFormat :: Toon => match toon_format:: encode_default ( & self . data ) {
262+ Ok ( body) => http:: Response :: builder ( )
263+ . status ( StatusCode :: OK )
264+ . header ( header:: CONTENT_TYPE , TOON_CONTENT_TYPE )
265+ . body ( Full :: new ( Bytes :: from ( body) ) )
266+ . unwrap ( ) ,
267+ Err ( err) => {
268+ let error = ApiError :: internal ( format ! ( "TOON serialization error: {}" , err) ) ;
269+ error. into_response ( )
262270 }
263- }
271+ } ,
264272 }
265273 }
266274}
@@ -275,7 +283,7 @@ impl<T: Send> OperationModifier for Negotiate<T> {
275283impl < T : Serialize > ResponseModifier for Negotiate < T > {
276284 fn update_response ( op : & mut Operation ) {
277285 let mut content = HashMap :: new ( ) ;
278-
286+
279287 // JSON response
280288 content. insert (
281289 JSON_CONTENT_TYPE . to_string ( ) ,
@@ -286,7 +294,7 @@ impl<T: Serialize> ResponseModifier for Negotiate<T> {
286294 } ) ) ,
287295 } ,
288296 ) ;
289-
297+
290298 // TOON response
291299 content. insert (
292300 TOON_CONTENT_TYPE . to_string ( ) ,
@@ -299,7 +307,8 @@ impl<T: Serialize> ResponseModifier for Negotiate<T> {
299307 ) ;
300308
301309 let response = ResponseSpec {
302- description : "Content-negotiated response (JSON or TOON based on Accept header)" . to_string ( ) ,
310+ description : "Content-negotiated response (JSON or TOON based on Accept header)"
311+ . to_string ( ) ,
303312 content : Some ( content) ,
304313 } ;
305314 op. responses . insert ( "200" . to_string ( ) , response) ;
0 commit comments