From 8c9ea43e232b55234fe24b04b023a7a7d5f18bd4 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sun, 27 Dec 2020 20:53:19 +0000 Subject: [PATCH] address clippy warnings --- actix-files/CHANGES.md | 1 + actix-files/src/range.rs | 41 +++++++++++++++++++---------------- src/middleware/errhandlers.rs | 1 + tests/test_httpserver.rs | 1 + 4 files changed, 25 insertions(+), 19 deletions(-) diff --git a/actix-files/CHANGES.md b/actix-files/CHANGES.md index 86e5e6563..49768419b 100644 --- a/actix-files/CHANGES.md +++ b/actix-files/CHANGES.md @@ -1,6 +1,7 @@ # Changes ## Unreleased - 2020-xx-xx +* `HttpRange::parse` now has its own error type. ## 0.5.0 - 2020-12-26 diff --git a/actix-files/src/range.rs b/actix-files/src/range.rs index e891ca7ec..e420ce414 100644 --- a/actix-files/src/range.rs +++ b/actix-files/src/range.rs @@ -1,3 +1,5 @@ +use derive_more::{Display, Error}; + /// HTTP Range header representation. #[derive(Debug, Clone, Copy)] pub struct HttpRange { @@ -11,17 +13,21 @@ pub struct HttpRange { const PREFIX: &str = "bytes="; const PREFIX_LEN: usize = 6; +#[derive(Debug, Clone, Display, Error)] +#[display(fmt = "Parse HTTP Range failed")] +pub struct ParseRangeErr(#[error(not(source))] ()); + impl HttpRange { /// Parses Range HTTP header string as per RFC 2616. /// /// `header` is HTTP Range header (e.g. `bytes=bytes=0-9`). /// `size` is full size of response (file). - pub fn parse(header: &str, size: u64) -> Result, ()> { + pub fn parse(header: &str, size: u64) -> Result, ParseRangeErr> { if header.is_empty() { return Ok(Vec::new()); } if !header.starts_with(PREFIX) { - return Err(()); + return Err(ParseRangeErr(())); } let size_sig = size as i64; @@ -34,13 +40,14 @@ impl HttpRange { .map(|ra| { let mut start_end_iter = ra.split('-'); - let start_str = start_end_iter.next().ok_or(())?.trim(); - let end_str = start_end_iter.next().ok_or(())?.trim(); + let start_str = start_end_iter.next().ok_or(ParseRangeErr(()))?.trim(); + let end_str = start_end_iter.next().ok_or(ParseRangeErr(()))?.trim(); if start_str.is_empty() { // If no start is specified, end specifies the // range start relative to the end of the file. - let mut length: i64 = end_str.parse().map_err(|_| ())?; + let mut length: i64 = + end_str.parse().map_err(|_| ParseRangeErr(()))?; if length > size_sig { length = size_sig; @@ -51,10 +58,10 @@ impl HttpRange { length: length as u64, })) } else { - let start: i64 = start_str.parse().map_err(|_| ())?; + let start: i64 = start_str.parse().map_err(|_| ParseRangeErr(()))?; if start < 0 { - return Err(()); + return Err(ParseRangeErr(())); } if start >= size_sig { no_overlap = true; @@ -65,10 +72,11 @@ impl HttpRange { // If no end is specified, range extends to end of the file. size_sig - start } else { - let mut end: i64 = end_str.parse().map_err(|_| ())?; + let mut end: i64 = + end_str.parse().map_err(|_| ParseRangeErr(()))?; if start > end { - return Err(()); + return Err(ParseRangeErr(())); } if end >= size_sig { @@ -89,7 +97,7 @@ impl HttpRange { let ranges: Vec = all_ranges.into_iter().filter_map(|x| x).collect(); if no_overlap && ranges.is_empty() { - return Err(()); + return Err(ParseRangeErr(())); } Ok(ranges) @@ -333,8 +341,7 @@ mod tests { if expected.is_empty() { continue; } else { - assert!( - false, + panic!( "parse({}, {}) returned error {:?}", header, size, @@ -346,28 +353,24 @@ mod tests { let got = res.unwrap(); if got.len() != expected.len() { - assert!( - false, + panic!( "len(parseRange({}, {})) = {}, want {}", header, size, got.len(), expected.len() ); - continue; } for i in 0..expected.len() { if got[i].start != expected[i].start { - assert!( - false, + panic!( "parseRange({}, {})[{}].start = {}, want {}", header, size, i, got[i].start, expected[i].start ) } if got[i].length != expected[i].length { - assert!( - false, + panic!( "parseRange({}, {})[{}].length = {}, want {}", header, size, i, got[i].length, expected[i].length ) diff --git a/src/middleware/errhandlers.rs b/src/middleware/errhandlers.rs index c0cb9594e..d2d3b0d8c 100644 --- a/src/middleware/errhandlers.rs +++ b/src/middleware/errhandlers.rs @@ -179,6 +179,7 @@ mod tests { assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "0001"); } + #[allow(clippy::unnecessary_wraps)] fn render_500_async( mut res: ServiceResponse, ) -> Result> { diff --git a/tests/test_httpserver.rs b/tests/test_httpserver.rs index 118640aca..d164f4445 100644 --- a/tests/test_httpserver.rs +++ b/tests/test_httpserver.rs @@ -63,6 +63,7 @@ async fn test_start() { let _ = sys.stop(); } +#[allow(clippy::unnecessary_wraps)] #[cfg(feature = "openssl")] fn ssl_acceptor() -> std::io::Result { use open_ssl::ssl::{SslAcceptor, SslFiletype, SslMethod};