From fdb0594a820bd9c49a714153dba7a1a2cdf042a4 Mon Sep 17 00:00:00 2001 From: MrAntix Date: Tue, 30 Jan 2024 17:27:07 +0000 Subject: [PATCH] fix(build): revert unnecessary format changes --- .vscode/settings.json | 3 + src/builder.rs | 18 ++-- src/lib.rs | 225 +++++++++++++++--------------------------- 3 files changed, 90 insertions(+), 156 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..d3def913 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "editor.formatOnSave": false +} diff --git a/src/builder.rs b/src/builder.rs index daf445ba..c7c7d887 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -1,6 +1,6 @@ -use std::borrow::{Borrow, BorrowMut, Cow}; +use std::borrow::{Cow, Borrow, BorrowMut}; -use crate::{Cookie, Expiration, SameSite}; +use crate::{Cookie, SameSite, Expiration}; /// Structure that follows the builder pattern for building `Cookie` structs. /// @@ -59,13 +59,10 @@ impl<'c> CookieBuilder<'c> { /// assert_eq!(c.name_value(), ("foo", "bar")); /// ``` pub fn new(name: N, value: V) -> Self - where - N: Into>, - V: Into>, + where N: Into>, + V: Into> { - CookieBuilder { - cookie: Cookie::new(name, value), - } + CookieBuilder { cookie: Cookie::new(name, value) } } /// Sets the `value` field in the cookie being built. @@ -371,10 +368,7 @@ impl<'c> CookieBuilder<'c> { /// Instead of using this method, pass a `CookieBuilder` directly into /// methods expecting a `T: Into`. For other cases, use /// [`CookieBuilder::build()`]. - #[deprecated( - since = "0.18.0", - note = "`CookieBuilder` can be passed in to methods expecting a `Cookie`; for other cases, use `CookieBuilder::build()`" - )] + #[deprecated(since="0.18.0", note="`CookieBuilder` can be passed in to methods expecting a `Cookie`; for other cases, use `CookieBuilder::build()`")] pub fn finish(self) -> Cookie<'c> { self.cookie } diff --git a/src/lib.rs b/src/lib.rs index f3e4f1a3..f666d2c3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -69,16 +69,17 @@ //! ``` #![cfg_attr(all(nightly, doc), feature(doc_cfg))] + #![deny(missing_docs)] pub use time; mod builder; -mod delta; -mod expiration; -mod jar; mod parse; +mod jar; +mod delta; mod same_site; +mod expiration; /// Implementation of [HTTP RFC6265 draft] cookie prefixes. /// @@ -86,11 +87,8 @@ mod same_site; /// https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis#name-cookie-name-prefixes pub mod prefix; -#[cfg(any(feature = "private", feature = "signed"))] -#[macro_use] -mod secure; -#[cfg(any(feature = "private", feature = "signed"))] -pub use secure::*; +#[cfg(any(feature = "private", feature = "signed"))] #[macro_use] mod secure; +#[cfg(any(feature = "private", feature = "signed"))] pub use secure::*; use std::borrow::Cow; use std::fmt; @@ -99,14 +97,14 @@ use std::str::FromStr; #[allow(unused_imports, deprecated)] use std::ascii::AsciiExt; -use time::{macros::datetime, Duration, OffsetDateTime, UtcOffset}; +use time::{Duration, OffsetDateTime, UtcOffset, macros::datetime}; -pub use crate::builder::CookieBuilder; -pub use crate::expiration::*; -pub use crate::jar::{CookieJar, Delta, Iter}; use crate::parse::parse_cookie; pub use crate::parse::ParseError; +pub use crate::builder::CookieBuilder; +pub use crate::jar::{CookieJar, Delta, Iter}; pub use crate::same_site::*; +pub use crate::expiration::*; #[derive(Debug, Clone)] enum CookieStr<'c> { @@ -149,12 +147,10 @@ impl<'c> CookieStr<'c> { fn to_str<'s>(&'s self, string: Option<&'s Cow>) -> &'s str { match *self { CookieStr::Indexed(i, j) => { - let s = string.expect( - "`Some` base string must exist when \ - converting indexed str to str! (This is a module invariant.)", - ); + let s = string.expect("`Some` base string must exist when \ + converting indexed str to str! (This is a module invariant.)"); &s[i..j] - } + }, CookieStr::Concrete(ref cstr) => &*cstr, } } @@ -162,9 +158,11 @@ impl<'c> CookieStr<'c> { #[allow(clippy::ptr_arg)] fn to_raw_str<'s, 'b: 's>(&'s self, string: &'s Cow<'b, str>) -> Option<&'b str> { match *self { - CookieStr::Indexed(i, j) => match *string { - Cow::Borrowed(s) => Some(&s[i..j]), - Cow::Owned(_) => None, + CookieStr::Indexed(i, j) => { + match *string { + Cow::Borrowed(s) => Some(&s[i..j]), + Cow::Owned(_) => None, + } }, CookieStr::Concrete(_) => None, } @@ -256,9 +254,8 @@ impl<'c> Cookie<'c> { /// assert_eq!(cookie.name_value(), ("name", "value")); /// ``` pub fn new(name: N, value: V) -> Self - where - N: Into>, - V: Into>, + where N: Into>, + V: Into> { Cookie { cookie_string: None, @@ -291,13 +288,9 @@ impl<'c> Cookie<'c> { /// assert_eq!(cookie.name(), "name"); /// assert!(cookie.value().is_empty()); /// ``` - #[deprecated( - since = "0.18.0", - note = "use `Cookie::build(name)` or `Cookie::from(name)`" - )] + #[deprecated(since = "0.18.0", note = "use `Cookie::build(name)` or `Cookie::from(name)`")] pub fn named(name: N) -> Cookie<'c> - where - N: Into>, + where N: Into> { Cookie::new(name, "") } @@ -349,8 +342,7 @@ impl<'c> Cookie<'c> { /// assert_eq!(c.secure(), None); /// ``` pub fn parse(s: S) -> Result, ParseError> - where - S: Into>, + where S: Into> { parse_cookie(s.into(), false) } @@ -372,8 +364,7 @@ impl<'c> Cookie<'c> { #[cfg(feature = "percent-encode")] #[cfg_attr(all(nightly, doc), doc(cfg(feature = "percent-encode")))] pub fn parse_encoded(s: S) -> Result, ParseError> - where - S: Into>, + where S: Into> { parse_cookie(s.into(), true) } @@ -408,8 +399,7 @@ impl<'c> Cookie<'c> { /// ``` #[inline(always)] pub fn split_parse(string: S) -> SplitCookies<'c> - where - S: Into>, + where S: Into> { SplitCookies { string: string.into(), @@ -450,8 +440,7 @@ impl<'c> Cookie<'c> { #[cfg_attr(all(nightly, doc), doc(cfg(feature = "percent-encode")))] #[inline(always)] pub fn split_parse_encoded(string: S) -> SplitCookies<'c> - where - S: Into>, + where S: Into> { SplitCookies { string: string.into(), @@ -564,7 +553,7 @@ impl<'c> Cookie<'c> { let bytes = s.as_bytes(); match (bytes.first(), bytes.last()) { (Some(b'"'), Some(b'"')) => &s[1..(s.len() - 1)], - _ => s, + _ => s } } @@ -790,7 +779,7 @@ impl<'c> Cookie<'c> { Some(ref c) => { let domain = c.to_str(self.cookie_string.as_ref()); domain.strip_prefix(".").or(Some(domain)) - } + }, None => None, } } @@ -1129,7 +1118,8 @@ impl<'c> Cookie<'c> { static MAX_DATETIME: OffsetDateTime = datetime!(9999-12-31 23:59:59.999_999 UTC); // RFC 6265 requires dates not to exceed 9999 years. - self.expires = Some(time.into().map(|time| std::cmp::min(time, MAX_DATETIME))); + self.expires = Some(time.into() + .map(|time| std::cmp::min(time, MAX_DATETIME))); } /// Unsets the `expires` of `self`. @@ -1239,11 +1229,7 @@ impl<'c> Cookie<'c> { if let Some(time) = self.expires_datetime() { let time = time.to_offset(UtcOffset::UTC); - write!( - f, - "; Expires={}", - time.format(&crate::parse::FMT1).map_err(|_| fmt::Error)? - )?; + write!(f, "; Expires={}", time.format(&crate::parse::FMT1).map_err(|_| fmt::Error)?)?; } Ok(()) @@ -1275,8 +1261,7 @@ impl<'c> Cookie<'c> { /// ``` #[inline] pub fn name_raw(&self) -> Option<&'c str> { - self.cookie_string - .as_ref() + self.cookie_string.as_ref() .and_then(|s| self.name.to_raw_str(s)) } @@ -1306,8 +1291,7 @@ impl<'c> Cookie<'c> { /// ``` #[inline] pub fn value_raw(&self) -> Option<&'c str> { - self.cookie_string - .as_ref() + self.cookie_string.as_ref() .and_then(|s| self.value.to_raw_str(s)) } @@ -1380,7 +1364,7 @@ impl<'c> Cookie<'c> { (Some(domain), Some(string)) => match domain.to_raw_str(string) { Some(s) => s.strip_prefix(".").or(Some(s)), None => None, - }, + } _ => None, } } @@ -1422,13 +1406,10 @@ impl<'c> Cookie<'c> { /// /// let mut c = Cookie::build(("key?", "value")).secure(true).path("/").build(); /// assert_eq!(&c.stripped().to_string(), "key?=value"); - #[cfg_attr( - feature = "percent-encode", - doc = r##" + #[cfg_attr(feature = "percent-encode", doc = r##" // Note: `encoded()` is only available when `percent-encode` is enabled. assert_eq!(&c.stripped().encoded().to_string(), "key%3F=value"); - #"## - )] + #"##)] /// ``` #[inline(always)] pub fn stripped<'a>(&'a self) -> Display<'a, 'c> { @@ -1467,7 +1448,7 @@ impl<'c> Iterator for SplitCookies<'c> { return Some(match self.string { Cow::Borrowed(s) => parse_cookie(s[i..j].trim(), self.decode), Cow::Owned(ref s) => parse_cookie(s[i..j].trim().to_owned(), self.decode), - }); + }) } None @@ -1479,10 +1460,19 @@ mod encoding { use percent_encoding::{AsciiSet, CONTROLS}; /// https://url.spec.whatwg.org/#fragment-percent-encode-set - const FRAGMENT: &AsciiSet = &CONTROLS.add(b' ').add(b'"').add(b'<').add(b'>').add(b'`'); + const FRAGMENT: &AsciiSet = &CONTROLS + .add(b' ') + .add(b'"') + .add(b'<') + .add(b'>') + .add(b'`'); /// https://url.spec.whatwg.org/#path-percent-encode-set - const PATH: &AsciiSet = &FRAGMENT.add(b'#').add(b'?').add(b'{').add(b'}'); + const PATH: &AsciiSet = &FRAGMENT + .add(b'#') + .add(b'?') + .add(b'{') + .add(b'}'); /// https://url.spec.whatwg.org/#userinfo-percent-encode-set const USERINFO: &AsciiSet = &PATH @@ -1499,7 +1489,10 @@ mod encoding { .add(b'%'); /// https://www.rfc-editor.org/rfc/rfc6265#section-4.1.1 + '(', ')' - const COOKIE: &AsciiSet = &USERINFO.add(b'(').add(b')').add(b','); + const COOKIE: &AsciiSet = &USERINFO + .add(b'(') + .add(b')') + .add(b','); /// Percent-encode a cookie name or value with the proper encoding set. pub fn encode(string: &str) -> impl std::fmt::Display + '_ { @@ -1522,15 +1515,12 @@ mod encoding { /// /// let c = Cookie::build(("my name", "this; value%?")).secure(true).build(); /// assert_eq!(&c.stripped().to_string(), "my name=this; value%?"); -#[cfg_attr( - feature = "percent-encode", - doc = r##" +#[cfg_attr(feature = "percent-encode", doc = r##" // Note: `encoded()` is only available when `percent-encode` is enabled. assert_eq!(&c.encoded().to_string(), "my%20name=this%3B%20value%25%3F; Secure"); assert_eq!(&c.stripped().encoded().to_string(), "my%20name=this%3B%20value%25%3F"); assert_eq!(&c.encoded().stripped().to_string(), "my%20name=this%3B%20value%25%3F"); -"## -)] +"##)] /// ``` pub struct Display<'a, 'c: 'a> { cookie: &'a Cookie<'c>, @@ -1541,8 +1531,7 @@ pub struct Display<'a, 'c: 'a> { impl<'a, 'c: 'a> fmt::Display for Display<'a, 'c> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - #[cfg(feature = "percent-encode")] - { + #[cfg(feature = "percent-encode")] { if self.encode { let name = encoding::encode(self.cookie.name()); let value = encoding::encode(self.cookie.value()); @@ -1552,14 +1541,13 @@ impl<'a, 'c: 'a> fmt::Display for Display<'a, 'c> { } } - #[cfg(not(feature = "percent-encode"))] - { + #[cfg(not(feature = "percent-encode"))] { write!(f, "{}={}", self.cookie.name(), self.cookie.value())?; } match self.strip { true => Ok(()), - false => self.cookie.fmt_parameters(f), + false => self.cookie.fmt_parameters(f) } } } @@ -1567,20 +1555,11 @@ impl<'a, 'c: 'a> fmt::Display for Display<'a, 'c> { impl<'a, 'c> Display<'a, 'c> { #[cfg(feature = "percent-encode")] fn new_encoded(cookie: &'a Cookie<'c>) -> Self { - Display { - cookie, - strip: false, - encode: true, - } + Display { cookie, strip: false, encode: true } } fn new_stripped(cookie: &'a Cookie<'c>) -> Self { - Display { - cookie, - strip: true, - #[cfg(feature = "percent-encode")] - encode: false, - } + Display { cookie, strip: true, #[cfg(feature = "percent-encode")] encode: false } } /// Percent-encode the name and value pair. @@ -1677,9 +1656,8 @@ impl<'a> From> for Cookie<'a> { } impl<'a, N, V> From<(N, V)> for Cookie<'a> -where - N: Into>, - V: Into>, + where N: Into>, + V: Into> { fn from((name, value): (N, V)) -> Self { Cookie::new(name, value) @@ -1706,7 +1684,7 @@ impl<'a> AsMut> for Cookie<'a> { #[cfg(test)] mod tests { - use crate::{parse::parse_date, Cookie, SameSite}; + use crate::{Cookie, SameSite, parse::parse_date}; use time::{Duration, OffsetDateTime}; #[test] @@ -1714,9 +1692,6 @@ mod tests { let cookie = Cookie::new("foo", "bar"); assert_eq!(&cookie.to_string(), "foo=bar"); - let cookie = Cookie::build("foo").value("bar"); - assert_eq!(&cookie.to_string(), "foo=bar"); - let cookie = Cookie::build(("foo", "bar")).http_only(true); assert_eq!(&cookie.to_string(), "foo=bar; HttpOnly"); @@ -1741,10 +1716,8 @@ mod tests { let time_str = "Wed, 21 Oct 2015 07:28:00 GMT"; let expires = parse_date(time_str, &crate::parse::FMT1).unwrap(); let cookie = Cookie::build(("foo", "bar")).expires(expires); - assert_eq!( - &cookie.to_string(), - "foo=bar; Expires=Wed, 21 Oct 2015 07:28:00 GMT" - ); + assert_eq!(&cookie.to_string(), + "foo=bar; Expires=Wed, 21 Oct 2015 07:28:00 GMT"); let cookie = Cookie::build(("foo", "bar")).same_site(SameSite::Strict); assert_eq!(&cookie.to_string(), "foo=bar; SameSite=Strict"); @@ -1752,16 +1725,11 @@ mod tests { let cookie = Cookie::build(("foo", "bar")).same_site(SameSite::Lax); assert_eq!(&cookie.to_string(), "foo=bar; SameSite=Lax"); - let mut cookie = Cookie::build(("foo", "bar")) - .same_site(SameSite::None) - .build(); + let mut cookie = Cookie::build(("foo", "bar")).same_site(SameSite::None).build(); assert_eq!(&cookie.to_string(), "foo=bar; SameSite=None; Secure"); cookie.set_partitioned(true); - assert_eq!( - &cookie.to_string(), - "foo=bar; SameSite=None; Partitioned; Secure" - ); + assert_eq!(&cookie.to_string(), "foo=bar; SameSite=None; Partitioned; Secure"); cookie.set_same_site(None); assert_eq!(&cookie.to_string(), "foo=bar; Partitioned; Secure"); @@ -1775,10 +1743,7 @@ mod tests { cookie.set_partitioned(None); assert_eq!(&cookie.to_string(), "foo=bar"); - let mut c = Cookie::build(("foo", "bar")) - .same_site(SameSite::None) - .secure(false) - .build(); + let mut c = Cookie::build(("foo", "bar")).same_site(SameSite::None).secure(false).build(); assert_eq!(&c.to_string(), "foo=bar; SameSite=None"); c.set_secure(true); assert_eq!(&c.to_string(), "foo=bar; SameSite=None; Secure"); @@ -1789,17 +1754,11 @@ mod tests { fn format_date_wraps() { let expires = OffsetDateTime::UNIX_EPOCH + Duration::MAX; let cookie = Cookie::build(("foo", "bar")).expires(expires); - assert_eq!( - &cookie.to_string(), - "foo=bar; Expires=Fri, 31 Dec 9999 23:59:59 GMT" - ); + assert_eq!(&cookie.to_string(), "foo=bar; Expires=Fri, 31 Dec 9999 23:59:59 GMT"); let expires = time::macros::datetime!(9999-01-01 0:00 UTC) + Duration::days(1000); let cookie = Cookie::build(("foo", "bar")).expires(expires); - assert_eq!( - &cookie.to_string(), - "foo=bar; Expires=Fri, 31 Dec 9999 23:59:59 GMT" - ); + assert_eq!(&cookie.to_string(), "foo=bar; Expires=Fri, 31 Dec 9999 23:59:59 GMT"); } #[test] @@ -1869,34 +1828,13 @@ mod tests { ("a=d#$%^&*()_", vec![("a", "d#$%^&*()_")]), (" name=value ", vec![("name", "value")]), ("name=value ", vec![("name", "value")]), - ( - "name=value;;other=key", - vec![("name", "value"), ("other", "key")], - ), - ( - "name=value; ;other=key", - vec![("name", "value"), ("other", "key")], - ), - ( - "name=value ; ;other=key", - vec![("name", "value"), ("other", "key")], - ), - ( - "name=value ; ; other=key", - vec![("name", "value"), ("other", "key")], - ), - ( - "name=value ; ; other=key ", - vec![("name", "value"), ("other", "key")], - ), - ( - "name=value ; ; other=key;; ", - vec![("name", "value"), ("other", "key")], - ), - ( - ";name=value ; ; other=key ", - vec![("name", "value"), ("other", "key")], - ), + ("name=value;;other=key", vec![("name", "value"), ("other", "key")]), + ("name=value; ;other=key", vec![("name", "value"), ("other", "key")]), + ("name=value ; ;other=key", vec![("name", "value"), ("other", "key")]), + ("name=value ; ; other=key", vec![("name", "value"), ("other", "key")]), + ("name=value ; ; other=key ", vec![("name", "value"), ("other", "key")]), + ("name=value ; ; other=key;; ", vec![("name", "value"), ("other", "key")]), + (";name=value ; ; other=key ", vec![("name", "value"), ("other", "key")]), (";a=1 ; ; b=2 ", vec![("a", "1"), ("b", "2")]), (";a=1 ; ; b= ", vec![("a", "1"), ("b", "")]), (";a=1 ; ; =v ; c=", vec![("a", "1"), ("c", "")]), @@ -1921,13 +1859,10 @@ mod tests { ("", vec![]), (";;", vec![]), ("name=val%20ue", vec![("name", "val ue")]), - ( - "foo%20!%25%3F%3D=bar%3B%3B%2C%20a", - vec![("foo !%?=", "bar;;, a")], - ), + ("foo%20!%25%3F%3D=bar%3B%3B%2C%20a", vec![("foo !%?=", "bar;;, a")]), ( "name=val%20ue ; ; foo%20!%25%3F%3D=bar%3B%3B%2C%20a", - vec![("name", "val ue"), ("foo !%?=", "bar;;, a")], + vec![("name", "val ue"), ("foo !%?=", "bar;;, a")] ), ]; @@ -1936,7 +1871,9 @@ mod tests { .filter_map(|parse| parse.ok()) .collect(); - let actual: Vec<_> = cookies.iter().map(|c| c.name_value()).collect(); + let actual: Vec<_> = cookies.iter() + .map(|c| c.name_value()) + .collect(); assert_eq!(expected, actual); }