55//!
66//! These utilities are primarily useful for testing and debugging profiling exports.
77
8+ use anyhow:: Context as _;
89use std:: collections:: HashMap ;
910
1011/// Represents a parsed HTTP request
@@ -47,7 +48,7 @@ pub fn parse_http_request(data: &[u8]) -> anyhow::Result<HttpRequest> {
4748 let split_pos = data
4849 . windows ( separator. len ( ) )
4950 . position ( |window| window == separator)
50- . ok_or_else ( || anyhow :: anyhow! ( "No header/body separator found" ) ) ?;
51+ . context ( "No header/body separator found" ) ?;
5152
5253 let header_section = & data[ ..split_pos] ;
5354 let body = & data[ split_pos + separator. len ( ) ..] ;
@@ -59,7 +60,7 @@ pub fn parse_http_request(data: &[u8]) -> anyhow::Result<HttpRequest> {
5960 // Parse request line
6061 let request_line = lines
6162 . next ( )
62- . ok_or_else ( || anyhow :: anyhow! ( "No request line found" ) ) ?;
63+ . context ( "No request line found" ) ?;
6364 let parts: Vec < & str > = request_line. split_whitespace ( ) . collect ( ) ;
6465 if parts. len ( ) < 2 {
6566 anyhow:: bail!( "Invalid request line" ) ;
@@ -111,7 +112,7 @@ pub fn extract_boundary(content_type: &str) -> anyhow::Result<String> {
111112 part. strip_prefix ( boundary_prefix)
112113 . map ( |s| s. trim ( ) . to_string ( ) )
113114 } )
114- . ok_or_else ( || anyhow :: anyhow! ( "No boundary found in Content-Type" ) ) ?;
115+ . context ( "No boundary found in Content-Type" ) ?;
115116 Ok ( boundary)
116117}
117118
@@ -160,7 +161,7 @@ pub fn parse_multipart(body: &[u8], boundary: &str) -> anyhow::Result<Vec<Multip
160161
161162 // Find the next boundary or end
162163 let next_delimiter_pos = find_subsequence ( & body[ pos..] , delimiter_bytes)
163- . ok_or_else ( || anyhow :: anyhow! ( "Expected delimiter not found" ) ) ?;
164+ . context ( "Expected delimiter not found" ) ?;
164165
165166 // Extract this part's data (remove trailing CRLF before delimiter)
166167 let part_end = pos + next_delimiter_pos;
@@ -201,7 +202,7 @@ pub fn parse_multipart_part(data: &[u8]) -> anyhow::Result<MultipartPart> {
201202 let split_pos = data
202203 . windows ( separator. len ( ) )
203204 . position ( |window| window == separator)
204- . ok_or_else ( || anyhow :: anyhow! ( "No part header/body separator" ) ) ?;
205+ . context ( "No part header/body separator" ) ?;
205206
206207 let header_section = & data[ ..split_pos] ;
207208 let mut content = data[ split_pos + separator. len ( ) ..] . to_vec ( ) ;
0 commit comments