From a6df909a8d8018ef87b66b2128c6acfa9fb0599c Mon Sep 17 00:00:00 2001 From: pan93412 Date: Wed, 26 Apr 2023 17:30:03 +0800 Subject: Allow passing `region` to LocalName (#926) --- src/doc.rs | 26 ++++++++++++++++++++++++++ src/util/mod.rs | 10 ++++++++++ 2 files changed, 36 insertions(+) (limited to 'src') diff --git a/src/doc.rs b/src/doc.rs index b7b1eacd..5f62506a 100644 --- a/src/doc.rs +++ b/src/doc.rs @@ -591,6 +591,12 @@ impl Region { } } +impl PartialEq<&str> for Region { + fn eq(&self, other: &&str) -> bool { + self.as_str() == *other + } +} + impl FromStr for Region { type Err = &'static str; @@ -688,3 +694,23 @@ cast_to_value! { "y" => Value::Length(v.point.y.into()), }) } + +#[cfg(test)] +mod tests { + use crate::{doc::Region, util::option_eq}; + + #[test] + fn test_partialeq_str() { + let region = Region([b'U', b'S']); + assert_eq!(region, "US"); + assert_ne!(region, "AB"); + } + + #[test] + fn test_region_option_eq() { + let region = Some(Region([b'U', b'S'])); + + assert!(option_eq(region, "US")); + assert!(!option_eq(region, "AB")); + } +} diff --git a/src/util/mod.rs b/src/util/mod.rs index 592ba1fa..83ec5961 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -226,3 +226,13 @@ pub fn pretty_array_like(parts: &[impl AsRef], trailing_comma: bool) -> Str buf.push(')'); buf } + +/// Check if the [`Option`]-wrapped L is same to R. +/// +/// This is the stable version of [`Option::contains`]. +pub fn option_eq(left: Option, other: R) -> bool +where + L: PartialEq, +{ + left.map(|v| v == other).unwrap_or(false) +} -- cgit v1.2.3