diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 00000000..7828abfa --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,71 @@ +name: CI +on: + pull_request: + branches: [ main ] + push: + branches: [ main ] + +# Concurrency strategy: +# github.workflow: distinguish this workflow from others +# github.event_name: distinguish `push` event from `pull_request` event +# github.event.number: set to the number of the pull request if `pull_request` event +# github.run_id: otherwise, it's a `push` event, only cancel if we rerun the workflow +# +# Reference: +# https://docs.github.com/en/actions/using-jobs/using-concurrency +# https://docs.github.com/en/actions/learn-github-actions/contexts#github-context +concurrency: + group: ${{ github.workflow }}-${{ github.event_name }}-${{ github.event.number || github.run_id }} + cancel-in-progress: true +jobs: + check: + name: Check + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v4 + - name: Install toolchain + uses: dtolnay/rust-toolchain@nightly + with: + components: rustfmt,clippy + - uses: Swatinem/rust-cache@v2 + - name: Check format + run: cargo +nightly fmt --all + - name: Check clippy + run: cargo +nightly clippy --all-targets --all-features -- -D warnings + + test: + name: Run tests + strategy: + matrix: + rust-version: [ stable, beta, nightly ] + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v4 + - uses: Swatinem/rust-cache@v2 + - name: Install toolchain + uses: dtolnay/rust-toolchain@master + with: + toolchain: ${{ matrix.rust-version }} + - name: Build all targets + run: cargo build --workspace --all-features --bins --tests --examples --benches --lib + - name: Run unit tests + shell: bash + run: cargo test --all-features -- --nocapture + + required: + name: Required + runs-on: ubuntu-22.04 + if: ${{ always() }} + needs: + - check + - test + steps: + - name: Guardian + run: | + if [[ ! ( \ + "${{ needs.check.result }}" == "success" \ + && "${{ needs.test.result }}" == "success" \ + ) ]]; then + echo "Required jobs haven't been completed successfully." + exit -1 + fi diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 8c077c65..00000000 --- a/.travis.yml +++ /dev/null @@ -1,15 +0,0 @@ -sudo: false -language: rust -script: cargo test --all -matrix: - fast_finish: true - allow_failures: - - rust: nightly - include: - - rust: stable - - rust: beta - - rust: nightly - - - rust: stable - script: cargo test --all --features macro - diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md deleted file mode 100644 index 365ac25b..00000000 --- a/CONTRIBUTING.md +++ /dev/null @@ -1,6 +0,0 @@ -# Contributing to mime - -## License - -Licensed under MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) - diff --git a/Cargo.toml b/Cargo.toml index 635d6fd0..7c18f323 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,4 @@ [package] - name = "mime" version = "0.4.0-a.0" # remember to update html_root_url authors = ["Sean McArthur "] @@ -17,7 +16,7 @@ mime-macro = { path = "./mime-macro", optional = true } mime-parse = { path = "./mime-parse" } proc-macro-hack = { version = "0.5", optional = true } quoted-string = "0.2.2" -serde1 = { version = "1", optional = true, package = "serde" } +serde1 = { version = "1.0", optional = true, package = "serde" } [features] macro = ["mime-macro", "proc-macro-hack"] @@ -25,7 +24,7 @@ macro = ["mime-macro", "proc-macro-hack"] [workspace] members = [ - "./", + ".", "mime-macro", "mime-parse", ] diff --git a/LICENSE b/LICENSE index aa33b8e7..7da4eea2 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2014-2019 Sean McArthur +Copyright (c) 2014-2024 Sean McArthur Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 70c477df..83c6d9e6 100644 --- a/README.md +++ b/README.md @@ -1,18 +1,13 @@ # mime -[![Build Status](https://travis-ci.org/hyperium/mime.svg?branch=master)](https://travis-ci.org/hyperium/mime) [![crates.io](https://img.shields.io/crates/v/mime.svg)](https://crates.io/crates/mime) [![docs.rs](https://docs.rs/mime/badge.svg)](https://docs.rs/mime) Support MIME (HTTP Media Types) as strong types in Rust. -[Documentation](https://docs.rs/mime) - ## Usage ```rust -extern crate mime; - fn main() { // common types are constants let text = mime::TEXT_PLAIN; @@ -21,13 +16,21 @@ fn main() { match (text.type_(), text.subtype()) { (mime::TEXT, mime::PLAIN) => { // plain text! - }, + } (mime::TEXT, _) => { // structured text! - }, + } _ => { // not text! } } } ``` + +## Documentation + +Read the online documents at https://docs.rs/mime. + +## License + +This project is licensed under [The MIT License](LICENSE). diff --git a/benches/cmp.rs b/benches/cmp.rs index ce4c09b1..641d085a 100644 --- a/benches/cmp.rs +++ b/benches/cmp.rs @@ -18,7 +18,7 @@ fn bench_eq_parsed_atom(b: &mut Bencher) { #[bench] fn bench_eq_parsed_dynamic(b: &mut Bencher) { let mime1 = "text/foo; charset=utf-8".parse::().unwrap(); - let mime2 = mime1.clone(); + let mime2 = mime1.clone(); b.bytes = mime1.as_ref().len() as u64; b.iter(|| { assert_eq!(mime1, mime2); @@ -27,8 +27,10 @@ fn bench_eq_parsed_dynamic(b: &mut Bencher) { #[bench] fn bench_eq_multiple_parameters(b: &mut Bencher) { - let mime1 = "text/foo; aaa=bbb; ccc=ddd; eee=fff; ggg=hhh".parse::().unwrap(); - let mime2 = mime1.clone(); + let mime1 = "text/foo; aaa=bbb; ccc=ddd; eee=fff; ggg=hhh" + .parse::() + .unwrap(); + let mime2 = mime1.clone(); b.bytes = mime1.as_ref().len() as u64; b.iter(|| { assert_eq!(mime1, mime2); @@ -65,7 +67,7 @@ fn bench_ne_consts(b: &mut Bencher) { } #[bench] -fn bench_eq_type_(b: &mut Bencher) { +fn bench_eq_type(b: &mut Bencher) { let mime = TEXT_PLAIN_UTF_8; let name = TEXT; b.bytes = name.len() as u64; diff --git a/benches/fmt.rs b/benches/fmt.rs index 106de1a1..8785ff58 100644 --- a/benches/fmt.rs +++ b/benches/fmt.rs @@ -8,12 +8,14 @@ use test::Bencher; #[bench] fn bench_fmt(b: &mut Bencher) { use std::fmt::Write; - let mime = ::mime::TEXT_PLAIN_UTF_8; + let mime = mime::TEXT_PLAIN_UTF_8; b.bytes = mime.to_string().as_bytes().len() as u64; let mut s = String::with_capacity(64); b.iter(|| { let _ = write!(s, "{}", mime); - ::test::black_box(&s); - unsafe { s.as_mut_vec().set_len(0); } + test::black_box(&s); + unsafe { + s.as_mut_vec().set_len(0); + } }) } diff --git a/benches/parse.rs b/benches/parse.rs index 667d19c9..f57ffa1c 100644 --- a/benches/parse.rs +++ b/benches/parse.rs @@ -6,7 +6,6 @@ extern crate test; use mime::MediaType; use test::Bencher; - #[bench] fn text_plain(b: &mut Bencher) { let s = "text/plain"; diff --git a/mime-macro/Cargo.toml b/mime-macro/Cargo.toml index d1a085c2..bc150c16 100644 --- a/mime-macro/Cargo.toml +++ b/mime-macro/Cargo.toml @@ -1,5 +1,4 @@ [package] - name = "mime-macro" version = "0.0.0" description = "mime procedural macros" @@ -14,7 +13,7 @@ proc-macro = true [dependencies] mime-parse = { path = "../mime-parse" } -proc-macro2 = "0.4" +proc-macro2 = "1.0" proc-macro-hack = "0.5" -quote = "0.6" -syn = "0.15" +quote = "1.0" +syn = "2.0" diff --git a/mime-macro/src/lib.rs b/mime-macro/src/lib.rs index 5b6f8e0c..37fa111b 100644 --- a/mime-macro/src/lib.rs +++ b/mime-macro/src/lib.rs @@ -1,8 +1,6 @@ -extern crate proc_macro; - use proc_macro::TokenStream; -use proc_macro_hack::proc_macro_hack; use proc_macro2::Span; +use proc_macro_hack::proc_macro_hack; use quote::quote; #[proc_macro_hack] @@ -24,13 +22,13 @@ pub fn media_type(tokens: TokenStream) -> TokenStream { quote! { $crate::private::Source::Atom(0, #s) } - }, + } a => { let s = mime.as_ref(); quote! { $crate::private::Source::Atom(#a, #s) } - }, + } }; let slash = mime.private_subtype_offset(); let plus = match mime.private_suffix_offset() { @@ -66,13 +64,10 @@ fn parse_mime_lit(value: &str) -> Result { match mime { Ok(mime) => match mime.private_params_source() { - mime_parse::ParamSource::None | - mime_parse::ParamSource::Utf8(_) => Ok(mime), + mime_parse::ParamSource::None | mime_parse::ParamSource::Utf8(_) => Ok(mime), mime_parse::ParamSource::One(..) => Ok(mime), - _ => Err("multiple parameters not supported yet".into()) + _ => Err("multiple parameters not supported yet".into()), }, - Err(err) => { - Err(format!("invalid MediaType: {}", err)) - } + Err(err) => Err(format!("invalid MediaType: {}", err)), } } diff --git a/mime-parse/Cargo.toml b/mime-parse/Cargo.toml index 601b945e..b4173a43 100644 --- a/mime-parse/Cargo.toml +++ b/mime-parse/Cargo.toml @@ -1,5 +1,4 @@ [package] - name = "mime-parse" version = "0.0.0" description = "mime parsing internals" diff --git a/mime-parse/src/constants.rs b/mime-parse/src/constants.rs index 634c2bf3..0009b92f 100644 --- a/mime-parse/src/constants.rs +++ b/mime-parse/src/constants.rs @@ -60,7 +60,6 @@ macro_rules! mime_constant { ) } - #[cfg(test)] macro_rules! mime_constant_test { ($id:ident, $src:expr, $slash:expr) => ( @@ -135,12 +134,8 @@ impl Atoms { ); match params { - InternParams::Utf8(semicolon) => { - Atoms::intern_charset_utf8(s, slash, semicolon) - }, - InternParams::None => { - Atoms::intern_no_params(s, slash) - }, + InternParams::Utf8(semicolon) => Atoms::intern_charset_utf8(s, slash, semicolon), + InternParams::None => Atoms::intern_no_params(s, slash), } } @@ -199,7 +194,7 @@ impl Atoms { if sub == CSV { return Atoms::TEXT_CSV; } - }, + } 4 => { if sub == HTML { return Atoms::TEXT_HTML; @@ -222,7 +217,7 @@ impl Atoms { if sub == EVENT_STREAM { return Atoms::TEXT_EVENT_STREAM; } - }, + } 20 => { if sub == TAB_SEPARATED_VALUES { return Atoms::TEXT_TAB_SEPARATED_VALUES; @@ -236,16 +231,16 @@ impl Atoms { if sub == WOFF { return Atoms::FONT_WOFF; } - }, + } 5 => { if sub == WOFF2 { return Atoms::FONT_WOFF2; } - }, + } _ => (), } } - }, + } 5 => { if top == IMAGE { match sub.len() { @@ -269,14 +264,13 @@ impl Atoms { if sub == JPEG { return Atoms::IMAGE_JPEG; } - }, + } 7 => { if sub == SVG { return Atoms::IMAGE_SVG; } - }, + } _ => (), - } } else if top == VIDEO { match sub.len() { @@ -284,7 +278,7 @@ impl Atoms { if sub.as_bytes()[0] == b'*' { return Atoms::VIDEO_STAR; } - }, + } _ => (), } } else if top == AUDIO { @@ -293,11 +287,11 @@ impl Atoms { if sub.as_bytes()[0] == b'*' { return Atoms::AUDIO_STAR; } - }, + } _ => (), } } - }, + } 11 => { if top == APPLICATION { match sub.len() { @@ -310,22 +304,22 @@ impl Atoms { if sub == JSON { return Atoms::APPLICATION_JSON; } - }, + } 7 => { if sub == MSGPACK { return Atoms::APPLICATION_MSGPACK; } - }, + } 10 => { if sub == JAVASCRIPT { return Atoms::APPLICATION_JAVASCRIPT; } - }, + } 11 => { if sub == "dns-message" { return Atoms::APPLICATION_DNS; } - }, + } 12 => { if sub == OCTET_STREAM { return Atoms::APPLICATION_OCTET_STREAM; @@ -475,4 +469,3 @@ mimes! { VIDEO_STAR, "video/*", 5; AUDIO_STAR, "audio/*", 5; } - diff --git a/mime-parse/src/lib.rs b/mime-parse/src/lib.rs index 8d18e0f7..c68d76b4 100644 --- a/mime-parse/src/lib.rs +++ b/mime-parse/src/lib.rs @@ -60,10 +60,7 @@ pub enum ParseError { MissingSlash, MissingEqual, MissingQuote, - InvalidToken { - pos: usize, - byte: Byte, - }, + InvalidToken { pos: usize, byte: Byte }, InvalidRange, TooLong, } @@ -85,14 +82,15 @@ impl fmt::Debug for Byte { } } -impl Error for ParseError { -} +impl Error for ParseError {} impl fmt::Display for ParseError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let description = match self { ParseError::MissingSlash => "a slash (/) was missing between the type and subtype", - ParseError::MissingEqual => "an equals sign (=) was missing between a parameter and its value", + ParseError::MissingEqual => { + "an equals sign (=) was missing between a parameter and its value" + } ParseError::MissingQuote => "a quote (\") was missing from a parameter value", ParseError::InvalidToken { .. } => "invalid token", ParseError::InvalidRange => "unexpected asterisk", @@ -128,7 +126,8 @@ impl Mime { #[inline] pub fn suffix(&self) -> Option<&str> { let end = self.semicolon_or_end(); - self.plus.map(|idx| &self.source.as_ref()[idx as usize + 1..end]) + self.plus + .map(|idx| &self.source.as_ref()[idx as usize + 1..end]) } #[doc(hidden)] @@ -142,12 +141,10 @@ impl Mime { ParamSource::Utf8(_) => ParamsInner::Utf8, ParamSource::One(_, a) => ParamsInner::Inlined(&self.source, Inline::One(a)), ParamSource::Two(_, a, b) => ParamsInner::Inlined(&self.source, Inline::Two(a, b)), - ParamSource::Custom(_, ref params) => { - ParamsInner::Custom { - source: &self.source, - params: params.iter(), - } - } + ParamSource::Custom(_, ref params) => ParamsInner::Custom { + source: &self.source, + params: params.iter(), + }, ParamSource::None => ParamsInner::None, }; @@ -159,7 +156,7 @@ impl Mime { &self.params } - pub fn param<'a>(&'a self, attr: &str) -> Option<&'a str> { + pub fn param(&self, attr: &str) -> Option<&str> { self.params().find(|e| attr == e.0).map(|e| e.1) } @@ -188,17 +185,18 @@ impl Mime { #[inline] fn semicolon(&self) -> Option { match self.params { - ParamSource::Utf8(i) | - ParamSource::One(i, ..) | - ParamSource::Two(i, ..) | - ParamSource::Custom(i, _) => Some(i as usize), + ParamSource::Utf8(i) + | ParamSource::One(i, ..) + | ParamSource::Two(i, ..) + | ParamSource::Custom(i, _) => Some(i as usize), ParamSource::None => None, } } #[inline] fn semicolon_or_end(&self) -> usize { - self.semicolon().unwrap_or_else(|| self.source.as_ref().len()) + self.semicolon() + .unwrap_or_else(|| self.source.as_ref().len()) } #[doc(hidden)] @@ -256,13 +254,13 @@ impl fmt::Display for Mime { #[inline] fn as_u16(i: usize) -> u16 { - debug_assert!(i <= std::u16::MAX as usize, "as_u16 overflow"); + debug_assert!(i <= u16::MAX as usize, "as_u16 overflow"); i as u16 } #[inline] fn range(index: (u16, u16)) -> std::ops::Range { - index.0 as usize .. index.1 as usize + index.0 as usize..index.1 as usize } // ===== impl Parser ===== @@ -270,16 +268,12 @@ fn range(index: (u16, u16)) -> std::ops::Range { impl Parser { #[inline] pub fn can_range() -> Self { - Parser { - can_range: true, - } + Parser { can_range: true } } #[inline] pub fn cannot_range() -> Self { - Parser { - can_range: false, - } + Parser { can_range: false } } pub fn parse(&self, src: impl Parse) -> Result { @@ -287,7 +281,6 @@ impl Parser { } } - fn lower_ascii_with_params(s: &str, semi: usize, params: &[IndexedPair]) -> String { let mut owned = s.to_owned(); owned[..semi].make_ascii_lowercase(); @@ -305,9 +298,7 @@ fn lower_ascii_with_params(s: &str, semi: usize, params: &[IndexedPair]) -> Stri owned } - -// Params =================== - +// ========= Params ========== enum ParamsInner<'a> { Utf8, @@ -319,7 +310,6 @@ enum ParamsInner<'a> { None, } - enum Inline { Done, One(IndexedPair), @@ -345,34 +335,33 @@ impl<'a> Iterator for Params<'a> { let value = ("charset", "utf-8"); self.0 = ParamsInner::None; Some(value) - }, + } ParamsInner::Inlined(source, ref mut inline) => { let next = match *inline { - Inline::Done => { - None - } + Inline::Done => None, Inline::One(one) => { *inline = Inline::Done; Some(one) - }, + } Inline::Two(one, two) => { *inline = Inline::One(two); Some(one) - }, + } }; next.map(|(name, value)| { let name = &source.as_ref()[range(name)]; let value = &source.as_ref()[range(value)]; (name, value) }) - }, - ParamsInner::Custom { source, ref mut params } => { - params.next().map(|&(name, value)| { - let name = &source.as_ref()[range(name)]; - let value = &source.as_ref()[range(value)]; - (name, value) - }) - }, + } + ParamsInner::Custom { + source, + ref mut params, + } => params.next().map(|&(name, value)| { + let name = &source.as_ref()[range(name)]; + let value = &source.as_ref()[range(value)]; + (name, value) + }), ParamsInner::None => None, } } @@ -413,4 +402,3 @@ impl<'a> Sealed for &'a String { } impl<'a> Parse for &'a String {} - diff --git a/mime-parse/src/rfc7231.rs b/mime-parse/src/rfc7231.rs index 50297791..524a18ec 100644 --- a/mime-parse/src/rfc7231.rs +++ b/mime-parse/src/rfc7231.rs @@ -1,17 +1,6 @@ use crate::{ - as_u16, - constants, - Atoms, - Byte, - InternParams, - lower_ascii_with_params, - Mime, - Parse, - Parser, - ParseError, - ParamSource, - range, - Source, + as_u16, constants, lower_ascii_with_params, range, Atoms, Byte, InternParams, Mime, + ParamSource, Parse, ParseError, Parser, Source, }; // From [RFC6838](http://tools.ietf.org/html/rfc6838#section-4.2): @@ -76,12 +65,14 @@ pub(crate) fn parse(opts: &Parser, src: impl Parse) -> Result slash = as_u16(i); start = i + 1; break; - }, + } None => return Err(ParseError::MissingSlash), // EOF and no toplevel is no Mime - Some((pos, byte)) => return Err(ParseError::InvalidToken { - pos: pos, - byte: Byte(byte), - }), + Some((pos, byte)) => { + return Err(ParseError::InvalidToken { + pos: pos, + byte: Byte(byte), + }) + } }; } @@ -91,15 +82,15 @@ pub(crate) fn parse(opts: &Parser, src: impl Parse) -> Result match iter.next() { Some((i, b'+')) if i > start => { plus = Some(as_u16(i)); - }, + } Some((i, b';')) if i > start => { start = i; break; - }, + } Some((i, b' ')) if i > start => { start = i; break; - }, + } Some((i, b'*')) if i == start && opts.can_range => { // sublevel star can only be the first character, and the next // must either be the end, or `;` @@ -107,19 +98,23 @@ pub(crate) fn parse(opts: &Parser, src: impl Parse) -> Result Some((i, b';')) => { start = i; break; - }, - None => return Ok(Mime { - source: Atoms::intern(s, slash, InternParams::None), - slash, - plus, - params: ParamSource::None, - }), - Some((pos, byte)) => return Err(ParseError::InvalidToken { - pos, - byte: Byte(byte), - }), + } + None => { + return Ok(Mime { + source: Atoms::intern(s, slash, InternParams::None), + slash, + plus, + params: ParamSource::None, + }) + } + Some((pos, byte)) => { + return Err(ParseError::InvalidToken { + pos, + byte: Byte(byte), + }) + } } - }, + } Some((_, c)) if is_token(c) => (), None => { @@ -129,11 +124,13 @@ pub(crate) fn parse(opts: &Parser, src: impl Parse) -> Result plus, params: ParamSource::None, }); - }, - Some((pos, byte)) => return Err(ParseError::InvalidToken { - pos: pos, - byte: Byte(byte), - }) + } + Some((pos, byte)) => { + return Err(ParseError::InvalidToken { + pos: pos, + byte: Byte(byte), + }) + } }; } @@ -150,11 +147,19 @@ pub(crate) fn parse(opts: &Parser, src: impl Parse) -> Result b == b';' || b == b' ' }); Atoms::intern(&s[..start], slash, InternParams::None) - }, - ParamSource::Utf8(params_start) => Atoms::intern(s, slash, InternParams::Utf8(params_start as usize)), - ParamSource::One(params_start, a) => Source::Dynamic(lower_ascii_with_params(s, params_start as usize, &[a])), - ParamSource::Two(params_start, a, b) => Source::Dynamic(lower_ascii_with_params(s, params_start as usize, &[a, b])), - ParamSource::Custom(params_start, ref indices) => Source::Dynamic(lower_ascii_with_params(s, params_start as usize, indices)), + } + ParamSource::Utf8(params_start) => { + Atoms::intern(s, slash, InternParams::Utf8(params_start as usize)) + } + ParamSource::One(params_start, a) => { + Source::Dynamic(lower_ascii_with_params(s, params_start as usize, &[a])) + } + ParamSource::Two(params_start, a, b) => { + Source::Dynamic(lower_ascii_with_params(s, params_start as usize, &[a, b])) + } + ParamSource::Custom(params_start, ref indices) => { + Source::Dynamic(lower_ascii_with_params(s, params_start as usize, indices)) + } }; Ok(Mime { @@ -165,8 +170,11 @@ pub(crate) fn parse(opts: &Parser, src: impl Parse) -> Result }) } - -fn params_from_str(s: &str, iter: &mut impl Iterator, mut start: usize) -> Result { +fn params_from_str( + s: &str, + iter: &mut impl Iterator, + mut start: usize, +) -> Result { let params_start = as_u16(start); start += 1; let mut params = ParamSource::None; @@ -179,23 +187,25 @@ fn params_from_str(s: &str, iter: &mut impl Iterator, mut star Some((i, b' ')) if i == start => { start = i + 1; continue 'params; - }, + } // empty param Some((i, b';')) if i == start => { start = i + 1; continue 'params; - }, + } Some((_, c)) if is_token(c) => (), Some((i, b'=')) if i > start => { name = (as_u16(start), as_u16(i)); start = i + 1; break 'name; - }, + } None => return Err(ParseError::MissingEqual), - Some((pos, byte)) => return Err(ParseError::InvalidToken { - pos: pos, - byte: Byte(byte), - }), + Some((pos, byte)) => { + return Err(ParseError::InvalidToken { + pos: pos, + byte: Byte(byte), + }) + } } } @@ -210,27 +220,30 @@ fn params_from_str(s: &str, iter: &mut impl Iterator, mut star is_quoted_pair = false; match iter.next() { Some((_, ch)) if is_restricted_quoted_char(ch) => (), - Some((pos, byte)) => return Err(ParseError::InvalidToken { - pos: pos, - byte: Byte(byte), - }), + Some((pos, byte)) => { + return Err(ParseError::InvalidToken { + pos: pos, + byte: Byte(byte), + }) + } None => return Err(ParseError::MissingQuote), } - } else { match iter.next() { Some((i, b'"')) if i > start => { value = (as_u16(start), as_u16(i + 1)); start = i + 1; break 'value; - }, + } Some((_, b'\\')) => is_quoted_pair = true, Some((_, c)) if is_restricted_quoted_char(c) => (), None => return Err(ParseError::MissingQuote), - Some((pos, byte)) => return Err(ParseError::InvalidToken { - pos: pos, - byte: Byte(byte), - }), + Some((pos, byte)) => { + return Err(ParseError::InvalidToken { + pos: pos, + byte: Byte(byte), + }) + } } } } else { @@ -238,10 +251,9 @@ fn params_from_str(s: &str, iter: &mut impl Iterator, mut star Some((i, b'"')) if i == start => { is_quoted = true; start = i; - }, + } Some((_, c)) if is_token(c) => (), - Some((i, b' ')) | - Some((i, b';')) if i > start => { + Some((i, b' ')) | Some((i, b';')) if i > start => { value = (as_u16(start), as_u16(i)); start = i + 1; break 'value; @@ -250,12 +262,14 @@ fn params_from_str(s: &str, iter: &mut impl Iterator, mut star value = (as_u16(start), as_u16(s.len())); start = s.len(); break 'value; - }, + } - Some((pos, byte)) => return Err(ParseError::InvalidToken { - pos: pos, - byte: Byte(byte), - }), + Some((pos, byte)) => { + return Err(ParseError::InvalidToken { + pos: pos, + byte: Byte(byte), + }) + } } } } @@ -266,25 +280,26 @@ fn params_from_str(s: &str, iter: &mut impl Iterator, mut star let charset = (i, "charset".len() as u16 + i); let utf8 = (charset.1 + 1, charset.1 + "utf-8".len() as u16 + 1); params = ParamSource::Two(params_start, (charset, utf8), (name, value)); - }, + } ParamSource::One(sc, a) => { params = ParamSource::Two(sc, a, (name, value)); - }, + } ParamSource::Two(sc, a, b) => { params = ParamSource::Custom(sc, vec![a, b, (name, value)]); - }, + } ParamSource::Custom(_, ref mut vec) => { vec.push((name, value)); - }, + } ParamSource::None => { - if params_start + 2 == name.0 && - "charset".eq_ignore_ascii_case(&s[range(name)]) && - "utf-8".eq_ignore_ascii_case(&s[range(value)]) { + if params_start + 2 == name.0 + && "charset".eq_ignore_ascii_case(&s[range(name)]) + && "utf-8".eq_ignore_ascii_case(&s[range(value)]) + { params = ParamSource::Utf8(params_start); continue 'params; } params = ParamSource::One(params_start, (name, value)); - }, + } } } Ok(params) @@ -296,22 +311,14 @@ macro_rules! byte_map { } static TOKEN_MAP: [bool; 256] = byte_map![ - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, - 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ]; fn is_token(c: u8) -> bool { @@ -333,26 +340,30 @@ mod tests { for (i, &valid) in super::TOKEN_MAP.iter().enumerate() { let i = i as u8; let should = match i { - b'a'..=b'z' | - b'A'..=b'Z' | - b'0'..=b'9' | - b'!' | - b'#' | - b'$' | - b'%' | - b'&' | - b'\'' | - b'+' | - b'-' | - b'.' | - b'^' | - b'_' | - b'`' | - b'|' | - b'~' => true, - _ => false + b'a'..=b'z' + | b'A'..=b'Z' + | b'0'..=b'9' + | b'!' + | b'#' + | b'$' + | b'%' + | b'&' + | b'\'' + | b'+' + | b'-' + | b'.' + | b'^' + | b'_' + | b'`' + | b'|' + | b'~' => true, + _ => false, }; - assert_eq!(valid, should, "{:?} ({}) should be {}", i as char, i, should); + assert_eq!( + valid, should, + "{:?} ({}) should be {}", + i as char, i, should + ); } } @@ -478,7 +489,6 @@ mod tests { parse("te xt/plain").unwrap_err(); } - #[test] fn error_type_lf() { parse("te\nxt/plain").unwrap_err(); diff --git a/src/cmp.rs b/src/cmp.rs index 17b1b5d4..6439d328 100644 --- a/src/cmp.rs +++ b/src/cmp.rs @@ -4,9 +4,7 @@ pub(crate) fn str_eq(mime: &Mime, s: &str) -> bool { if mime.has_params() { Parser::can_range() .parse(s) - .map(|other_mime| { - mime_eq(mime, &other_mime) - }) + .map(|other_mime| mime_eq(mime, &other_mime)) .unwrap_or(false) } else { mime.as_ref().eq_ignore_ascii_case(s) @@ -17,12 +15,9 @@ pub(crate) fn mime_eq(a: &Mime, b: &Mime) -> bool { match (a.private_atom(), b.private_atom()) { // If either atom is 0, it is "dynamic" and needs to be compared // slowly... - (0, _) | (_, 0) => { - essence_eq(a, b) && params_eq(a, b) - }, + (0, _) | (_, 0) => essence_eq(a, b) && params_eq(a, b), (aa, ba) => aa == ba, } - } fn essence_eq(a: &Mime, b: &Mime) -> bool { diff --git a/src/constants.rs b/src/constants.rs index af4376ec..ef503995 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -1,4 +1,3 @@ - macro_rules! mimes { ($(@ $kind:ident: $($id:ident, $src:expr;)+)+) => ( pub(super) mod mimes { @@ -31,35 +30,35 @@ macro_rules! mimes { } macro_rules! mime_constant { - ($kind:ident, $id:ident, $src:expr) => ( + ($kind:ident, $id:ident, $src:expr) => { mime_constant! { @DOC concat!("A `", stringify!($kind), "` representing `\"", $src, "\"`."), $kind, $id, $src } - ); - (@DOC $doc:expr, $kind:ident, $id:ident, $src:expr) => ( + }; + (@DOC $doc:expr, $kind:ident, $id:ident, $src:expr) => { #[doc = $doc] pub const $id: $kind = $kind { mime: mime_parse::constants::$id, }; - ) + }; } #[cfg(test)] macro_rules! mime_constant_test { - ($id:ident, $src:expr) => ({ + ($id:ident, $src:expr) => {{ let __mime = $id; // prevent ranges from being MediaTypes __mime.test_assert_asterisks(); - }) + }}; } #[cfg(test)] macro_rules! mime_constant_proc_macro_test { - (@MediaType, $id:ident, $src:expr) => ( + (@MediaType, $id:ident, $src:expr) => { // Test proc macro matches constants #[cfg(feature = "macro")] { @@ -71,8 +70,8 @@ macro_rules! mime_constant_proc_macro_test { assert_ne!(macroed.mime.private_atom(), 0); assert_eq!(constant.mime.private_atom(), macroed.mime.private_atom()); } - ); - (@MediaRange, $id:ident, $src:expr) => (); + }; + (@MediaRange, $id:ident, $src:expr) => {}; } mimes! { @@ -118,4 +117,3 @@ mimes! { VIDEO_STAR, "video/*"; AUDIO_STAR, "audio/*"; } - diff --git a/src/error.rs b/src/error.rs index e06d29ad..c265eaab 100644 --- a/src/error.rs +++ b/src/error.rs @@ -9,8 +9,7 @@ pub struct InvalidMime { pub(crate) inner: ParseError, } -impl Error for InvalidMime { -} +impl Error for InvalidMime {} impl fmt::Display for InvalidMime { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { diff --git a/src/lib.rs b/src/lib.rs index d0753965..5e154114 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -53,7 +53,7 @@ //! ## Using Media Ranges for matching //! //! [`MediaRange`]s are often used by agents to declare a "range" of media -//! types that they can understand. A common place to find these is `Accept` +//! types that they can understand. A common place to find there is `Accept` //! HTTP header, perhaps like this: //! //! ```http @@ -106,12 +106,12 @@ use proc_macro_hack::proc_macro_hack; #[proc_macro_hack] pub use mime_macro::media_type; -pub use mime_parse::constants::names::*; pub use self::constants::mimes::*; pub use self::error::InvalidMime; pub use self::range::MediaRange; pub use self::type_::MediaType; pub use self::value::{Value, UTF_8}; +pub use mime_parse::constants::names::*; mod cmp; mod constants; @@ -124,7 +124,6 @@ mod serde; mod type_; mod value; - fn _assert_traits() { fn assert_send_sync() {} diff --git a/src/macros.rs b/src/macros.rs index 0e851b48..6d4ddb1b 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -52,12 +52,8 @@ impl MediaType { /// ``` #[doc(hidden)] #[cfg(feature = "macro")] - pub const unsafe fn private_from_proc_macro( - mime: crate::private::Mime, - ) -> Self { - MediaType { - mime, - } + pub const unsafe fn private_from_proc_macro(mime: crate::private::Mime) -> Self { + MediaType { mime } } } @@ -127,4 +123,3 @@ mod tests { assert_eq!(mt.to_string(), "multipart/form-data; boundary=AbCd"); } } - diff --git a/src/range.rs b/src/range.rs index abec1ed8..0f0f6f05 100644 --- a/src/range.rs +++ b/src/range.rs @@ -173,7 +173,7 @@ impl MediaRange { /// assert_eq!(range.param("charset").unwrap(), "utf-8"); /// assert_eq!(range.param("boundary"), None); /// ``` - pub fn param<'a>(&'a self, attr: &str) -> Option> { + pub fn param(&self, attr: &str) -> Option { crate::value::param(&self.mime, attr) } @@ -235,9 +235,7 @@ impl MediaRange { /// ``` impl From for MediaRange { fn from(mt: MediaType) -> MediaRange { - MediaRange { - mime: mt.mime, - } + MediaRange { mime: mt.mime } } } @@ -255,12 +253,12 @@ impl PartialEq for MediaRange { impl<'a> PartialEq<&'a str> for MediaRange { #[inline] - fn eq(&self, s: & &'a str) -> bool { + fn eq(&self, s: &&'a str) -> bool { self == *s } } -impl<'a> PartialEq for &'a str { +impl PartialEq for &str { #[inline] fn eq(&self, mr: &MediaRange) -> bool { mr == self @@ -309,14 +307,20 @@ mod tests { #[test] fn media_range_from_str() { // exact types - assert_eq!(MediaRange::parse("text/plain").unwrap(), MediaRange::from(TEXT_PLAIN)); + assert_eq!( + MediaRange::parse("text/plain").unwrap(), + MediaRange::from(TEXT_PLAIN) + ); // stars let any = "*/*".parse::().unwrap(); assert_eq!(any, "*/*"); assert_eq!(any, STAR_STAR); assert_eq!("image/*".parse::().unwrap(), "image/*"); - assert_eq!("text/*; charset=utf-8".parse::().unwrap(), "text/*; charset=utf-8"); + assert_eq!( + "text/*; charset=utf-8".parse::().unwrap(), + "text/*; charset=utf-8" + ); // bad stars MediaRange::parse("text/*plain").unwrap_err(); @@ -328,9 +332,15 @@ mod tests { assert!(TEXT_STAR.matches(&TEXT_PLAIN), "text/* matches text/plain"); assert!(TEXT_STAR.matches(&TEXT_HTML), "text/* matches text/html"); - assert!(TEXT_STAR.matches(&TEXT_HTML_UTF_8), "text/* matches text/html; charset=utf-8"); - - assert!(!TEXT_STAR.matches(&IMAGE_GIF), "text/* doesn't match image/gif"); + assert!( + TEXT_STAR.matches(&TEXT_HTML_UTF_8), + "text/* matches text/html; charset=utf-8" + ); + + assert!( + !TEXT_STAR.matches(&IMAGE_GIF), + "text/* doesn't match image/gif" + ); } #[test] @@ -363,4 +373,3 @@ mod tests { assert!(!range.matches(&TEXT_HTML)); } } - diff --git a/src/serde.rs b/src/serde.rs index e536f356..73f30cb8 100644 --- a/src/serde.rs +++ b/src/serde.rs @@ -3,10 +3,10 @@ use std::fmt; use serde1::de::{self, Deserialize, Deserializer}; use serde1::ser::{Serialize, Serializer}; -use super::{MediaType, MediaRange}; +use super::{MediaRange, MediaType}; macro_rules! serde_impl { - ($ty:ident) => ( + ($ty:ident) => { impl Serialize for $ty { fn serialize(&self, serializer: S) -> Result where @@ -41,7 +41,7 @@ macro_rules! serde_impl { deserializer.deserialize_str(Visitor) } } - ) + }; } serde_impl!(MediaType); diff --git a/src/type_.rs b/src/type_.rs index cfa027e2..e13ae8c2 100644 --- a/src/type_.rs +++ b/src/type_.rs @@ -137,11 +137,10 @@ impl MediaType { /// let mime = "multipart/form-data; boundary=ABCDEFG".parse::().unwrap(); /// assert_eq!(mime.param(mime::BOUNDARY).unwrap(), "ABCDEFG"); /// ``` - pub fn param<'a>(&'a self, attr: &str) -> Option> { + pub fn param(&self, attr: &str) -> Option { crate::value::param(&self.mime, attr) } - /// Returns an iterator over the parameters. /// /// # Example @@ -207,7 +206,11 @@ impl MediaType { #[cfg(test)] pub(super) fn test_assert_asterisks(&self) { - assert!(!self.as_ref().contains('*'), "{:?} contains an asterisk", self); + assert!( + !self.as_ref().contains('*'), + "{:?} contains an asterisk", + self + ); } } @@ -225,12 +228,12 @@ impl PartialEq for MediaType { impl<'a> PartialEq<&'a str> for MediaType { #[inline] - fn eq(&self, s: & &'a str) -> bool { + fn eq(&self, s: &&'a str) -> bool { self == *s } } -impl<'a> PartialEq for &'a str { +impl PartialEq for &str { #[inline] fn eq(&self, mt: &MediaType) -> bool { mt == self @@ -279,13 +282,13 @@ mod tests { #[test] fn test_size_of() { assert!( - std::mem::size_of::() < 100, + size_of::() < 100, "just to be warned if the size grows suddenly" ); assert_eq!( - std::mem::size_of::(), - std::mem::size_of::>(), + size_of::(), + size_of::>(), "option size optimization" ); } @@ -295,7 +298,6 @@ mod tests { assert_eq!(TEXT_PLAIN.type_(), TEXT); } - #[test] fn test_subtype() { assert_eq!(TEXT_PLAIN.subtype(), PLAIN); @@ -331,8 +333,14 @@ mod tests { fn test_media_type_from_str() { assert_eq!(MediaType::parse("text/plain").unwrap(), TEXT_PLAIN); assert_eq!(MediaType::parse("TEXT/PLAIN").unwrap(), TEXT_PLAIN); - assert_eq!(MediaType::parse("text/plain; charset=utf-8").unwrap(), TEXT_PLAIN_UTF_8); - assert_eq!(MediaType::parse("text/plain;charset=\"utf-8\"").unwrap(), TEXT_PLAIN_UTF_8); + assert_eq!( + MediaType::parse("text/plain; charset=utf-8").unwrap(), + TEXT_PLAIN_UTF_8 + ); + assert_eq!( + MediaType::parse("text/plain;charset=\"utf-8\"").unwrap(), + TEXT_PLAIN_UTF_8 + ); // quotes + semi colon MediaType::parse("text/plain;charset=\"utf-8\"; foo=bar").unwrap(); @@ -343,7 +351,6 @@ mod tests { assert_eq!(upper.type_(), TEXT); assert_eq!(upper.subtype(), PLAIN); - let extended = MediaType::parse("TEXT/PLAIN; CHARSET=UTF-8; FOO=BAR").unwrap(); assert_eq!(extended, "text/plain; charset=utf-8; foo=BAR"); assert_eq!(extended.param("charset").unwrap(), "utf-8"); @@ -364,10 +371,9 @@ mod tests { MediaType::parse("text/plain; charset=\"\r\nutf-8\"").unwrap_err(); } - #[test] fn test_from_str_empty_parameter_list() { - static CASES: &'static [&'static str] = &[ + static CASES: &[&str] = &[ "text/event-stream;", "text/event-stream; ", "text/event-stream; ", @@ -384,7 +390,7 @@ mod tests { #[test] fn test_parse_too_long() { - let mut source = vec![b'a'; ::std::u16::MAX as usize]; + let mut source = vec![b'a'; u16::MAX as usize]; source[5] = b'/'; let mut s = String::from_utf8(source).unwrap(); @@ -397,7 +403,8 @@ mod tests { #[test] fn test_case_sensitive_values() { - let mime = MediaType::parse("multipart/form-data; charset=BASE64; boundary=ABCDEFG").unwrap(); + let mime = + MediaType::parse("multipart/form-data; charset=BASE64; boundary=ABCDEFG").unwrap(); assert_eq!(mime.param(CHARSET).unwrap(), "bAsE64"); assert_eq!(mime.param(BOUNDARY).unwrap(), "ABCDEFG"); assert_eq!(mime.param(BOUNDARY).unwrap().as_str_repr(), "ABCDEFG"); @@ -417,7 +424,6 @@ mod tests { assert_eq!(mime.param("foo").unwrap(), "bar"); assert_eq!(mime.param("baz"), None); - let mime = MediaType::parse("text/plain;charset=\"utf-8\"").unwrap(); assert_eq!(mime.param(CHARSET), Some(UTF_8)); } @@ -448,13 +454,13 @@ mod tests { #[test] fn test_has_params() { let mime = TEXT_PLAIN; - assert_eq!(mime.has_params(), false); + assert!(!mime.has_params()); let mime = MediaType::parse("text/plain; charset=utf-8").unwrap(); - assert_eq!(mime.has_params(), true); + assert!(mime.has_params()); let mime = MediaType::parse("text/plain; charset=utf-8; foo=bar").unwrap(); - assert_eq!(mime.has_params(), true); + assert!(mime.has_params()); } #[test] @@ -531,4 +537,3 @@ mod tests { assert_ne!(mime1, mime2); } } - diff --git a/src/value.rs b/src/value.rs index d1e4b2c6..137acd5d 100644 --- a/src/value.rs +++ b/src/value.rs @@ -1,9 +1,8 @@ +use mime_parse::Mime; +use quoted_string::{AsciiCaseInsensitiveEq, ContentChars}; +use std::borrow::Cow; use std::cmp::PartialEq; use std::fmt; -use std::borrow::Cow; - -use mime_parse::Mime; -use quoted_string::{self, ContentChars, AsciiCaseInsensitiveEq}; /// a `Value` usable for a charset parameter. /// @@ -55,7 +54,7 @@ impl<'a> Value<'a> { /// /// The underlying representation differs from the content, /// as it can contain quotes surrounding the content and - /// quoted-pairs, even if non of them are necessary to + /// quoted-pairs, even if none of them are necessary to /// represent the content. /// /// For example the representation `r#""a\"\ b""#` corresponds @@ -84,7 +83,7 @@ impl<'a> Value<'a> { /// /// If the underlying representation is a quoted-string without /// quoted-pairs `Cow::Borrowed` is returned as normal - /// str slicing can be used to strip the surrounding double quoted. + /// str slicing can be used to strip the surrounding double-quoted. /// /// If the underlying representation is not a quoted-string /// `Cow::Borrowed` is returned, too. @@ -109,10 +108,9 @@ impl<'a> Value<'a> { pub fn to_content(&self) -> Cow<'a, str> { quoted_string::unquote_unchecked(self.source) } - } -impl<'a, 'b> PartialEq> for Value<'a> { +impl<'b> PartialEq> for Value<'_> { #[inline] fn eq(&self, other: &Value<'b>) -> bool { let left_content_chars = ContentChars::from_string_unchecked(self.source); @@ -126,7 +124,7 @@ impl<'a, 'b> PartialEq> for Value<'a> { } } -impl<'a> PartialEq for Value<'a> { +impl PartialEq for Value<'_> { fn eq(&self, other: &str) -> bool { if self.source.starts_with('"') { let content_chars = ContentChars::from_string_unchecked(self.source); @@ -143,15 +141,14 @@ impl<'a> PartialEq for Value<'a> { } } -impl<'a, 'b> PartialEq<&'b str> for Value<'a> { +impl<'b> PartialEq<&'b str> for Value<'_> { #[inline] - fn eq(&self, other: & &'b str) -> bool { + fn eq(&self, other: &&'b str) -> bool { self == *other } } - -impl<'a, 'b> PartialEq> for &'a str { +impl<'b> PartialEq> for &str { #[inline] fn eq(&self, other: &Value<'b>) -> bool { other == self @@ -172,14 +169,14 @@ impl<'a> From> for Cow<'a, str> { } } -impl<'a> fmt::Debug for Value<'a> { +impl fmt::Debug for Value<'_> { #[inline] fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Debug::fmt(self.source, f) } } -impl<'a> fmt::Display for Value<'a> { +impl fmt::Display for Value<'_> { #[inline] fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Display::fmt(self.source, f) @@ -194,12 +191,12 @@ mod test { use super::Value; - fn bidi_eq, B: Debug+PartialEq>(left: A, right: B) { + fn bidi_eq, B: Debug + PartialEq>(left: A, right: B) { assert_eq!(left, right); assert_eq!(right, left); } - fn bidi_ne, B: Debug+PartialEq>(left: A, right: B) { + fn bidi_ne, B: Debug + PartialEq>(left: A, right: B) { assert_ne!(left, right); assert_ne!(right, left); } @@ -208,15 +205,15 @@ mod test { fn test_value_eq_str() { let value = Value { source: "abc", - ascii_case_insensitive: false + ascii_case_insensitive: false, }; let value_quoted = Value { source: "\"abc\"", - ascii_case_insensitive: false + ascii_case_insensitive: false, }; let value_quoted_with_esacpes = Value { source: "\"a\\bc\"", - ascii_case_insensitive: false + ascii_case_insensitive: false, }; bidi_eq(value, "abc"); @@ -231,7 +228,6 @@ mod test { bidi_ne(value_quoted_with_esacpes, "\"abc\""); bidi_ne(value_quoted_with_esacpes, "\"a\\bc\""); - assert_ne!(value, "aBc"); assert_ne!(value_quoted, "aBc"); assert_ne!(value_quoted_with_esacpes, "aBc"); @@ -241,18 +237,18 @@ mod test { fn test_value_eq_str_ascii_case_insensitive() { let value = Value { source: "abc", - ascii_case_insensitive: true + ascii_case_insensitive: true, }; let value_quoted = Value { source: "\"abc\"", - ascii_case_insensitive: true + ascii_case_insensitive: true, }; let value_quoted_with_esacpes = Value { source: "\"a\\bc\"", - ascii_case_insensitive: true + ascii_case_insensitive: true, }; - //1st. all case sensitive checks which still apply + //1st. all case-sensitive checks which still apply bidi_eq(value, "abc"); bidi_ne(value, "\"abc\""); bidi_ne(value, "\"a\\bc\""); @@ -265,8 +261,7 @@ mod test { bidi_ne(value_quoted_with_esacpes, "\"abc\""); bidi_ne(value_quoted_with_esacpes, "\"a\\bc\""); - - //2nd the case insensitive check + //2nd the case-insensitive check bidi_eq(value, "aBc"); bidi_ne(value, "\"aBc\""); bidi_ne(value, "\"a\\Bc\""); @@ -284,15 +279,15 @@ mod test { fn test_value_eq_value() { let value = Value { source: "abc", - ascii_case_insensitive: false + ascii_case_insensitive: false, }; let value_quoted = Value { source: "\"abc\"", - ascii_case_insensitive: false + ascii_case_insensitive: false, }; let value_quoted_with_esacpes = Value { source: "\"a\\bc\"", - ascii_case_insensitive: false + ascii_case_insensitive: false, }; assert_eq!(value, value); assert_eq!(value_quoted, value_quoted); @@ -307,15 +302,15 @@ mod test { fn test_value_eq_value_case_insensitive() { let value = Value { source: "Abc", - ascii_case_insensitive: true + ascii_case_insensitive: true, }; let value_quoted = Value { source: "\"aBc\"", - ascii_case_insensitive: true + ascii_case_insensitive: true, }; let value_quoted_with_esacpes = Value { source: "\"a\\bC\"", - ascii_case_insensitive: true + ascii_case_insensitive: true, }; assert_eq!(value, value); assert_eq!(value_quoted, value_quoted); @@ -330,21 +325,21 @@ mod test { fn test_value_eq_value_mixed_case_sensitivity() { let value = Value { source: "Abc", - ascii_case_insensitive: true + ascii_case_insensitive: true, }; let value_quoted = Value { source: "\"aBc\"", - ascii_case_insensitive: false + ascii_case_insensitive: false, }; let value_quoted_with_esacpes = Value { source: "\"a\\bC\"", - ascii_case_insensitive: false + ascii_case_insensitive: false, }; bidi_eq(value, value_quoted); bidi_eq(value, value_quoted_with_esacpes); - //both are ascii case insensitive + //both are ascii case-insensitive bidi_ne(value_quoted, value_quoted_with_esacpes); } @@ -374,5 +369,4 @@ mod test { let expected: Cow<'static, str> = Cow::Owned("ab\"cd".into()); assert_eq!(value.to_content(), expected); } - }