From 6336a1048f558585b651e04b2dd3a23049b3fbed Mon Sep 17 00:00:00 2001 From: Bruce Mitchener Date: Sat, 14 Oct 2023 22:30:27 +0700 Subject: [PATCH] Switch from `language_tags` to `icu_locid`. --- Cargo.toml | 2 +- src/boolean.rs | 30 +++++++++++++++++------------- src/lib.rs | 4 ++-- src/parser.rs | 31 ++++++++++++++++++------------- 4 files changed, 38 insertions(+), 29 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 86df64e..687dd0b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,4 +12,4 @@ repository = "https://github.com/endoli/humanize.rs" edition = "2021" [dependencies] -language-tags = "0.2.2" +icu_locid = "1" diff --git a/src/boolean.rs b/src/boolean.rs index 3639d36..1c67f7f 100644 --- a/src/boolean.rs +++ b/src/boolean.rs @@ -8,7 +8,7 @@ //! //! ## Parsing //! -//! For all languages, we support parsing `"1"` and `"0"` as `true` +//! For all locales, we support parsing `"1"` and `"0"` as `true` //! and `false` respectively. //! //! In English, these lexical values map to `true`: @@ -28,16 +28,20 @@ //! * `"off"` use crate::Parse; -use language_tags::{langtag, LanguageTag}; +use icu_locid::{locale, Locale}; + +fn locale_matches(left: &Locale, right: &Locale) -> bool { + (*left == *right) || (*left == Locale::UND) || (*right == Locale::UND) +} impl Parse for bool { - fn parse(text: &str, language: &LanguageTag) -> Option { - let en = langtag!(en); + fn parse(text: &str, locale: &Locale) -> Option { + let en = locale!("en"); match &*text.to_lowercase() { "1" => Some(true), "0" => Some(false), - "ok" | "okay" | "on" | "true" | "yep" | "yes" if language.matches(&en) => Some(true), - "false" | "no" | "nope" | "off" if language.matches(&en) => Some(false), + "ok" | "okay" | "on" | "true" | "yep" | "yes" if locale_matches(locale, &en) => Some(true), + "false" | "no" | "nope" | "off" if locale_matches(locale, &en) => Some(false), _ => None, } } @@ -45,8 +49,8 @@ impl Parse for bool { #[cfg(test)] mod tests { - use crate::{parse, parse_with_language}; - use language_tags::langtag; + use crate::{parse, parse_with_locale}; + use icu_locid::locale; #[test] fn basic() { @@ -64,10 +68,10 @@ mod tests { assert_eq!(Some(false), parse::("nope")); assert_eq!(Some(false), parse::("off")); - let badlang = langtag!("no"); - assert_eq!(Some(true), parse_with_language::("1", &badlang)); - assert_eq!(Some(false), parse_with_language::("0", &badlang)); - assert_eq!(None, parse_with_language::("okay", &badlang)); - assert_eq!(None, parse_with_language::("nope", &badlang)); + let bad_locale = locale!("no"); + assert_eq!(Some(true), parse_with_locale::("1", &bad_locale)); + assert_eq!(Some(false), parse_with_locale::("0", &bad_locale)); + assert_eq!(None, parse_with_locale::("okay", &bad_locale)); + assert_eq!(None, parse_with_locale::("nope", &bad_locale)); } } diff --git a/src/lib.rs b/src/lib.rs index 6a78618..40b7dbf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,7 +22,7 @@ //! * The ['at' command]'s input parsing. //! //! Contributions extending our functionality are welcome, as are -//! contributions that add support for additional languages. +//! contributions that add support for additional languages / locales. //! //! # Human-friendly Parsing //! @@ -56,4 +56,4 @@ pub mod boolean; mod parser; -pub use crate::parser::{parse, parse_or, parse_with_language, parse_with_language_or, Parse}; +pub use crate::parser::{parse, parse_or, parse_with_locale, parse_with_locale_or, Parse}; diff --git a/src/parser.rs b/src/parser.rs index 67609ef..e10838f 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -4,38 +4,43 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use language_tags::LanguageTag; +use icu_locid::Locale; /// Construct `Self` by parsing humanized text. pub trait Parse: Sized { /// Perform the conversion. - fn parse(text: &str, language: &LanguageTag) -> Option; + fn parse(text: &str, locale: &Locale) -> Option; } /// Construct a value by parsing humanized text. /// -/// This uses a wild card for the language, so text in any language +/// This uses a wild card for the locale, so text in any locale /// supported by the library should work. pub fn parse(text: &str) -> Option { - let language = LanguageTag::default(); - T::parse(text, &language) + let locale = Locale::default(); + T::parse(text, &locale) } -/// Construct a value by parsing humanized text, with a default +/// Construct a value by parsing humanized `text`, with a `default` /// value when parsing fails. /// -/// This uses a wild card for the language, so text in any language +/// This uses a wild card for the locale, so text in any locale /// supported by the library should work. pub fn parse_or(text: &str, default: T) -> T { parse::(text).unwrap_or(default) } -/// Construct a value by parsing humanized text using the specified language. -pub fn parse_with_language(text: &str, language: &LanguageTag) -> Option { - T::parse(text, language) +/// Construct a value by parsing humanized `text` using the specified [`locale`]. +/// +/// [`locale`]: Locale +pub fn parse_with_locale(text: &str, locale: &Locale) -> Option { + T::parse(text, locale) } -/// Construct a value by parsing humanized text using the specified language. -pub fn parse_with_language_or(text: &str, language: &LanguageTag, default: T) -> T { - T::parse(text, language).unwrap_or(default) +/// Construct a value by parsing humanized `text` using the specified [`locale`], +/// with a `default` value when parsing fails. +/// +/// [`locale`]: Locale +pub fn parse_with_locale_or(text: &str, locale: &Locale, default: T) -> T { + T::parse(text, locale).unwrap_or(default) }