Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: apply small refactoring #168

Merged
merged 13 commits into from
Mar 4, 2024
Merged
6 changes: 2 additions & 4 deletions src/common/accept_ranges.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use http::HeaderValue;

use util::FlatCsv;

/// `Accept-Ranges` header, defined in [RFC7233](http://tools.ietf.org/html/rfc7233#section-2.3)
Expand Down Expand Up @@ -36,12 +34,12 @@ derive_header! {
name: ACCEPT_RANGES
}

const ACCEPT_RANGES_BYTES: HeaderValue = ::HeaderValue::from_static("bytes");
const ACCEPT_RANGES_BYTES: &str = "bytes";

impl AcceptRanges {
/// A constructor to easily create the common `Accept-Ranges: bytes` header.
pub fn bytes() -> Self {
AcceptRanges(ACCEPT_RANGES_BYTES.into())
AcceptRanges(::HeaderValue::from_static(ACCEPT_RANGES_BYTES).into())
}

/// Check if the unit is `bytes`.
Expand Down
4 changes: 2 additions & 2 deletions src/common/access_control_allow_headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ derive_header! {

impl AccessControlAllowHeaders {
/// Returns an iterator over `HeaderName`s contained within.
pub fn iter<'a>(&'a self) -> impl Iterator<Item = HeaderName> + 'a {
pub fn iter(&self) -> impl Iterator<Item = HeaderName> + '_ {
self.0
.iter()
.map(|s| s.parse().ok())
.take_while(|val| val.is_some())
.filter_map(|val| val)
.flatten()
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/common/access_control_allow_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ derive_header! {

impl AccessControlAllowMethods {
/// Returns an iterator over `Method`s contained within.
pub fn iter<'a>(&'a self) -> impl Iterator<Item = Method> + 'a {
pub fn iter(&self) -> impl Iterator<Item = Method> + '_ {
self.0.iter().filter_map(|s| s.parse().ok())
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/common/access_control_allow_origin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl TryFromValues for OriginOrAny {
impl<'a> From<&'a OriginOrAny> for HeaderValue {
fn from(origin: &'a OriginOrAny) -> HeaderValue {
match origin {
OriginOrAny::Origin(ref origin) => origin.into_value(),
OriginOrAny::Origin(ref origin) => origin.to_value(),
OriginOrAny::Any => HeaderValue::from_static("*"),
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/common/access_control_expose_headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ derive_header! {

impl AccessControlExposeHeaders {
/// Returns an iterator over `HeaderName`s contained within.
pub fn iter<'a>(&'a self) -> impl Iterator<Item = HeaderName> + 'a {
pub fn iter(&self) -> impl Iterator<Item = HeaderName> + '_ {
self.0.iter().filter_map(|s| s.parse().ok())
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/common/access_control_request_headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ derive_header! {

impl AccessControlRequestHeaders {
/// Returns an iterator over `HeaderName`s contained within.
pub fn iter<'a>(&'a self) -> impl Iterator<Item = HeaderName> + 'a {
pub fn iter(&self) -> impl Iterator<Item = HeaderName> + '_ {
self.0.iter().filter_map(|s| s.parse().ok())
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/common/allow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ derive_header! {

impl Allow {
/// Returns an iterator over `Method`s contained within.
pub fn iter<'a>(&'a self) -> impl Iterator<Item = Method> + 'a {
pub fn iter(&self) -> impl Iterator<Item = Method> + '_ {
self.0.iter().filter_map(|s| s.parse().ok())
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/common/authorization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl Authorization<Bearer> {
pub fn bearer(token: &str) -> Result<Self, InvalidBearerToken> {
HeaderValueString::from_string(format!("Bearer {}", token))
.map(|val| Authorization(Bearer(val)))
.ok_or_else(|| InvalidBearerToken { _inner: () })
.ok_or(InvalidBearerToken { _inner: () })
}

/// View the token part as a `&str`.
Expand Down
10 changes: 5 additions & 5 deletions src/common/cache_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,16 +284,16 @@ impl FromIterator<KnownDirective> for FromIter {
cc.flags.insert(Flags::PROXY_REVALIDATE);
}
Directive::MaxAge(secs) => {
cc.max_age = Some(Duration::from_secs(secs.into()).into());
cc.max_age = Some(Duration::from_secs(secs).into());
}
Directive::MaxStale(secs) => {
cc.max_stale = Some(Duration::from_secs(secs.into()).into());
cc.max_stale = Some(Duration::from_secs(secs).into());
}
Directive::MinFresh(secs) => {
cc.min_fresh = Some(Duration::from_secs(secs.into()).into());
cc.min_fresh = Some(Duration::from_secs(secs).into());
}
Directive::SMaxAge(secs) => {
cc.s_max_age = Some(Duration::from_secs(secs.into()).into());
cc.s_max_age = Some(Duration::from_secs(secs).into());
}
}
}
Expand Down Expand Up @@ -420,7 +420,7 @@ impl FromStr for KnownDirective {
"" => return Err(()),
_ => match s.find('=') {
Some(idx) if idx + 1 < s.len() => {
match (&s[..idx], (&s[idx + 1..]).trim_matches('"')) {
match (&s[..idx], (s[idx + 1..]).trim_matches('"')) {
("max-age", secs) => secs.parse().map(Directive::MaxAge).map_err(|_| ())?,
("max-stale", secs) => {
secs.parse().map(Directive::MaxStale).map_err(|_| ())?
Expand Down
7 changes: 2 additions & 5 deletions src/common/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,7 @@ impl Connection {
/// ```
pub fn contains(&self, name: impl AsConnectionOption) -> bool {
let s = name.as_connection_option();
self.0
.iter()
.find(|&opt| opt.eq_ignore_ascii_case(s))
.is_some()
self.0.iter().any(|opt| opt.eq_ignore_ascii_case(s))
}
}

Expand All @@ -112,7 +109,7 @@ mod sealed {

impl<'a> AsConnectionOption for &'a str {
fn as_connection_option(&self) -> &str {
*self
self
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/common/content_encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl ContentEncoding {
/// ```
pub fn contains(&self, coding: impl AsCoding) -> bool {
let s = coding.as_coding();
self.0.iter().find(|&opt| opt == s).is_some()
self.0.iter().any(|opt| opt == s)
}
}

Expand All @@ -87,7 +87,7 @@ mod sealed {

impl<'a> Sealed for &'a str {
fn as_coding(&self) -> &str {
*self
self
}
}
}
2 changes: 1 addition & 1 deletion src/common/content_type.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fmt;

use mime::{self, Mime};
use mime::Mime;

/// `Content-Type` header, defined in
/// [RFC7231](http://tools.ietf.org/html/rfc7231#section-3.1.1.5)
Expand Down
2 changes: 1 addition & 1 deletion src/common/etag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl FromStr for ETag {

EntityTag::from_owned(val)
.map(ETag)
.ok_or_else(|| InvalidETag { _inner: () })
.ok_or(InvalidETag { _inner: () })
}
}

Expand Down
9 changes: 3 additions & 6 deletions src/common/origin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@ impl Origin {
/// Checks if `Origin` is `null`.
#[inline]
pub fn is_null(&self) -> bool {
match self.0 {
OriginOrNull::Null => true,
_ => false,
}
matches!(self.0, OriginOrNull::Null)
}

/// Get the "scheme" part of this origin.
Expand Down Expand Up @@ -101,15 +98,15 @@ impl Origin {
HeaderValue::from_maybe_shared(bytes)
.ok()
.and_then(|val| Self::try_from_value(&val))
.ok_or_else(|| InvalidOrigin { _inner: () })
.ok_or(InvalidOrigin { _inner: () })
}

// Used in AccessControlAllowOrigin
pub(super) fn try_from_value(value: &HeaderValue) -> Option<Self> {
OriginOrNull::try_from_value(value).map(Origin)
}

pub(super) fn into_value(&self) -> HeaderValue {
pub(super) fn to_value(&self) -> HeaderValue {
(&self.0).into()
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/common/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::ops::{Bound, RangeBounds};
/// # ABNF
///
/// ```text
/// Range = byte-ranges-specifier / other-ranges-specifier
/// Range = byte-ranges-specifier / other-ranges-specifier
/// other-ranges-specifier = other-range-unit "=" other-range-set
/// other-range-set = 1*VCHAR
///
Expand Down Expand Up @@ -66,10 +66,10 @@ impl Range {
///
/// The length of the content is passed as an argument, and all ranges
/// that can be satisfied will be iterated.
pub fn satisfiable_ranges<'a>(
&'a self,
pub fn satisfiable_ranges(
&self,
len: u64,
) -> impl Iterator<Item = (Bound<u64>, Bound<u64>)> + 'a {
) -> impl Iterator<Item = (Bound<u64>, Bound<u64>)> + '_ {
let s = self
.0
.to_str()
Expand Down
2 changes: 1 addition & 1 deletion src/common/sec_websocket_accept.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn sign(key: &[u8]) -> SecWebsocketAccept {
let mut sha1 = Sha1::default();
sha1.update(key);
sha1.update(&b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11"[..]);
let b64 = Bytes::from(ENGINE.encode(&sha1.finalize()));
let b64 = Bytes::from(ENGINE.encode(sha1.finalize()));

let val = ::HeaderValue::from_maybe_shared(b64).expect("base64 is a valid value");

Expand Down
12 changes: 6 additions & 6 deletions src/common/strict_transport_security.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,13 @@ fn from_str(s: &str) -> Result<StrictTransportSecurity, ::Error> {
}
}
})
.fold(Some((None, None)), |res, dir| match (res, dir) {
(Some((None, sub)), Some(Directive::MaxAge(age))) => Some((Some(age), sub)),
(Some((age, None)), Some(Directive::IncludeSubdomains)) => Some((age, Some(()))),
(Some((Some(_), _)), Some(Directive::MaxAge(_)))
| (Some((_, Some(_))), Some(Directive::IncludeSubdomains))
.try_fold((None, None), |res, dir| match (res, dir) {
((None, sub), Some(Directive::MaxAge(age))) => Some((Some(age), sub)),
((age, None), Some(Directive::IncludeSubdomains)) => Some((age, Some(()))),
((Some(_), _), Some(Directive::MaxAge(_)))
| ((_, Some(_)), Some(Directive::IncludeSubdomains))
| (_, None) => None,
(res, _) => res,
(res, _) => Some(res),
})
.and_then(|res| match res {
(Some(age), sub) => Some(StrictTransportSecurity {
Expand Down
1 change: 0 additions & 1 deletion src/map_ext.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use super::{Error, Header, HeaderValue};
use http;

/// An extension trait adding "typed" methods to `http::HeaderMap`.
pub trait HeaderMapExt: self::sealed::Sealed {
Expand Down
2 changes: 1 addition & 1 deletion src/util/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ fn check_slice_validity(slice: &[u8]) -> bool {
// The debug_assert is just in case we use check_slice_validity in
// some new context that didnt come from a HeaderValue.
debug_assert!(
(c >= b'\x21' && c <= b'\x7e') | (c >= b'\x80'),
(b'\x21'..=b'\x7e').contains(&c) | (c >= b'\x80'),
"EntityTag expects HeaderValue to have check for control characters"
);
c != b'"'
Expand Down
5 changes: 3 additions & 2 deletions src/util/flat_csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ impl<Sep: Separator> FlatCsv<Sep> {
let mut in_quotes = false;
value_str
.split(move |c| {
#[allow(clippy::collapsible_else_if)]
if in_quotes {
if c == '"' {
in_quotes = false;
Expand Down Expand Up @@ -113,7 +114,7 @@ impl<'a, Sep: Separator> FromIterator<&'a HeaderValue> for FlatCsv<Sep> {
.next()
.cloned()
.map(|val| BytesMut::from(val.as_bytes()))
.unwrap_or_else(|| BytesMut::new());
.unwrap_or_default();

for val in values {
buf.extend_from_slice(&[Sep::BYTE, b' ']);
Expand Down Expand Up @@ -144,7 +145,7 @@ impl<Sep: Separator> FromIterator<HeaderValue> for FlatCsv<Sep> {
let mut buf = values
.next()
.map(|val| BytesMut::from(val.as_bytes()))
.unwrap_or_else(|| BytesMut::new());
.unwrap_or_default();

for val in values {
buf.extend_from_slice(&[Sep::BYTE, b' ']);
Expand Down
1 change: 0 additions & 1 deletion src/util/http_date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::time::SystemTime;

use bytes::Bytes;
use http::header::HeaderValue;
use httpdate;

use super::IterExt;

Expand Down
2 changes: 1 addition & 1 deletion src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,6 @@ impl TryFromValues for HeaderValue {
where
I: Iterator<Item = &'i HeaderValue>,
{
values.next().cloned().ok_or_else(|| ::Error::invalid())
values.next().cloned().ok_or_else(::Error::invalid)
}
}