rename entitytag::weak+strong

This commit is contained in:
Rob Ede 2022-01-03 18:22:20 +00:00
parent e935e979ab
commit d019c6e76c
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
7 changed files with 64 additions and 49 deletions

View File

@ -14,6 +14,7 @@ jobs:
uses: actions-rs/toolchain@v1 uses: actions-rs/toolchain@v1
with: with:
toolchain: stable toolchain: stable
profile: minimal
components: rustfmt components: rustfmt
- name: Check with rustfmt - name: Check with rustfmt
uses: actions-rs/cargo@v1 uses: actions-rs/cargo@v1
@ -30,6 +31,7 @@ jobs:
uses: actions-rs/toolchain@v1 uses: actions-rs/toolchain@v1
with: with:
toolchain: stable toolchain: stable
profile: minimal
components: clippy components: clippy
override: true override: true
@ -38,7 +40,7 @@ jobs:
with: { command: generate-lockfile } with: { command: generate-lockfile }
- name: Cache Dependencies - name: Cache Dependencies
uses: Swatinem/rust-cache@v1.2.0 uses: Swatinem/rust-cache@v1.2.0
- name: Check with Clippy - name: Check with Clippy
uses: actions-rs/clippy-check@v1 uses: actions-rs/clippy-check@v1
with: with:

View File

@ -381,7 +381,7 @@ impl NamedFile {
.duration_since(UNIX_EPOCH) .duration_since(UNIX_EPOCH)
.expect("modification time must be after epoch"); .expect("modification time must be after epoch");
header::EntityTag::strong(format!( header::EntityTag::new_strong(format!(
"{:x}:{:x}:{:x}:{:x}", "{:x}:{:x}:{:x}:{:x}",
ino, ino,
self.md.len(), self.md.len(),

View File

@ -13,6 +13,7 @@
- `QualityItem::min` semantics changed with `QualityItem::MIN`. [#2501] - `QualityItem::min` semantics changed with `QualityItem::MIN`. [#2501]
- Rename `ContentEncoding::{Br => Brotli}`. [#2501] - Rename `ContentEncoding::{Br => Brotli}`. [#2501]
- Minimum supported Rust version (MSRV) is now 1.54. - Minimum supported Rust version (MSRV) is now 1.54.
- Rename `header::EntityTag::{weak => new_weak, strong => new_strong}`. [#2565]
### Fixed ### Fixed
- `ContentEncoding::Identity` can now be parsed from a string. [#2501] - `ContentEncoding::Identity` can now be parsed from a string. [#2501]
@ -23,6 +24,7 @@
- `ContentEncoding::is_compression()`. [#2501] - `ContentEncoding::is_compression()`. [#2501]
[#2501]: https://github.com/actix/actix-web/pull/2501 [#2501]: https://github.com/actix/actix-web/pull/2501
[#2565]: https://github.com/actix/actix-web/pull/2565
## 3.0.0-beta.17 - 2021-12-27 ## 3.0.0-beta.17 - 2021-12-27

View File

@ -59,7 +59,7 @@ pub struct EntityTag {
} }
impl EntityTag { impl EntityTag {
/// Constructs a new EntityTag. /// Constructs a new `EntityTag`.
/// ///
/// # Panics /// # Panics
/// If the tag contains invalid characters. /// If the tag contains invalid characters.
@ -72,51 +72,61 @@ impl EntityTag {
/// ///
/// # Panics /// # Panics
/// If the tag contains invalid characters. /// If the tag contains invalid characters.
pub fn weak(tag: String) -> EntityTag { pub fn new_weak(tag: String) -> EntityTag {
EntityTag::new(true, tag) EntityTag::new(true, tag)
} }
#[deprecated(since = "3.0.0", note = "Renamed to `new_weak`.")]
pub fn weak(tag: String) -> EntityTag {
Self::new_weak(tag)
}
/// Constructs a new strong EntityTag. /// Constructs a new strong EntityTag.
/// ///
/// # Panics /// # Panics
/// If the tag contains invalid characters. /// If the tag contains invalid characters.
pub fn strong(tag: String) -> EntityTag { pub fn new_strong(tag: String) -> EntityTag {
EntityTag::new(false, tag) EntityTag::new(false, tag)
} }
/// Get the tag. #[deprecated(since = "3.0.0", note = "Renamed to `new_strong`.")]
pub fn strong(tag: String) -> EntityTag {
Self::new_strong(tag)
}
/// Returns tag.
pub fn tag(&self) -> &str { pub fn tag(&self) -> &str {
self.tag.as_ref() self.tag.as_ref()
} }
/// Set the tag. /// Sets tag.
/// ///
/// # Panics /// # Panics
/// If the tag contains invalid characters. /// If the tag contains invalid characters.
pub fn set_tag(&mut self, tag: String) { pub fn set_tag(&mut self, tag: impl Into<String>) {
let tag = tag.into();
assert!(check_slice_validity(&tag), "Invalid tag: {:?}", tag); assert!(check_slice_validity(&tag), "Invalid tag: {:?}", tag);
self.tag = tag self.tag = tag
} }
/// For strong comparison two entity-tags are equivalent if both are not /// For strong comparison two entity-tags are equivalent if both are not weak and their
/// weak and their opaque-tags match character-by-character. /// opaque-tags match character-by-character.
pub fn strong_eq(&self, other: &EntityTag) -> bool { pub fn strong_eq(&self, other: &EntityTag) -> bool {
!self.weak && !other.weak && self.tag == other.tag !self.weak && !other.weak && self.tag == other.tag
} }
/// For weak comparison two entity-tags are equivalent if their /// For weak comparison two entity-tags are equivalent if their opaque-tags match
/// opaque-tags match character-by-character, regardless of either or /// character-by-character, regardless of either or both being tagged as "weak".
/// both being tagged as "weak".
pub fn weak_eq(&self, other: &EntityTag) -> bool { pub fn weak_eq(&self, other: &EntityTag) -> bool {
self.tag == other.tag self.tag == other.tag
} }
/// The inverse of `EntityTag.strong_eq()`. /// Returns the inverse of `strong_eq()`.
pub fn strong_ne(&self, other: &EntityTag) -> bool { pub fn strong_ne(&self, other: &EntityTag) -> bool {
!self.strong_eq(other) !self.strong_eq(other)
} }
/// The inverse of `EntityTag.weak_eq()`. /// Returns inverse of `weak_eq()`.
pub fn weak_ne(&self, other: &EntityTag) -> bool { pub fn weak_ne(&self, other: &EntityTag) -> bool {
!self.weak_eq(other) !self.weak_eq(other)
} }
@ -184,23 +194,23 @@ mod tests {
// Expected success // Expected success
assert_eq!( assert_eq!(
"\"foobar\"".parse::<EntityTag>().unwrap(), "\"foobar\"".parse::<EntityTag>().unwrap(),
EntityTag::strong("foobar".to_owned()) EntityTag::new_strong("foobar".to_owned())
); );
assert_eq!( assert_eq!(
"\"\"".parse::<EntityTag>().unwrap(), "\"\"".parse::<EntityTag>().unwrap(),
EntityTag::strong("".to_owned()) EntityTag::new_strong("".to_owned())
); );
assert_eq!( assert_eq!(
"W/\"weaktag\"".parse::<EntityTag>().unwrap(), "W/\"weaktag\"".parse::<EntityTag>().unwrap(),
EntityTag::weak("weaktag".to_owned()) EntityTag::new_weak("weaktag".to_owned())
); );
assert_eq!( assert_eq!(
"W/\"\x65\x62\"".parse::<EntityTag>().unwrap(), "W/\"\x65\x62\"".parse::<EntityTag>().unwrap(),
EntityTag::weak("\x65\x62".to_owned()) EntityTag::new_weak("\x65\x62".to_owned())
); );
assert_eq!( assert_eq!(
"W/\"\"".parse::<EntityTag>().unwrap(), "W/\"\"".parse::<EntityTag>().unwrap(),
EntityTag::weak("".to_owned()) EntityTag::new_weak("".to_owned())
); );
} }
@ -220,19 +230,19 @@ mod tests {
#[test] #[test]
fn test_etag_fmt() { fn test_etag_fmt() {
assert_eq!( assert_eq!(
format!("{}", EntityTag::strong("foobar".to_owned())), format!("{}", EntityTag::new_strong("foobar".to_owned())),
"\"foobar\"" "\"foobar\""
); );
assert_eq!(format!("{}", EntityTag::strong("".to_owned())), "\"\""); assert_eq!(format!("{}", EntityTag::new_strong("".to_owned())), "\"\"");
assert_eq!( assert_eq!(
format!("{}", EntityTag::weak("weak-etag".to_owned())), format!("{}", EntityTag::new_weak("weak-etag".to_owned())),
"W/\"weak-etag\"" "W/\"weak-etag\""
); );
assert_eq!( assert_eq!(
format!("{}", EntityTag::weak("\u{0065}".to_owned())), format!("{}", EntityTag::new_weak("\u{0065}".to_owned())),
"W/\"\x65\"" "W/\"\x65\""
); );
assert_eq!(format!("{}", EntityTag::weak("".to_owned())), "W/\"\""); assert_eq!(format!("{}", EntityTag::new_weak("".to_owned())), "W/\"\"");
} }
#[test] #[test]
@ -243,29 +253,29 @@ mod tests {
// | `W/"1"` | `W/"2"` | no match | no match | // | `W/"1"` | `W/"2"` | no match | no match |
// | `W/"1"` | `"1"` | no match | match | // | `W/"1"` | `"1"` | no match | match |
// | `"1"` | `"1"` | match | match | // | `"1"` | `"1"` | match | match |
let mut etag1 = EntityTag::weak("1".to_owned()); let mut etag1 = EntityTag::new_weak("1".to_owned());
let mut etag2 = EntityTag::weak("1".to_owned()); let mut etag2 = EntityTag::new_weak("1".to_owned());
assert!(!etag1.strong_eq(&etag2)); assert!(!etag1.strong_eq(&etag2));
assert!(etag1.weak_eq(&etag2)); assert!(etag1.weak_eq(&etag2));
assert!(etag1.strong_ne(&etag2)); assert!(etag1.strong_ne(&etag2));
assert!(!etag1.weak_ne(&etag2)); assert!(!etag1.weak_ne(&etag2));
etag1 = EntityTag::weak("1".to_owned()); etag1 = EntityTag::new_weak("1".to_owned());
etag2 = EntityTag::weak("2".to_owned()); etag2 = EntityTag::new_weak("2".to_owned());
assert!(!etag1.strong_eq(&etag2)); assert!(!etag1.strong_eq(&etag2));
assert!(!etag1.weak_eq(&etag2)); assert!(!etag1.weak_eq(&etag2));
assert!(etag1.strong_ne(&etag2)); assert!(etag1.strong_ne(&etag2));
assert!(etag1.weak_ne(&etag2)); assert!(etag1.weak_ne(&etag2));
etag1 = EntityTag::weak("1".to_owned()); etag1 = EntityTag::new_weak("1".to_owned());
etag2 = EntityTag::strong("1".to_owned()); etag2 = EntityTag::new_strong("1".to_owned());
assert!(!etag1.strong_eq(&etag2)); assert!(!etag1.strong_eq(&etag2));
assert!(etag1.weak_eq(&etag2)); assert!(etag1.weak_eq(&etag2));
assert!(etag1.strong_ne(&etag2)); assert!(etag1.strong_ne(&etag2));
assert!(!etag1.weak_ne(&etag2)); assert!(!etag1.weak_ne(&etag2));
etag1 = EntityTag::strong("1".to_owned()); etag1 = EntityTag::new_strong("1".to_owned());
etag2 = EntityTag::strong("1".to_owned()); etag2 = EntityTag::new_strong("1".to_owned());
assert!(etag1.strong_eq(&etag2)); assert!(etag1.strong_eq(&etag2));
assert!(etag1.weak_eq(&etag2)); assert!(etag1.weak_eq(&etag2));
assert!(!etag1.strong_ne(&etag2)); assert!(!etag1.strong_ne(&etag2));

View File

@ -31,7 +31,7 @@ crate::http::header::common_header! {
/// ///
/// let mut builder = HttpResponse::Ok(); /// let mut builder = HttpResponse::Ok();
/// builder.insert_header( /// builder.insert_header(
/// ETag(EntityTag::new(false, "xyzzy".to_owned())) /// ETag(EntityTag::new_strong("xyzzy".to_owned()))
/// ); /// );
/// ``` /// ```
/// ///
@ -41,7 +41,7 @@ crate::http::header::common_header! {
/// ///
/// let mut builder = HttpResponse::Ok(); /// let mut builder = HttpResponse::Ok();
/// builder.insert_header( /// builder.insert_header(
/// ETag(EntityTag::new(true, "xyzzy".to_owned())) /// ETag(EntityTag::new_weak("xyzzy".to_owned()))
/// ); /// );
/// ``` /// ```
(ETag, ETAG) => [EntityTag] (ETag, ETAG) => [EntityTag]
@ -50,29 +50,29 @@ crate::http::header::common_header! {
// From the RFC // From the RFC
crate::http::header::common_header_test!(test1, crate::http::header::common_header_test!(test1,
vec![b"\"xyzzy\""], vec![b"\"xyzzy\""],
Some(ETag(EntityTag::new(false, "xyzzy".to_owned())))); Some(ETag(EntityTag::new_strong("xyzzy".to_owned()))));
crate::http::header::common_header_test!(test2, crate::http::header::common_header_test!(test2,
vec![b"W/\"xyzzy\""], vec![b"W/\"xyzzy\""],
Some(ETag(EntityTag::new(true, "xyzzy".to_owned())))); Some(ETag(EntityTag::new_weak("xyzzy".to_owned()))));
crate::http::header::common_header_test!(test3, crate::http::header::common_header_test!(test3,
vec![b"\"\""], vec![b"\"\""],
Some(ETag(EntityTag::new(false, "".to_owned())))); Some(ETag(EntityTag::new_strong("".to_owned()))));
// Own tests // Own tests
crate::http::header::common_header_test!(test4, crate::http::header::common_header_test!(test4,
vec![b"\"foobar\""], vec![b"\"foobar\""],
Some(ETag(EntityTag::new(false, "foobar".to_owned())))); Some(ETag(EntityTag::new_strong("foobar".to_owned()))));
crate::http::header::common_header_test!(test5, crate::http::header::common_header_test!(test5,
vec![b"\"\""], vec![b"\"\""],
Some(ETag(EntityTag::new(false, "".to_owned())))); Some(ETag(EntityTag::new_strong("".to_owned()))));
crate::http::header::common_header_test!(test6, crate::http::header::common_header_test!(test6,
vec![b"W/\"weak-etag\""], vec![b"W/\"weak-etag\""],
Some(ETag(EntityTag::new(true, "weak-etag".to_owned())))); Some(ETag(EntityTag::new_weak("weak-etag".to_owned()))));
crate::http::header::common_header_test!(test7, crate::http::header::common_header_test!(test7,
vec![b"W/\"\x65\x62\""], vec![b"W/\"\x65\x62\""],
Some(ETag(EntityTag::new(true, "\u{0065}\u{0062}".to_owned())))); Some(ETag(EntityTag::new_weak("\u{0065}\u{0062}".to_owned()))));
crate::http::header::common_header_test!(test8, crate::http::header::common_header_test!(test8,
vec![b"W/\"\""], vec![b"W/\"\""],
Some(ETag(EntityTag::new(true, "".to_owned())))); Some(ETag(EntityTag::new_weak("".to_owned()))));
crate::http::header::common_header_test!(test9, crate::http::header::common_header_test!(test9,
vec![b"no-dquotes"], vec![b"no-dquotes"],
None::<ETag>); None::<ETag>);

View File

@ -54,14 +54,15 @@ common_header! {
test1, test1,
vec![b"\"xyzzy\""], vec![b"\"xyzzy\""],
Some(HeaderField::Items( Some(HeaderField::Items(
vec![EntityTag::new(false, "xyzzy".to_owned())]))); vec![EntityTag::new_strong("xyzzy".to_owned())])));
crate::http::header::common_header_test!( crate::http::header::common_header_test!(
test2, test2,
vec![b"\"xyzzy\", \"r2d2xxxx\", \"c3piozzzz\""], vec![b"\"xyzzy\", \"r2d2xxxx\", \"c3piozzzz\""],
Some(HeaderField::Items( Some(HeaderField::Items(
vec![EntityTag::new(false, "xyzzy".to_owned()), vec![EntityTag::new_strong("xyzzy".to_owned()),
EntityTag::new(false, "r2d2xxxx".to_owned()), EntityTag::new_strong("r2d2xxxx".to_owned()),
EntityTag::new(false, "c3piozzzz".to_owned())]))); EntityTag::new_strong("c3piozzzz".to_owned())])));
crate::http::header::common_header_test!(test3, vec![b"*"], Some(IfMatch::Any)); crate::http::header::common_header_test!(test3, vec![b"*"], Some(IfMatch::Any));
} }
} }

View File

@ -82,8 +82,8 @@ mod tests {
if_none_match = Header::parse(&req); if_none_match = Header::parse(&req);
let mut entities: Vec<EntityTag> = Vec::new(); let mut entities: Vec<EntityTag> = Vec::new();
let foobar_etag = EntityTag::new(false, "foobar".to_owned()); let foobar_etag = EntityTag::new_strong("foobar".to_owned());
let weak_etag = EntityTag::new(true, "weak-etag".to_owned()); let weak_etag = EntityTag::new_weak("weak-etag".to_owned());
entities.push(foobar_etag); entities.push(foobar_etag);
entities.push(weak_etag); entities.push(weak_etag);
assert_eq!(if_none_match.ok(), Some(IfNoneMatch::Items(entities))); assert_eq!(if_none_match.ok(), Some(IfNoneMatch::Items(entities)));