From 9c702e315c88d14b80cc1694a4b8b70a69010f96 Mon Sep 17 00:00:00 2001 From: Robert Bastian Date: Mon, 21 Feb 2022 21:50:37 +0100 Subject: [PATCH 01/13] macro_rules langid --- components/locid/src/extensions/mod.rs | 6 +- components/locid/src/extensions/other/mod.rs | 4 +- .../locid/src/extensions/private/mod.rs | 5 +- .../locid/src/extensions/transform/mod.rs | 7 +- .../locid/src/extensions/unicode/mod.rs | 5 +- components/locid/src/langid.rs | 21 +- components/locid/src/lib.rs | 1 + components/locid/src/locale.rs | 2 +- components/locid/src/macros.rs | 239 ++++++++++++++++++ components/locid/src/parser/langid.rs | 95 ++++++- components/locid/src/parser/locale.rs | 2 +- components/locid/src/parser/mod.rs | 74 +++++- components/locid/src/subtags/language.rs | 27 +- components/locid/src/subtags/region.rs | 34 +-- components/locid/src/subtags/script.rs | 20 +- components/locid/src/subtags/variant.rs | 23 +- utils/tinystr/src/ascii.rs | 4 + 17 files changed, 514 insertions(+), 55 deletions(-) create mode 100644 components/locid/src/macros.rs diff --git a/components/locid/src/extensions/mod.rs b/components/locid/src/extensions/mod.rs index 5a8da86aac6..0272ab56b7a 100644 --- a/components/locid/src/extensions/mod.rs +++ b/components/locid/src/extensions/mod.rs @@ -59,7 +59,7 @@ use alloc::vec::Vec; use core::iter::Peekable; use crate::parser::ParserError; - +use crate::parser::SubtagIterator; /// Defines the type of extension. #[derive(Debug, PartialEq, Eq, Clone, Hash, PartialOrd, Ord, Copy)] pub enum ExtensionType { @@ -136,9 +136,7 @@ impl Extensions { && self.other.is_empty() } - pub(crate) fn try_from_iter<'a>( - iter: &mut Peekable>, - ) -> Result { + pub(crate) fn try_from_iter<'a>(iter: &mut SubtagIterator<'a>) -> Result { let mut unicode = None; let mut transform = None; let mut private = None; diff --git a/components/locid/src/extensions/other/mod.rs b/components/locid/src/extensions/other/mod.rs index 2c03ab0855b..99127297cbf 100644 --- a/components/locid/src/extensions/other/mod.rs +++ b/components/locid/src/extensions/other/mod.rs @@ -25,9 +25,9 @@ mod key; use alloc::boxed::Box; use crate::parser::ParserError; +use crate::parser::SubtagIterator; use alloc::vec::Vec; use core::iter::Peekable; - pub use key::Key; /// A list of [`Other Use Extensions`] as defined in [`Unicode Locale @@ -78,7 +78,7 @@ impl Other { pub(crate) fn try_from_iter<'a>( ext: u8, - iter: &mut Peekable>, + iter: &mut SubtagIterator<'a>, ) -> Result { debug_assert!(ext.is_ascii_alphabetic()); diff --git a/components/locid/src/extensions/private/mod.rs b/components/locid/src/extensions/private/mod.rs index 9332aad5f8d..ecab691735b 100644 --- a/components/locid/src/extensions/private/mod.rs +++ b/components/locid/src/extensions/private/mod.rs @@ -37,6 +37,7 @@ use core::ops::Deref; pub use key::Key; use crate::parser::ParserError; +use crate::parser::SubtagIterator; /// A list of [`Private Use Extensions`] as defined in [`Unicode Locale /// Identifier`] specification. @@ -124,9 +125,7 @@ impl Private { self.0 = None; } - pub(crate) fn try_from_iter<'a>( - iter: &mut impl Iterator, - ) -> Result { + pub(crate) fn try_from_iter<'a>(iter: &mut SubtagIterator<'a>) -> Result { let keys = iter.map(Key::from_bytes).collect::, _>>()?; Ok(Self::from_vec_unchecked(keys)) diff --git a/components/locid/src/extensions/transform/mod.rs b/components/locid/src/extensions/transform/mod.rs index 72dd295d38e..e4f0f8a6f1b 100644 --- a/components/locid/src/extensions/transform/mod.rs +++ b/components/locid/src/extensions/transform/mod.rs @@ -40,13 +40,12 @@ pub use fields::Fields; pub use key::Key; pub use value::Value; +use crate::parser::SubtagIterator; use crate::parser::{parse_language_identifier_from_iter, ParserError, ParserMode}; use crate::subtags::Language; use crate::LanguageIdentifier; use alloc::vec; -use core::iter::Peekable; - /// A list of [`Unicode BCP47 T Extensions`] as defined in [`Unicode Locale /// Identifier`] specification. /// @@ -118,9 +117,7 @@ impl Transform { self.lang.is_none() && self.fields.is_empty() } - pub(crate) fn try_from_iter<'a>( - iter: &mut Peekable>, - ) -> Result { + pub(crate) fn try_from_iter<'a>(iter: &mut SubtagIterator<'a>) -> Result { let mut tlang = None; let mut tfields = vec![]; diff --git a/components/locid/src/extensions/unicode/mod.rs b/components/locid/src/extensions/unicode/mod.rs index 89b0e00489d..90894bc4808 100644 --- a/components/locid/src/extensions/unicode/mod.rs +++ b/components/locid/src/extensions/unicode/mod.rs @@ -43,6 +43,7 @@ pub use keywords::Keywords; pub use value::Value; use crate::parser::ParserError; +use crate::parser::SubtagIterator; use core::iter::Peekable; @@ -114,9 +115,7 @@ impl Unicode { self.keywords.is_empty() && self.attributes.is_empty() } - pub(crate) fn try_from_iter<'a>( - iter: &mut Peekable>, - ) -> Result { + pub(crate) fn try_from_iter<'a>(iter: &mut SubtagIterator<'a>) -> Result { let mut attributes = vec![]; let mut keywords = vec![]; diff --git a/components/locid/src/langid.rs b/components/locid/src/langid.rs index dd274de79e1..b6315fd5fd5 100644 --- a/components/locid/src/langid.rs +++ b/components/locid/src/langid.rs @@ -4,7 +4,10 @@ use core::str::FromStr; -use crate::parser::{get_subtag_iterator, parse_language_identifier, ParserError, ParserMode}; +use crate::parser::{ + get_subtag_iterator, parse_language_identifier, parse_language_identifier_without_variants, + ParserError, ParserMode, +}; use crate::subtags; use alloc::string::String; use alloc::string::ToString; @@ -85,6 +88,22 @@ impl LanguageIdentifier { parse_language_identifier(v, ParserMode::LanguageIdentifier) } + #[doc(hidden)] + // We cannot return `Result`, as it's currently impossible to unwrap non-Copy + // types in a const context. See [rust-lang#73255](https://github.com/rust-lang/rust/issues/73255). + pub const fn from_bytes_without_variants( + v: &[u8], + ) -> Result< + ( + subtags::Language, + Option, + Option, + ), + ParserError, + > { + parse_language_identifier_without_variants(v, ParserMode::LanguageIdentifier) + } + /// A constructor which takes a utf8 slice which may contain extension keys, /// parses it and produces a well-formed [`LanguageIdentifier`]. /// diff --git a/components/locid/src/lib.rs b/components/locid/src/lib.rs index c2ac02a6261..d662a40b95a 100644 --- a/components/locid/src/lib.rs +++ b/components/locid/src/lib.rs @@ -61,6 +61,7 @@ mod helpers; pub mod extensions; mod langid; mod locale; +pub mod macros; mod parser; #[cfg(feature = "serde")] mod serde; diff --git a/components/locid/src/locale.rs b/components/locid/src/locale.rs index 2c118afb7ef..0415a095606 100644 --- a/components/locid/src/locale.rs +++ b/components/locid/src/locale.rs @@ -260,7 +260,7 @@ macro_rules! subtag_matches { impl PartialEq for Locale { fn eq(&self, other: &str) -> bool { - let mut iter = get_subtag_iterator(other.as_bytes()).peekable(); + let mut iter = get_subtag_iterator(other.as_bytes()); if !subtag_matches!(subtags::Language, iter, self.id.language) { return false; } diff --git a/components/locid/src/macros.rs b/components/locid/src/macros.rs new file mode 100644 index 00000000000..a21078ee235 --- /dev/null +++ b/components/locid/src/macros.rs @@ -0,0 +1,239 @@ +// This file is part of ICU4X. For terms of use, please see the file +// called LICENSE at the top level of the ICU4X source tree +// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). + +//! This module provides convenience macros for [`icu_locid`]. +//! +//! # Examples +//! +//! ```rust +//! use icu_locid::macros::{language, region, langid}; +//! +//! let lid = langid!("EN_US"); +//! +//! assert_eq!(lid.language, language!("en")); +//! assert_eq!(lid.region, Some(region!("US"))); +//! ``` + +/// A macro allowing for compile-time construction of valid [`Language`] subtag. +/// +/// The macro will perform syntax canonicalization of the tag. +/// +/// # Examples +/// +/// ``` +/// use icu::locid::subtags::Language; +/// use icu::locid::macros::language; +/// +/// const DE: Language = language!("DE"); +/// +/// let de: Language = "DE".parse() +/// .expect("Failed to parse language subtag."); +/// +/// assert_eq!(DE, "de"); +/// assert_eq!(DE, de); +/// ``` +/// +/// [`Language`]: icu_locid::subtags::Language +#[macro_export] +macro_rules! language { + ($language:literal) => {{ + const R: $crate::subtags::Language = + match $crate::subtags::Language::from_bytes($language.as_bytes()) { + Ok(r) => r, + _ => panic!(concat!("Invalid language code: ", $language)), + }; + R + }}; +} + +/// A macro allowing for compile-time construction of valid [`Script`] subtag. +/// +/// The macro will perform syntax canonicalization of the tag. +/// +/// # Examples +/// +/// ``` +/// use icu::locid::subtags::Script; +/// use icu::locid::macros::script; +/// +/// const ARAB: Script = script!("aRAB"); +/// +/// let arab: Script = "aRaB".parse() +/// .expect("Failed to parse script subtag."); +/// +/// assert_eq!(ARAB, "Arab"); +/// assert_eq!(ARAB, arab); +/// ``` +/// +/// [`Script`]: icu_locid::subtags::Script +#[macro_export] +macro_rules! script { + ($script:literal) => {{ + const R: $crate::subtags::Script = + match $crate::subtags::Script::from_bytes($script.as_bytes()) { + Ok(r) => r, + _ => panic!(concat!("Invalid script code: ", $script)), + }; + R + }}; +} + +/// A macro allowing for compile-time construction of valid [`Region`] subtag. +/// +/// The macro will perform syntax canonicalization of the tag. +/// +/// # Examples +/// +/// ``` +/// use icu::locid::subtags::Region; +/// use icu::locid::macros::region; +/// +/// const CN: Region = region!("cn"); +/// +/// let cn: Region = "cn".parse() +/// .expect("Failed to parse region subtag."); +/// +/// assert_eq!(CN, "CN"); +/// assert_eq!(CN, cn); +/// ``` +/// +/// [`Region`]: icu_locid::subtags::Region +#[macro_export] +macro_rules! region { + ($region:literal) => {{ + const R: $crate::subtags::Region = + match $crate::subtags::Region::from_bytes($region.as_bytes()) { + Ok(r) => r, + _ => panic!(concat!("Invalid region code: ", $region)), + }; + R + }}; +} + +/// A macro allowing for compile-time construction of valid [`Variant`] subtag. +/// +/// The macro will perform syntax canonicalization of the tag. +/// +/// # Examples +/// +/// ``` +/// use icu::locid::subtags::Variant; +/// use icu::locid::macros::variant; +/// +/// const POSIX: Variant = variant!("Posix"); +/// +/// let posix: Variant = "Posix".parse() +/// .expect("Failed to parse variant subtag."); +/// +/// assert_eq!(POSIX, "posix"); +/// assert_eq!(POSIX, posix); +/// ``` +/// +/// [`Variant`]: icu_locid::subtags::Variant +#[macro_export] +macro_rules! variant { + ($variant:literal) => {{ + const R: $crate::subtags::Variant = + match $crate::subtags::Variant::from_bytes($variant.as_bytes()) { + Ok(r) => r, + _ => panic!(concat!("Invalid variant code: ", $variant)), + }; + R + }}; +} + +/// A macro allowing for compile-time construction of valid [`LanguageIdentifier`]. +/// +/// The macro will perform syntax canonicalization of the tag. +/// +/// # Examples +/// +/// ``` +/// use icu::locid::LanguageIdentifier; +/// use icu::locid::macros::langid; +/// +/// const DE_AT: LanguageIdentifier = langid!("de_at"); +/// +/// let de_at: LanguageIdentifier = "de_at".parse() +/// .expect("Failed to parse language identifier."); +/// +/// assert_eq!(DE_AT, "de-AT"); +/// assert_eq!(DE_AT, de_at); +/// ``` +/// +/// *Note*: As of Rust 1.47, the macro cannot produce language identifier +/// with variants in the const mode pending [`Heap Allocations in Constants`]. +/// +/// [`LanguageIdentifier`]: icu_locid::LanguageIdentifier +/// [`Heap Allocations in Constants`]: https://github.com/rust-lang/const-eval/issues/20 +#[macro_export] +macro_rules! langid { + ($langid:literal) => {{ + const R: $crate::LanguageIdentifier = + match $crate::LanguageIdentifier::from_bytes_without_variants($langid.as_bytes()) { + Ok((language, script, region)) => $crate::LanguageIdentifier { + language, + script, + region, + variants: $crate::subtags::Variants::new(), + }, + Err(_) => panic!(concat!("Invalid language code: ", $langid)), + }; + R + }}; +} + +#[cfg(test)] +mod test { + const LANG_PL: crate::subtags::Language = language!("pL"); + const SCRIPT_LATN: crate::subtags::Script = script!("lAtN"); + const REGION_US: crate::subtags::Region = region!("us"); + const VARIANT_MACOS: crate::subtags::Variant = variant!("MACOS"); + const LANGID: crate::LanguageIdentifier = langid!("de-Arab-AT"); + + #[test] + fn language() { + let lang = language!("Pl"); + + assert_eq!(lang, "pl"); + assert_eq!(LANG_PL, "pl"); + assert_eq!(lang, LANG_PL); + } + + #[test] + fn script() { + let script = script!("latn"); + + assert_eq!(script, "Latn"); + assert_eq!(SCRIPT_LATN, "Latn"); + assert_eq!(script, SCRIPT_LATN); + } + + #[test] + fn region() { + let region = region!("us"); + + assert_eq!(region, "US"); + assert_eq!(REGION_US, "US"); + assert_eq!(region, REGION_US); + } + + #[test] + fn variant() { + let variant = variant!("macOS"); + + assert_eq!(variant, "macos"); + assert_eq!(VARIANT_MACOS, "macos"); + assert_eq!(variant, VARIANT_MACOS); + } + + #[test] + fn langid() { + let langid = langid!("de_Arab_aT"); + + assert_eq!(langid.to_string(), "de-Arab-AT"); + assert_eq!(LANGID.to_string(), "de-Arab-AT"); + assert_eq!(langid, LANGID); + } +} diff --git a/components/locid/src/parser/langid.rs b/components/locid/src/parser/langid.rs index bcf86010e29..2bac66d4dd3 100644 --- a/components/locid/src/parser/langid.rs +++ b/components/locid/src/parser/langid.rs @@ -5,7 +5,7 @@ use core::iter::Peekable; pub use super::errors::ParserError; -use crate::parser::get_subtag_iterator; +use crate::parser::{get_subtag_iterator, SubtagIterator}; use crate::subtags; use crate::LanguageIdentifier; use alloc::vec::Vec; @@ -25,7 +25,7 @@ enum ParserPosition { } pub fn parse_language_identifier_from_iter<'a>( - iter: &mut Peekable>, + iter: &mut SubtagIterator<'a>, mode: ParserMode, ) -> Result { let language; @@ -103,6 +103,95 @@ pub fn parse_language_identifier( t: &[u8], mode: ParserMode, ) -> Result { - let mut iter = get_subtag_iterator(t).peekable(); + let mut iter = get_subtag_iterator(t); parse_language_identifier_from_iter(&mut iter, mode) } + +pub const fn parse_language_identifier_without_variants_from_iter<'a>( + mut iter: SubtagIterator<'a>, + mode: ParserMode, +) -> Result< + ( + subtags::Language, + Option, + Option, + ), + ParserError, +> { + let language; + let mut script = None; + let mut region = None; + + if let (i, Some((t, start, end))) = iter.next_manual() { + iter = i; + match subtags::Language::from_bytes_manual_slice(t, start, end) { + Ok(l) => language = l, + Err(e) => return Err(e), + } + } else { + return Err(ParserError::InvalidLanguage); + } + + let mut position = ParserPosition::Script; + + while let Some((t, start, end)) = iter.peek_manual() { + if !matches!(mode, ParserMode::LanguageIdentifier) && start - end == 1 { + break; + } + + if matches!(position, ParserPosition::Script) { + if let Ok(s) = subtags::Script::from_bytes_manual_slice(t, start, end) { + script = Some(s); + position = ParserPosition::Region; + } else if let Ok(s) = subtags::Region::from_bytes_manual_slice(t, start, end) { + region = Some(s); + position = ParserPosition::Variant; + } else if subtags::Variant::from_bytes_manual_slice(t, start, end).is_ok() { + // We cannot handle variants in a const context + return Err(ParserError::InvalidSubtag); + } else if matches!(mode, ParserMode::Partial) { + break; + } else { + return Err(ParserError::InvalidSubtag); + } + } else if matches!(position, ParserPosition::Region) { + if let Ok(s) = subtags::Region::from_bytes_manual_slice(t, start, end) { + region = Some(s); + position = ParserPosition::Variant; + } else if subtags::Variant::from_bytes_manual_slice(t, start, end).is_ok() { + // We cannot handle variants in a const context + return Err(ParserError::InvalidSubtag); + } else if matches!(mode, ParserMode::Partial) { + break; + } else { + return Err(ParserError::InvalidSubtag); + } + } else if subtags::Variant::from_bytes_manual_slice(t, start, end).is_ok() { + // We cannot handle variants in a const context + return Err(ParserError::InvalidSubtag); + } else if matches!(mode, ParserMode::Partial) { + break; + } else { + return Err(ParserError::InvalidSubtag); + } + + iter = iter.next_manual().0; + } + + Ok((language, script, region)) +} + +pub const fn parse_language_identifier_without_variants( + t: &[u8], + mode: ParserMode, +) -> Result< + ( + subtags::Language, + Option, + Option, + ), + ParserError, +> { + let iter = get_subtag_iterator(t); + parse_language_identifier_without_variants_from_iter(iter, mode) +} diff --git a/components/locid/src/parser/locale.rs b/components/locid/src/parser/locale.rs index c498763563b..ad037c57934 100644 --- a/components/locid/src/parser/locale.rs +++ b/components/locid/src/parser/locale.rs @@ -8,7 +8,7 @@ use crate::parser::{get_subtag_iterator, parse_language_identifier_from_iter, Pa use crate::Locale; pub fn parse_locale(t: &[u8]) -> Result { - let mut iter = get_subtag_iterator(t).peekable(); + let mut iter = get_subtag_iterator(t); let id = parse_language_identifier_from_iter(&mut iter, ParserMode::Locale)?; let extensions = if iter.peek().is_some() { diff --git a/components/locid/src/parser/mod.rs b/components/locid/src/parser/mod.rs index b4129efe7cb..bc5dd73cfb7 100644 --- a/components/locid/src/parser/mod.rs +++ b/components/locid/src/parser/mod.rs @@ -7,9 +7,77 @@ mod langid; mod locale; pub use errors::ParserError; -pub use langid::{parse_language_identifier, parse_language_identifier_from_iter, ParserMode}; +pub use langid::{ + parse_language_identifier, parse_language_identifier_from_iter, + parse_language_identifier_without_variants, ParserMode, +}; pub use locale::parse_locale; -pub fn get_subtag_iterator(t: &[u8]) -> impl Iterator { - t.split(|c| *c == b'-' || *c == b'_') +pub const fn get_subtag_iterator(t: &[u8]) -> SubtagIterator { + let mut l_cursor = 0; + while l_cursor < t.len() && (t[l_cursor] == b'_' || t[l_cursor] == b'-') { + l_cursor += 1; + } + let mut r_cursor = l_cursor; + while r_cursor < t.len() && t[r_cursor] != b'_' && t[r_cursor] != b'-' { + r_cursor += 1; + } + SubtagIterator { + t, + l_cursor, + r_cursor, + } +} + +#[derive(Copy, Clone, Debug)] +pub struct SubtagIterator<'a> { + t: &'a [u8], + l_cursor: usize, + r_cursor: usize, +} + +impl<'a> SubtagIterator<'a> { + pub const fn next_manual(mut self) -> (Self, Option<(&'a [u8], usize, usize)>) { + if self.l_cursor == self.r_cursor { + (self, None) + } else { + let r = (self.t, self.l_cursor, self.r_cursor); + self.l_cursor = self.r_cursor; + while self.l_cursor < self.t.len() + && (self.t[self.l_cursor] == b'_' || self.t[self.l_cursor] == b'-') + { + self.l_cursor += 1; + } + self.r_cursor = self.l_cursor; + while self.r_cursor < self.t.len() + && self.t[self.r_cursor] != b'_' + && self.t[self.r_cursor] != b'-' + { + self.r_cursor += 1; + } + (self, Some(r)) + } + } + + pub const fn peek_manual(&self) -> Option<(&'a [u8], usize, usize)> { + if self.l_cursor == self.r_cursor { + None + } else { + Some((self.t, self.l_cursor, self.r_cursor)) + } + } + + pub fn peek(&self) -> Option<&'a [u8]> { + self.peek_manual().map(|(t, s, e)| &t[s..e]) + } +} + +impl<'a> Iterator for SubtagIterator<'a> { + type Item = &'a [u8]; + + fn next(&mut self) -> Option { + let (s, res) = self.next_manual(); + self.clone_from(&s); + res.map(|(t, s, e)| &t[s..e]) + } } diff --git a/components/locid/src/subtags/language.rs b/components/locid/src/subtags/language.rs index bba45f3d0bc..775f2f8765a 100644 --- a/components/locid/src/subtags/language.rs +++ b/components/locid/src/subtags/language.rs @@ -58,14 +58,27 @@ impl Language { /// /// assert_eq!(lang, "en"); /// ``` - pub fn from_bytes(v: &[u8]) -> Result { - let slen = v.len(); + pub const fn from_bytes(v: &[u8]) -> Result { + Self::from_bytes_manual_slice(v, 0, v.len()) + } + + /// Equivalent to [`from_bytes(bytes[start..end])`](Self::from_bytes), + /// but callable in a `const` context (which range indexing is not). + pub const fn from_bytes_manual_slice( + v: &[u8], + start: usize, + end: usize, + ) -> Result { + let slen = end - start; - if !LANGUAGE_LENGTH.contains(&slen) { + if slen < *LANGUAGE_LENGTH.start() || *LANGUAGE_LENGTH.end() < slen { return Err(ParserError::InvalidLanguage); } - let s = TinyStr4::from_bytes(v).map_err(|_| ParserError::InvalidLanguage)?; + let s = match TinyStr4::from_bytes_manual_slice(v, start, end) { + Ok(s) => s, + _ => return Err(ParserError::InvalidLanguage), + }; if !s.is_ascii_alphabetic() { return Err(ParserError::InvalidLanguage); @@ -73,7 +86,11 @@ impl Language { let value = s.to_ascii_lowercase(); - if value == UND_VALUE { + if slen == 3 + && value.all_bytes()[0] == UND_VALUE.all_bytes()[0] + && value.all_bytes()[1] == UND_VALUE.all_bytes()[1] + && value.all_bytes()[2] == UND_VALUE.all_bytes()[2] + { Ok(Self(None)) } else { Ok(Self(Some(value))) diff --git a/components/locid/src/subtags/region.rs b/components/locid/src/subtags/region.rs index 99843174aca..71ec5f6e69d 100644 --- a/components/locid/src/subtags/region.rs +++ b/components/locid/src/subtags/region.rs @@ -41,22 +41,24 @@ impl Region { /// /// assert_eq!(region, "FR"); /// ``` - pub fn from_bytes(v: &[u8]) -> Result { - match v.len() { - REGION_ALPHA_LENGTH => { - let s = TinyStr4::from_bytes(v).map_err(|_| ParserError::InvalidSubtag)?; - if !s.is_ascii_alphabetic() { - return Err(ParserError::InvalidSubtag); - } - Ok(Self(s.to_ascii_uppercase())) - } - REGION_NUM_LENGTH => { - let s = TinyStr4::from_bytes(v).map_err(|_| ParserError::InvalidSubtag)?; - if !s.is_ascii_numeric() { - return Err(ParserError::InvalidSubtag); - } - Ok(Self(s)) - } + pub const fn from_bytes(v: &[u8]) -> Result { + Self::from_bytes_manual_slice(v, 0, v.len()) + } + + /// Equivalent to [`from_bytes(bytes[start..end])`](Self::from_bytes), + /// but callable in a `const` context (which range indexing is not). + pub const fn from_bytes_manual_slice( + v: &[u8], + start: usize, + end: usize, + ) -> Result { + let s = match TinyStr4::from_bytes_manual_slice(v, start, end) { + Ok(s) => s, + _ => return Err(ParserError::InvalidSubtag), + }; + match end - start { + REGION_ALPHA_LENGTH if s.is_ascii_alphabetic() => Ok(Self(s.to_ascii_uppercase())), + REGION_NUM_LENGTH if s.is_ascii_numeric() => Ok(Self(s)), _ => Err(ParserError::InvalidSubtag), } } diff --git a/components/locid/src/subtags/script.rs b/components/locid/src/subtags/script.rs index dcab5df3a6f..3ddf98ac511 100644 --- a/components/locid/src/subtags/script.rs +++ b/components/locid/src/subtags/script.rs @@ -40,12 +40,26 @@ impl Script { /// /// assert_eq!(script, "Latn"); /// ``` - pub fn from_bytes(v: &[u8]) -> Result { - if v.len() != SCRIPT_LENGTH { + pub const fn from_bytes(v: &[u8]) -> Result { + Self::from_bytes_manual_slice(v, 0, v.len()) + } + + /// Equivalent to [`from_bytes(bytes[start..end])`](Self::from_bytes), + /// but callable in a `const` context (which range indexing is not). + pub const fn from_bytes_manual_slice( + v: &[u8], + start: usize, + end: usize, + ) -> Result { + if end - start != SCRIPT_LENGTH { return Err(ParserError::InvalidSubtag); } - let s = TinyStr4::from_bytes(v).map_err(|_| ParserError::InvalidSubtag)?; + let s = match TinyStr4::from_bytes_manual_slice(v, start, end) { + Ok(s) => s, + _ => return Err(ParserError::InvalidSubtag), + }; + if !s.is_ascii_alphabetic() { return Err(ParserError::InvalidSubtag); } diff --git a/components/locid/src/subtags/variant.rs b/components/locid/src/subtags/variant.rs index 94bdd917d49..e335f6991cc 100644 --- a/components/locid/src/subtags/variant.rs +++ b/components/locid/src/subtags/variant.rs @@ -42,20 +42,33 @@ impl Variant { /// /// assert_eq!(variant, "posix"); /// ``` - pub fn from_bytes(v: &[u8]) -> Result { - let slen = v.len(); + pub const fn from_bytes(v: &[u8]) -> Result { + Self::from_bytes_manual_slice(v, 0, v.len()) + } + + /// Equivalent to [`from_bytes(bytes[start..end])`](Self::from_bytes), + /// but callable in a `const` context (which range indexing is not). + pub const fn from_bytes_manual_slice( + v: &[u8], + start: usize, + end: usize, + ) -> Result { + let slen = end - start; - if !VARIANT_LENGTH.contains(&slen) { + if slen < *VARIANT_LENGTH.start() || *VARIANT_LENGTH.end() < slen { return Err(ParserError::InvalidSubtag); } - let s = TinyStr8::from_bytes(v).map_err(|_| ParserError::InvalidSubtag)?; + let s = match TinyStr8::from_bytes_manual_slice(v, start, end) { + Ok(s) => s, + _ => return Err(ParserError::InvalidSubtag), + }; if !s.is_ascii_alphanumeric() { return Err(ParserError::InvalidSubtag); } - if slen == VARIANT_NUM_ALPHA_LENGTH && !v[0].is_ascii_digit() { + if slen == VARIANT_NUM_ALPHA_LENGTH && !v[start].is_ascii_digit() { return Err(ParserError::InvalidSubtag); } diff --git a/utils/tinystr/src/ascii.rs b/utils/tinystr/src/ascii.rs index 4b663623414..6dda80925b8 100644 --- a/utils/tinystr/src/ascii.rs +++ b/utils/tinystr/src/ascii.rs @@ -14,10 +14,14 @@ pub struct TinyAsciiStr { } impl TinyAsciiStr { + /// Creates a `TinyAsciiStr` from the given byte slice. + /// `bytes` may contain at most `N` non-null ASCII bytes. pub const fn from_bytes(bytes: &[u8]) -> Result { Self::from_bytes_inner(bytes, 0, bytes.len(), false) } + /// Equivalent to [`from_bytes(bytes[start..end])`](Self::from_bytes), + /// but callable in a `const` context (which range indexing is not). pub const fn from_bytes_manual_slice( bytes: &[u8], start: usize, From 90aaf368b3604685bc6ba1cc6058442945e62b29 Mon Sep 17 00:00:00 2001 From: Robert Bastian Date: Mon, 21 Feb 2022 22:06:16 +0100 Subject: [PATCH 02/13] Replacing icu_locid_macros --- Cargo.lock | 53 --- Cargo.toml | 1 - README.md | 2 +- components/datetime/Cargo.toml | 1 - components/datetime/README.md | 4 +- components/datetime/examples/work_log.rs | 2 +- components/datetime/src/datetime.rs | 12 +- components/datetime/src/format/datetime.rs | 2 +- components/datetime/src/lib.rs | 4 +- components/datetime/src/skeleton/mod.rs | 2 +- components/datetime/src/time_zone.rs | 10 +- components/datetime/src/zoned_datetime.rs | 10 +- components/datetime/tests/datetime.rs | 2 +- .../datetime/tests/resolved_components.rs | 2 +- components/decimal/Cargo.toml | 1 - components/decimal/README.md | 2 +- components/decimal/examples/code_line_diff.rs | 2 +- components/decimal/src/lib.rs | 2 +- components/icu/Cargo.toml | 5 - components/icu/README.md | 2 +- components/icu/examples/tui.rs | 2 +- components/icu/src/lib.rs | 12 +- components/locale_canonicalizer/Cargo.toml | 1 - components/locid/macros/Cargo.toml | 39 --- components/locid/macros/LICENSE | 331 ------------------ components/locid/macros/README.md | 20 -- components/locid/macros/src/lib.rs | 226 ------------ components/locid/macros/src/token_stream.rs | 116 ------ components/locid/macros/tests/macros.rs | 61 ---- components/locid/src/macros.rs | 12 +- components/plurals/Cargo.toml | 1 - components/plurals/README.md | 2 +- components/plurals/benches/pluralrules.rs | 2 +- .../plurals/examples/elevator_floors.rs | 2 +- components/plurals/examples/unread_emails.rs | 2 +- components/plurals/src/lib.rs | 18 +- components/plurals/tests/plurals.rs | 2 +- .../20201015_ICU4X_Project_Announcement.md | 2 +- docs/tutorials/intro.md | 4 +- experimental/list_formatter/Cargo.toml | 1 - experimental/list_formatter/README.md | 2 +- .../list_formatter/examples/and_list.rs | 2 +- experimental/list_formatter/src/lib.rs | 2 +- ffi/diplomat/Cargo.toml | 1 - ffi/diplomat/src/locale.rs | 4 +- provider/blob/Cargo.toml | 1 - provider/blob/examples/simple_static.rs | 2 +- provider/blob/src/blob_data_provider.rs | 2 +- provider/blob/src/static_data_provider.rs | 4 +- provider/cldr/Cargo.toml | 2 - .../cldr/src/transform/calendar/japanese.rs | 2 +- .../cldr/src/transform/datetime/patterns.rs | 4 +- .../cldr/src/transform/datetime/skeletons.rs | 2 +- .../cldr/src/transform/datetime/symbols.rs | 4 +- provider/cldr/src/transform/decimal/mod.rs | 2 +- provider/cldr/src/transform/list/mod.rs | 4 +- .../locale_canonicalizer/likely_subtags.rs | 2 +- provider/cldr/src/transform/plurals/mod.rs | 2 +- provider/cldr/src/transform/time_zones/mod.rs | 2 +- provider/core/Cargo.toml | 1 - provider/core/src/buf.rs | 2 +- provider/core/src/data_provider.rs | 2 +- provider/core/src/filter/impls.rs | 6 +- provider/core/src/filter/mod.rs | 2 +- provider/core/src/fork/by_key.rs | 6 +- provider/core/src/hello_world.rs | 34 +- provider/core/src/resource.rs | 2 +- provider/fs/Cargo.toml | 1 - provider/fs/benches/provider_fs.rs | 2 +- provider/fs/tests/test_file_io.rs | 2 +- provider/testdata/Cargo.toml | 1 - provider/testdata/README.md | 2 +- provider/testdata/src/lib.rs | 2 +- utils/litemap/Cargo.toml | 1 - .../examples/language_names_hash_map.rs | 2 +- .../examples/language_names_lite_map.rs | 2 +- 76 files changed, 112 insertions(+), 981 deletions(-) delete mode 100644 components/locid/macros/Cargo.toml delete mode 100644 components/locid/macros/LICENSE delete mode 100644 components/locid/macros/README.md delete mode 100644 components/locid/macros/src/lib.rs delete mode 100644 components/locid/macros/src/token_stream.rs delete mode 100644 components/locid/macros/tests/macros.rs diff --git a/Cargo.lock b/Cargo.lock index 49e99195699..9c354ca58c9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -996,7 +996,6 @@ dependencies = [ "icu_decimal", "icu_locale_canonicalizer", "icu_locid", - "icu_locid_macros", "icu_plurals", "icu_properties", "icu_provider", @@ -1073,7 +1072,6 @@ dependencies = [ "icu_decimal", "icu_locale_canonicalizer", "icu_locid", - "icu_locid_macros", "icu_plurals", "icu_properties", "icu_provider", @@ -1143,7 +1141,6 @@ dependencies = [ "icu_benchmark_macros", "icu_calendar", "icu_locid", - "icu_locid_macros", "icu_plurals", "icu_provider", "icu_testdata", @@ -1169,7 +1166,6 @@ dependencies = [ "icu", "icu_benchmark_macros", "icu_locid", - "icu_locid_macros", "icu_provider", "icu_testdata", "rand", @@ -1188,7 +1184,6 @@ dependencies = [ "displaydoc", "icu_benchmark_macros", "icu_locid", - "icu_locid_macros", "icu_provider", "icu_testdata", "postcard", @@ -1205,7 +1200,6 @@ version = "0.5.0" dependencies = [ "criterion", "icu_locid", - "icu_locid_macros", "icu_provider", "icu_testdata", "litemap", @@ -1228,16 +1222,6 @@ dependencies = [ "writeable", ] -[[package]] -name = "icu_locid_macros" -version = "0.5.0" -dependencies = [ - "icu", - "icu_locid", - "proc-macro-crate", - "tinystr 0.5.0", -] - [[package]] name = "icu_pattern" version = "0.1.1" @@ -1257,7 +1241,6 @@ dependencies = [ "icu", "icu_benchmark_macros", "icu_locid", - "icu_locid_macros", "icu_provider", "icu_testdata", "num_enum", @@ -1289,7 +1272,6 @@ dependencies = [ "displaydoc", "erased-serde", "icu_locid", - "icu_locid_macros", "icu_provider_macros", "litemap", "log", @@ -1311,7 +1293,6 @@ dependencies = [ "erased-serde", "icu", "icu_locid", - "icu_locid_macros", "icu_provider", "icu_testdata", "litemap", @@ -1335,7 +1316,6 @@ dependencies = [ "icu_list", "icu_locale_canonicalizer", "icu_locid", - "icu_locid_macros", "icu_plurals", "icu_properties", "icu_provider", @@ -1370,7 +1350,6 @@ dependencies = [ "erased-serde", "icu_benchmark_macros", "icu_locid", - "icu_locid_macros", "icu_plurals", "icu_provider", "log", @@ -1449,7 +1428,6 @@ dependencies = [ "cargo_metadata", "displaydoc", "icu_locid", - "icu_locid_macros", "icu_plurals", "icu_provider", "icu_provider_blob", @@ -1575,7 +1553,6 @@ dependencies = [ "criterion", "icu_benchmark_macros", "icu_locid", - "icu_locid_macros", "postcard", "rkyv", "serde", @@ -1953,16 +1930,6 @@ version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed0cfbc8191465bed66e1718596ee0b0b35d5ee1f41c5df2189d0fe8bde535ba" -[[package]] -name = "proc-macro-crate" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1ebace6889caf889b4d3f76becee12e90353f2b8c7d875534a71e5742f8f6f83" -dependencies = [ - "thiserror", - "toml", -] - [[package]] name = "proc-macro-hack" version = "0.5.19" @@ -2617,26 +2584,6 @@ dependencies = [ "unicode-width", ] -[[package]] -name = "thiserror" -version = "1.0.30" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "854babe52e4df1653706b98fcfc05843010039b406875930a70e4d9644e5c417" -dependencies = [ - "thiserror-impl", -] - -[[package]] -name = "thiserror-impl" -version = "1.0.30" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa32fd3f627f367fe16f893e2597ae3c05020f8bba2666a4e6ea73d377e5714b" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "thousands" version = "0.2.0" diff --git a/Cargo.toml b/Cargo.toml index 21529e33727..28dee278bdd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,6 @@ members = [ "components/icu4x", "components/locale_canonicalizer", "components/locid", - "components/locid/macros", "components/plurals", "components/properties", "experimental/bies", diff --git a/README.md b/README.md index 9870e1c4eac..8f5227d4eb2 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ icu_provider_fs = "0.2" ``` ```rust -use icu::locid::macros::langid; +use icu::locid::langid; use icu::locid::Locale; use icu::datetime::{DateTimeFormat, mock::datetime::MockDateTime, options::length}; use icu_provider_fs::FsDataProvider; diff --git a/components/datetime/Cargo.toml b/components/datetime/Cargo.toml index b6e922c6280..1edfa61155b 100644 --- a/components/datetime/Cargo.toml +++ b/components/datetime/Cargo.toml @@ -54,7 +54,6 @@ icu_benchmark_macros = { version = "0.5", path = "../../tools/benchmark/macros" icu_provider = { version = "0.5", path = "../../provider/core" } icu_testdata = { version = "0.5", path = "../../provider/testdata", features = ["static"] } icu_calendar = { version = "0.5", path = "../calendar", features = ["provider_serde"] } -icu_locid_macros = { version = "0.5", path = "../locid/macros" } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" bincode = "1.3" diff --git a/components/datetime/README.md b/components/datetime/README.md index 143beb8a517..c350c3189f7 100644 --- a/components/datetime/README.md +++ b/components/datetime/README.md @@ -12,7 +12,7 @@ used to quickly format any date and time provided. ```rust use icu::locid::Locale; -use icu::locid::macros::langid; +use icu::locid::langid; use icu::calendar::Gregorian; use icu::datetime::{DateTimeFormat, DateTimeFormatOptions, mock::parse_gregorian_from_str, options::length}; @@ -43,7 +43,7 @@ convert a [`options::length::Bag`] into a [`DateTimeFormatOptions::Length`]. ```rust use icu::locid::Locale; -use icu::locid::macros::langid; +use icu::locid::langid; use icu::calendar::Gregorian; use icu::datetime::{DateTimeFormat, DateTimeFormatOptions, options::length}; let options = length::Bag { diff --git a/components/datetime/examples/work_log.rs b/components/datetime/examples/work_log.rs index 37e03d5923e..2c0283ff060 100644 --- a/components/datetime/examples/work_log.rs +++ b/components/datetime/examples/work_log.rs @@ -12,8 +12,8 @@ icu_benchmark_macros::static_setup!(); use icu_calendar::{DateTime, Gregorian}; use icu_datetime::mock::parse_gregorian_from_str; use icu_datetime::{options::length, DateTimeFormat}; +use icu_locid::langid; use icu_locid::Locale; -use icu_locid_macros::langid; const DATES_ISO: &[&str] = &[ "2001-09-08T18:46:40:000", diff --git a/components/datetime/src/datetime.rs b/components/datetime/src/datetime.rs index 0dd2ea5002e..723d745ad86 100644 --- a/components/datetime/src/datetime.rs +++ b/components/datetime/src/datetime.rs @@ -32,7 +32,7 @@ use crate::{date::DateTimeInput, CldrCalendar, DateTimeFormatError, FormattedDat /// /// ``` /// use icu::locid::Locale; -/// use icu::locid::macros::langid; +/// use icu::locid::langid; /// use icu::datetime::{DateTimeFormat, options::length}; /// use icu::calendar::{DateTime, Gregorian}; /// use icu_provider::inv::InvariantDataProvider; @@ -69,7 +69,7 @@ impl DateTimeFormat { /// /// ``` /// use icu::locid::Locale; - /// use icu::locid::macros::langid; + /// use icu::locid::langid; /// use icu::calendar::Gregorian; /// use icu::datetime::{DateTimeFormat, DateTimeFormatOptions}; /// use icu_provider::inv::InvariantDataProvider; @@ -111,7 +111,7 @@ impl DateTimeFormat { /// /// ``` /// use icu::locid::Locale; - /// use icu::locid::macros::langid; + /// use icu::locid::langid; /// use icu::datetime::{DateTimeFormat, DateTimeFormatOptions}; /// use icu::calendar::{DateTime, Gregorian}; /// use icu_provider::inv::InvariantDataProvider; @@ -147,7 +147,7 @@ impl DateTimeFormat { /// /// ``` /// use icu::locid::Locale; - /// use icu::locid::macros::langid; + /// use icu::locid::langid; /// use icu::datetime::{DateTimeFormat, DateTimeFormatOptions}; /// use icu::calendar::{DateTime, Gregorian}; /// use icu_provider::inv::InvariantDataProvider; @@ -181,7 +181,7 @@ impl DateTimeFormat { /// /// ``` /// use icu::locid::Locale; - /// use icu::locid::macros::langid; + /// use icu::locid::langid; /// use icu::datetime::{DateTimeFormat, DateTimeFormatOptions}; /// use icu::calendar::{DateTime, Gregorian}; /// use icu_provider::inv::InvariantDataProvider; @@ -214,7 +214,7 @@ impl DateTimeFormat { /// options::{components, length}, /// DateTimeFormat, DateTimeFormatOptions, /// }; - /// use icu::locid::macros::langid; + /// use icu::locid::langid; /// use icu::locid::Locale; /// /// let options = DateTimeFormatOptions::Length(length::Bag { diff --git a/components/datetime/src/format/datetime.rs b/components/datetime/src/format/datetime.rs index 729af765706..39f81bd647b 100644 --- a/components/datetime/src/format/datetime.rs +++ b/components/datetime/src/format/datetime.rs @@ -31,7 +31,7 @@ use writeable::Writeable; /// /// ``` /// use icu::locid::Locale; -/// use icu::locid::macros::langid; +/// use icu::locid::langid; /// use icu::datetime::{DateTimeFormat, DateTimeFormatOptions}; /// use icu::calendar::{DateTime, Gregorian}; /// use icu_provider::inv::InvariantDataProvider; diff --git a/components/datetime/src/lib.rs b/components/datetime/src/lib.rs index 395acca9c5e..26236f9e54c 100644 --- a/components/datetime/src/lib.rs +++ b/components/datetime/src/lib.rs @@ -16,7 +16,7 @@ //! //! ``` //! use icu::locid::Locale; -//! use icu::locid::macros::langid; +//! use icu::locid::langid; //! use icu::calendar::Gregorian; //! use icu::datetime::{DateTimeFormat, DateTimeFormatOptions, mock::parse_gregorian_from_str, options::length}; //! @@ -47,7 +47,7 @@ //! //! ``` //! use icu::locid::Locale; -//! use icu::locid::macros::langid; +//! use icu::locid::langid; //! use icu::calendar::Gregorian; //! use icu::datetime::{DateTimeFormat, DateTimeFormatOptions, options::length}; //! # let provider = icu_testdata::get_provider(); diff --git a/components/datetime/src/skeleton/mod.rs b/components/datetime/src/skeleton/mod.rs index 261f29a6447..2d0ff3ba3ca 100644 --- a/components/datetime/src/skeleton/mod.rs +++ b/components/datetime/src/skeleton/mod.rs @@ -17,7 +17,7 @@ pub use helpers::*; mod test { use super::reference::Skeleton; use super::*; - use icu_locid_macros::langid; + use icu_locid::langid; use icu_provider::prelude::*; use crate::{ diff --git a/components/datetime/src/time_zone.rs b/components/datetime/src/time_zone.rs index dda2aeb9045..dffc7d63e38 100644 --- a/components/datetime/src/time_zone.rs +++ b/components/datetime/src/time_zone.rs @@ -63,7 +63,7 @@ where /// /// ``` /// use icu_locid::Locale; -/// use icu::locid::macros::langid; +/// use icu::locid::langid; /// use icu_datetime::{TimeZoneFormat, TimeZoneFormatConfig}; /// use icu_datetime::date::GmtOffset; /// use icu_datetime::mock::time_zone::MockTimeZone; @@ -223,7 +223,7 @@ impl TimeZoneFormat { /// /// ``` /// use icu_locid::Locale; - /// use icu::locid::macros::langid; + /// use icu::locid::langid; /// use icu_datetime::{TimeZoneFormat, TimeZoneFormatConfig}; /// use icu_datetime::mock::time_zone::MockTimeZone; /// use icu_provider::inv::InvariantDataProvider; @@ -313,7 +313,7 @@ impl TimeZoneFormat { /// /// ``` /// use icu_locid::Locale; - /// use icu::locid::macros::langid; + /// use icu::locid::langid; /// use icu_datetime::{TimeZoneFormat, TimeZoneFormatConfig}; /// use icu_datetime::date::GmtOffset; /// use icu_datetime::mock::time_zone::MockTimeZone; @@ -351,7 +351,7 @@ impl TimeZoneFormat { /// /// ``` /// use icu_locid::Locale; - /// use icu::locid::macros::langid; + /// use icu::locid::langid; /// use icu_datetime::{TimeZoneFormat, TimeZoneFormatConfig}; /// use icu_datetime::date::GmtOffset; /// use icu_datetime::mock::time_zone::MockTimeZone; @@ -390,7 +390,7 @@ impl TimeZoneFormat { /// /// ``` /// use icu_locid::Locale; - /// use icu::locid::macros::langid; + /// use icu::locid::langid; /// use icu_datetime::{TimeZoneFormat, TimeZoneFormatConfig}; /// use icu_datetime::date::GmtOffset; /// use icu_datetime::mock::time_zone::MockTimeZone; diff --git a/components/datetime/src/zoned_datetime.rs b/components/datetime/src/zoned_datetime.rs index 44e198102a7..0c7d3ee829d 100644 --- a/components/datetime/src/zoned_datetime.rs +++ b/components/datetime/src/zoned_datetime.rs @@ -36,7 +36,7 @@ use crate::{ /// /// ``` /// use icu::locid::Locale; -/// use icu::locid::macros::langid; +/// use icu::locid::langid; /// use icu::calendar::Gregorian; /// use icu::datetime::{ZonedDateTimeFormat, options::length}; /// use icu::datetime::mock::zoned_datetime::MockZonedDateTime; @@ -74,7 +74,7 @@ impl ZonedDateTimeFormat { /// /// ``` /// use icu::locid::Locale; - /// use icu::locid::macros::langid; + /// use icu::locid::langid; /// use icu::calendar::Gregorian; /// use icu::datetime::{ZonedDateTimeFormat, DateTimeFormatOptions}; /// use icu::datetime::mock::zoned_datetime::MockZonedDateTime; @@ -136,7 +136,7 @@ impl ZonedDateTimeFormat { /// /// ``` /// use icu::locid::Locale; - /// use icu::locid::macros::langid; + /// use icu::locid::langid; /// use icu::calendar::Gregorian; /// use icu::datetime::{ZonedDateTimeFormat, DateTimeFormatOptions}; /// use icu::datetime::mock::zoned_datetime::MockZonedDateTime; @@ -176,7 +176,7 @@ impl ZonedDateTimeFormat { /// /// ``` /// use icu::locid::Locale; - /// use icu::locid::macros::langid; + /// use icu::locid::langid; /// use icu::calendar::Gregorian; /// use icu::datetime::{ZonedDateTimeFormat, DateTimeFormatOptions}; /// use icu::datetime::mock::zoned_datetime::MockZonedDateTime; @@ -214,7 +214,7 @@ impl ZonedDateTimeFormat { /// /// ``` /// use icu::locid::Locale; - /// use icu::locid::macros::langid; + /// use icu::locid::langid; /// use icu::calendar::Gregorian; /// use icu::datetime::{ZonedDateTimeFormat, DateTimeFormatOptions}; /// use icu::datetime::mock::zoned_datetime::MockZonedDateTime; diff --git a/components/datetime/tests/datetime.rs b/components/datetime/tests/datetime.rs index 9900307ebfe..baf15745a57 100644 --- a/components/datetime/tests/datetime.rs +++ b/components/datetime/tests/datetime.rs @@ -492,8 +492,8 @@ fn constructing_datetime_format_with_time_zone_pattern_symbols_is_err() { options::length::{Bag, Time}, DateTimeFormatOptions, }; + use icu_locid::langid; use icu_locid::Locale; - use icu_locid_macros::langid; let options = DateTimeFormatOptions::Length(Bag { // Full has time-zone symbols. diff --git a/components/datetime/tests/resolved_components.rs b/components/datetime/tests/resolved_components.rs index 02a7653130b..e0f267b906c 100644 --- a/components/datetime/tests/resolved_components.rs +++ b/components/datetime/tests/resolved_components.rs @@ -7,8 +7,8 @@ use icu_datetime::{ options::{components, length, preferences}, DateTimeFormat, DateTimeFormatOptions, }; +use icu_locid::langid; use icu_locid::Locale; -use icu_locid_macros::langid; fn assert_resolved_components(options: &DateTimeFormatOptions, bag: &components::Bag) { let locale: Locale = langid!("en").into(); diff --git a/components/decimal/Cargo.toml b/components/decimal/Cargo.toml index 2883d740401..13e088d3c36 100644 --- a/components/decimal/Cargo.toml +++ b/components/decimal/Cargo.toml @@ -42,7 +42,6 @@ displaydoc = { version = "0.2.3", default-features = false } criterion = "0.3" icu = { path = "../icu", default-features = false } icu_benchmark_macros = { version = "0.5", path = "../../tools/benchmark/macros" } -icu_locid_macros = { version = "0.5", path = "../locid/macros" } icu_testdata = { version = "0.5", path = "../../provider/testdata", features = ["static"] } rand = "0.8" rand_pcg = "0.3" diff --git a/components/decimal/README.md b/components/decimal/README.md index 68461e4e986..b8d79ce67cf 100644 --- a/components/decimal/README.md +++ b/components/decimal/README.md @@ -15,7 +15,7 @@ follow [icu4x#275](https://github.com/unicode-org/icu4x/issues/275). ```rust use icu::decimal::FixedDecimalFormat; use icu::locid::Locale; -use icu::locid::macros::langid; +use icu::locid::langid; use writeable::Writeable; let locale: Locale = langid!("bn").into(); diff --git a/components/decimal/examples/code_line_diff.rs b/components/decimal/examples/code_line_diff.rs index 80dea33c7db..9ff31e1ec72 100644 --- a/components/decimal/examples/code_line_diff.rs +++ b/components/decimal/examples/code_line_diff.rs @@ -9,8 +9,8 @@ use fixed_decimal::FixedDecimal; use icu_decimal::{options, FixedDecimalFormat}; +use icu_locid::langid; use icu_locid::Locale; -use icu_locid_macros::langid; use writeable::Writeable; icu_benchmark_macros::static_setup!(); diff --git a/components/decimal/src/lib.rs b/components/decimal/src/lib.rs index 73b2cdcac61..8771fedd44c 100644 --- a/components/decimal/src/lib.rs +++ b/components/decimal/src/lib.rs @@ -19,7 +19,7 @@ //! ``` //! use icu::decimal::FixedDecimalFormat; //! use icu::locid::Locale; -//! use icu::locid::macros::langid; +//! use icu::locid::langid; //! use writeable::Writeable; //! //! let locale: Locale = langid!("bn").into(); diff --git a/components/icu/Cargo.toml b/components/icu/Cargo.toml index cdb8c7cead1..e0f11a428f5 100644 --- a/components/icu/Cargo.toml +++ b/components/icu/Cargo.toml @@ -51,11 +51,6 @@ version = "0.5" path = "../locid" default-features = false -[dependencies.icu_locid_macros] -version = "0.5" -path = "../locid/macros" -default-features = false - [dependencies.icu_locale_canonicalizer] version = "0.5" path = "../locale_canonicalizer" diff --git a/components/icu/README.md b/components/icu/README.md index 89432e7e56c..f7b86e85638 100644 --- a/components/icu/README.md +++ b/components/icu/README.md @@ -52,7 +52,7 @@ functionality are compiled. These features are: ```rust use icu::locid::Locale; -use icu::locid::macros::langid; +use icu::locid::langid; use icu::datetime::{DateTimeFormat, options::length, mock::parse_gregorian_from_str}; let provider = icu_testdata::get_provider(); diff --git a/components/icu/examples/tui.rs b/components/icu/examples/tui.rs index baf776ff164..ae9d7fc39d1 100644 --- a/components/icu/examples/tui.rs +++ b/components/icu/examples/tui.rs @@ -9,7 +9,7 @@ use icu::calendar::Gregorian; use icu::datetime::DateTimeFormatOptions; -use icu::locid::{macros::langid, Locale}; +use icu::locid::{langid, Locale}; use icu::plurals::{PluralCategory, PluralRules}; use icu_datetime::{mock::zoned_datetime::MockZonedDateTime, ZonedDateTimeFormat}; use icu_uniset::UnicodeSetBuilder; diff --git a/components/icu/src/lib.rs b/components/icu/src/lib.rs index 556c5ddd249..b93b14c6810 100644 --- a/components/icu/src/lib.rs +++ b/components/icu/src/lib.rs @@ -54,7 +54,7 @@ //! //! ``` //! use icu::locid::Locale; -//! use icu::locid::macros::langid; +//! use icu::locid::langid; //! use icu::datetime::{DateTimeFormat, options::length, mock::parse_gregorian_from_str}; //! //! let provider = icu_testdata::get_provider(); @@ -112,7 +112,7 @@ pub mod datetime { //! //! ``` //! use icu::locid::Locale; - //! use icu::locid::macros::langid; + //! use icu::locid::langid; //! use icu::datetime::{DateTimeFormat, options::length, mock::parse_gregorian_from_str}; //! //! let provider = icu_testdata::get_provider(); @@ -153,7 +153,7 @@ pub mod decimal { //! ``` //! use icu::decimal::FixedDecimalFormat; //! use icu::locid::Locale; - //! use icu::locid::macros::langid; + //! use icu::locid::langid; //! use writeable::Writeable; //! //! let locale: Locale = langid!("bn").into(); @@ -293,10 +293,6 @@ pub mod locid { //! [`ICU4X`]: https://github.com/unicode-org/icu4x //! [`Unicode Extensions`]: extensions pub use icu_locid::*; - - pub mod macros { - pub use icu_locid_macros::*; - } } pub mod plurals { @@ -322,7 +318,7 @@ pub mod plurals { //! # Examples //! //! ``` - //! use icu::locid::macros::langid; + //! use icu::locid::langid; //! use icu::plurals::{PluralRules, PluralRuleType, PluralCategory}; //! //! let lid = langid!("en"); diff --git a/components/locale_canonicalizer/Cargo.toml b/components/locale_canonicalizer/Cargo.toml index a3bcce752a4..b6d6f143741 100644 --- a/components/locale_canonicalizer/Cargo.toml +++ b/components/locale_canonicalizer/Cargo.toml @@ -39,7 +39,6 @@ tinystr = { path = "../../utils/tinystr", version = "0.5.0", default-features = [dev-dependencies] criterion = "0.3.3" -icu_locid_macros = { version = "0.5", path = "../locid/macros" } icu_testdata = { version = "0.5", path = "../../provider/testdata", features = ["static"] } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" diff --git a/components/locid/macros/Cargo.toml b/components/locid/macros/Cargo.toml deleted file mode 100644 index 22c1c43a12f..00000000000 --- a/components/locid/macros/Cargo.toml +++ /dev/null @@ -1,39 +0,0 @@ -# This file is part of ICU4X. For terms of use, please see the file -# called LICENSE at the top level of the ICU4X source tree -# (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). - -[package] -name = "icu_locid_macros" -description = "proc-macros for icu_locid" -version = "0.5.0" -authors = ["The ICU4X Project Developers"] -edition = "2018" -readme = "README.md" -repository = "https://github.com/unicode-org/icu4x" -license-file = "LICENSE" -categories = ["internationalization"] -# Keep this in sync with other crates unless there are exceptions -include = [ - "src/**/*", - "examples/**/*", - "benches/**/*", - "benches/**/*", - "Cargo.toml", - "LICENSE", - "README.md" -] - -[package.metadata.docs.rs] -all-features = true - -[lib] -path = "src/lib.rs" -proc_macro = true - -[dependencies] -proc-macro-crate = "1.0" -icu_locid = { version = "0.5", path = "../" } -tinystr = { path = "../../../utils/tinystr", version = "0.5.0", features = ["alloc"], default-features = false } - -[dev-dependencies] -icu = { path = "../../icu", default-features = false } diff --git a/components/locid/macros/LICENSE b/components/locid/macros/LICENSE deleted file mode 100644 index 5ab1f57507b..00000000000 --- a/components/locid/macros/LICENSE +++ /dev/null @@ -1,331 +0,0 @@ -Except as otherwise noted below, ICU4X is licensed under the Apache -License, Version 2.0 (included below) or the MIT license (included -below), at your option. Unless importing data or code in the manner -stated below, any contribution intentionally submitted for inclusion -in ICU4X by you, as defined in the Apache-2.0 license, shall be dual -licensed in the foregoing manner, without any additional terms or -conditions. - -As exceptions to the above: -* Portions of ICU4X that have been adapted from ICU4C and/or ICU4J are -under the Unicode license (included below) and/or the ICU license -(included below) as indicated by source code comments. -* Unicode data incorporated in ICU4X is under the Unicode license -(included below). -* Your contributions may import code from ICU4C and/or ICU4J and -Unicode data under these licenses. Indicate the license and the ICU4C -or ICU4J origin in source code comments. - -- - - - - -Apache License, version 2.0 - - - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "[]" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - Copyright [yyyy] [name of copyright owner] - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - -- - - - - -MIT License - -Copyright The ICU4X Authors - -Permission is hereby granted, free of charge, to any -person obtaining a copy of this software and associated -documentation files (the "Software"), to deal in the -Software without restriction, including without -limitation the rights to use, copy, modify, merge, -publish, distribute, sublicense, and/or sell copies of -the Software, and to permit persons to whom the Software -is furnished to do so, subject to the following -conditions: - -The above copyright notice and this permission notice -shall be included in all copies or substantial portions -of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF -ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED -TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A -PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT -SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR -IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -DEALINGS IN THE SOFTWARE. - -- - - - - -Unicode License - -COPYRIGHT AND PERMISSION NOTICE (ICU 58 and later) - -Copyright © 1991-2020 Unicode, Inc. All rights reserved. -Distributed under the Terms of Use in https://www.unicode.org/copyright.html. - -Permission is hereby granted, free of charge, to any person obtaining -a copy of the Unicode data files and any associated documentation -(the "Data Files") or Unicode software and any associated documentation -(the "Software") to deal in the Data Files or Software -without restriction, including without limitation the rights to use, -copy, modify, merge, publish, distribute, and/or sell copies of -the Data Files or Software, and to permit persons to whom the Data Files -or Software are furnished to do so, provided that either -(a) this copyright and permission notice appear with all copies -of the Data Files or Software, or -(b) this copyright and permission notice appear in associated -Documentation. - -THE DATA FILES AND SOFTWARE ARE PROVIDED "AS IS", WITHOUT WARRANTY OF -ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE -WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -NONINFRINGEMENT OF THIRD PARTY RIGHTS. -IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS -NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL -DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, -DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER -TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR -PERFORMANCE OF THE DATA FILES OR SOFTWARE. - -Except as contained in this notice, the name of a copyright holder -shall not be used in advertising or otherwise to promote the sale, -use or other dealings in these Data Files or Software without prior -written authorization of the copyright holder. - -- - - - - -ICU License - ICU 1.8.1 to ICU 57.1 - -COPYRIGHT AND PERMISSION NOTICE - -Copyright (c) 1995-2016 International Business Machines Corporation and others -All rights reserved. - -Permission is hereby granted, free of charge, to any person obtaining -a copy of this software and associated documentation files (the -"Software"), to deal in the Software without restriction, including -without limitation the rights to use, copy, modify, merge, publish, -distribute, and/or sell copies of the Software, and to permit persons -to whom the Software is furnished to do so, provided that the above -copyright notice(s) and this permission notice appear in all copies of -the Software and that both the above copyright notice(s) and this -permission notice appear in supporting documentation. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT -OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR -HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY -SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER -RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF -CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN -CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - -Except as contained in this notice, the name of a copyright holder -shall not be used in advertising or otherwise to promote the sale, use -or other dealings in this Software without prior written authorization -of the copyright holder. - -All trademarks and registered trademarks mentioned herein are the -property of their respective owners. - -- - - - diff --git a/components/locid/macros/README.md b/components/locid/macros/README.md deleted file mode 100644 index 65665e4dfa0..00000000000 --- a/components/locid/macros/README.md +++ /dev/null @@ -1,20 +0,0 @@ -# icu_locid_macros [![crates.io](https://img.shields.io/crates/v/icu_locid_macros)](https://crates.io/crates/icu_locid_macros) - -[`icu_locid_macros`](crate) is one of the ICU4X components. - -This API provides convenience macros for [`icu_locid`]. - -## Examples - -```rust -use icu_locid_macros::{language, region, langid}; - -let lid = langid!("EN_US"); - -assert_eq!(lid.language, language!("en")); -assert_eq!(lid.region, Some(region!("US"))); -``` - -## More Information - -For more information on development, authorship, contributing etc. please visit [`ICU4X home page`](https://github.com/unicode-org/icu4x). diff --git a/components/locid/macros/src/lib.rs b/components/locid/macros/src/lib.rs deleted file mode 100644 index 6d5a9b0938c..00000000000 --- a/components/locid/macros/src/lib.rs +++ /dev/null @@ -1,226 +0,0 @@ -// This file is part of ICU4X. For terms of use, please see the file -// called LICENSE at the top level of the ICU4X source tree -// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). - -//! [`icu_locid_macros`](crate) is one of the ICU4X components. -//! -//! This API provides convenience macros for [`icu_locid`]. -//! -//! # Examples -//! -//! ```rust -//! use icu_locid_macros::{language, region, langid}; -//! -//! let lid = langid!("EN_US"); -//! -//! assert_eq!(lid.language, language!("en")); -//! assert_eq!(lid.region, Some(region!("US"))); -//! ``` - -mod token_stream; - -extern crate proc_macro; - -use proc_macro::TokenStream; -use proc_macro_crate::{crate_name, FoundCrate}; -use token_stream::IntoTokenStream; - -fn get_crate_name() -> String { - match crate_name("icu") { - Ok(FoundCrate::Itself) => "icu::locid".to_string(), - Ok(FoundCrate::Name(name)) => format!("{}::locid", name), - Err(_) => match crate_name("icu_locid") { - Ok(FoundCrate::Name(name)) => name, - _ => "icu_locid".to_string(), - }, - } -} - -fn get_value_from_token_stream(input: TokenStream) -> String { - let val = format!("{}", input); - if !val.starts_with('"') || !val.ends_with('"') { - panic!("Argument must be a string literal."); - } - let len = val.len(); - (&val[1..len - 1]).to_string() -} - -/// A macro allowing for compile-time construction of valid [`Language`] subtag. -/// -/// The macro will perform syntax canonicalization of the tag. -/// -/// # Examples -/// -/// ``` -/// use icu::locid::subtags::Language; -/// use icu::locid::macros::language; -/// -/// const DE: Language = language!("DE"); -/// -/// let de: Language = "DE".parse() -/// .expect("Failed to parse language subtag."); -/// -/// assert_eq!(DE, "de"); -/// assert_eq!(DE, de); -/// ``` -/// -/// [`Language`]: icu_locid::subtags::Language -#[proc_macro] -pub fn language(input: TokenStream) -> TokenStream { - let val = get_value_from_token_stream(input); - - let parsed: icu_locid::subtags::Language = val.parse().expect("Malformed Language Subtag"); - - parsed - .into_token_stream() - .expect("Failed to parse token stream.") -} - -/// A macro allowing for compile-time construction of valid [`Script`] subtag. -/// -/// The macro will perform syntax canonicalization of the tag. -/// -/// # Examples -/// -/// ``` -/// use icu::locid::subtags::Script; -/// use icu::locid::macros::script; -/// -/// const ARAB: Script = script!("aRAB"); -/// -/// let arab: Script = "aRaB".parse() -/// .expect("Failed to parse script subtag."); -/// -/// assert_eq!(ARAB, "Arab"); -/// assert_eq!(ARAB, arab); -/// ``` -/// -/// [`Script`]: icu_locid::subtags::Script -#[proc_macro] -pub fn script(input: TokenStream) -> TokenStream { - let val = get_value_from_token_stream(input); - - let parsed: icu_locid::subtags::Script = val.parse().expect("Malformed Script Subtag"); - - parsed - .into_token_stream() - .expect("Failed to parse token stream.") -} - -/// A macro allowing for compile-time construction of valid [`Region`] subtag. -/// -/// The macro will perform syntax canonicalization of the tag. -/// -/// # Examples -/// -/// ``` -/// use icu::locid::subtags::Region; -/// use icu::locid::macros::region; -/// -/// const CN: Region = region!("cn"); -/// -/// let cn: Region = "cn".parse() -/// .expect("Failed to parse region subtag."); -/// -/// assert_eq!(CN, "CN"); -/// assert_eq!(CN, cn); -/// ``` -/// -/// [`Region`]: icu_locid::subtags::Region -#[proc_macro] -pub fn region(input: TokenStream) -> TokenStream { - let val = get_value_from_token_stream(input); - - let parsed: icu_locid::subtags::Region = val.parse().expect("Malformed Region Subtag"); - - parsed - .into_token_stream() - .expect("Failed to parse token stream.") -} - -/// A macro allowing for compile-time construction of valid [`Variant`] subtag. -/// -/// The macro will perform syntax canonicalization of the tag. -/// -/// # Examples -/// -/// ``` -/// use icu::locid::subtags::Variant; -/// use icu::locid::macros::variant; -/// -/// const POSIX: Variant = variant!("Posix"); -/// -/// let posix: Variant = "Posix".parse() -/// .expect("Failed to parse variant subtag."); -/// -/// assert_eq!(POSIX, "posix"); -/// assert_eq!(POSIX, posix); -/// ``` -/// -/// [`Variant`]: icu_locid::subtags::Variant -#[proc_macro] -pub fn variant(input: TokenStream) -> TokenStream { - let val = get_value_from_token_stream(input); - - let parsed: icu_locid::subtags::Variant = val.parse().expect("Malformed Variant Subtag"); - - parsed - .into_token_stream() - .expect("Failed to parse token stream.") -} - -/// A macro allowing for compile-time construction of valid [`LanguageIdentifier`]. -/// -/// The macro will perform syntax canonicalization of the tag. -/// -/// # Examples -/// -/// ``` -/// use icu::locid::LanguageIdentifier; -/// use icu::locid::macros::langid; -/// -/// const DE_AT: LanguageIdentifier = langid!("de_at"); -/// -/// let de_at: LanguageIdentifier = "de_at".parse() -/// .expect("Failed to parse language identifier."); -/// -/// assert_eq!(DE_AT, "de-AT"); -/// assert_eq!(DE_AT, de_at); -/// ``` -/// -/// *Note*: As of Rust 1.47, the macro cannot produce language identifier -/// with variants in the const mode pending [`Heap Allocations in Constants`]. -/// -/// [`LanguageIdentifier`]: icu_locid::LanguageIdentifier -/// [`Heap Allocations in Constants`]: https://github.com/rust-lang/const-eval/issues/20 -#[proc_macro] -pub fn langid(input: TokenStream) -> TokenStream { - let val = get_value_from_token_stream(input); - - let langid: icu_locid::LanguageIdentifier = val.parse().expect("Malformed Language Identifier"); - - let lang = langid.language.into_token_stream_string(); - - let script = langid.script.into_token_stream_string(); - let region = langid.region.into_token_stream_string(); - - let variants = langid.variants.into_token_stream_string(); - - let output = format!( - r#" -{}::LanguageIdentifier {{ - language: {}, - script: {}, - region: {}, - variants: {}, -}} -"#, - get_crate_name(), - lang, - script, - region, - variants - ); - - output.parse().expect("Output should parse.") -} diff --git a/components/locid/macros/src/token_stream.rs b/components/locid/macros/src/token_stream.rs deleted file mode 100644 index 5dccd53b363..00000000000 --- a/components/locid/macros/src/token_stream.rs +++ /dev/null @@ -1,116 +0,0 @@ -// This file is part of ICU4X. For terms of use, please see the file -// called LICENSE at the top level of the ICU4X source tree -// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). - -use icu_locid::subtags; - -extern crate proc_macro; - -use super::get_crate_name; -use proc_macro::TokenStream; - -pub trait IntoTokenStream { - fn into_token_stream_string(self) -> String; - fn into_token_stream(self) -> Result - where - Self: Sized, - { - self.into_token_stream_string().parse() - } -} - -impl IntoTokenStream for u32 { - fn into_token_stream_string(self) -> String { - self.to_string() - } -} - -impl IntoTokenStream for u64 { - fn into_token_stream_string(self) -> String { - self.to_string() - } -} - -impl IntoTokenStream for subtags::Language { - fn into_token_stream_string(self) -> String { - format!( - "unsafe {{ {}::subtags::Language::from_raw_unchecked({}) }}", - get_crate_name(), - self.into_raw().into_token_stream_string() - ) - } -} - -impl IntoTokenStream for [u8; 4] { - fn into_token_stream_string(self) -> String { - format!("{:?}", self) - } -} - -impl IntoTokenStream for [u8; 8] { - fn into_token_stream_string(self) -> String { - format!("{:?}", self) - } -} - -impl IntoTokenStream for subtags::Script { - fn into_token_stream_string(self) -> String { - format!( - "unsafe {{ {}::subtags::Script::from_raw_unchecked({}) }}", - get_crate_name(), - self.into_raw().into_token_stream_string() - ) - } -} - -impl IntoTokenStream for subtags::Region { - fn into_token_stream_string(self) -> String { - format!( - "unsafe {{ {}::subtags::Region::from_raw_unchecked({}) }}", - get_crate_name(), - self.into_raw().into_token_stream_string() - ) - } -} - -impl IntoTokenStream for subtags::Variant { - fn into_token_stream_string(self) -> String { - format!( - "unsafe {{ {}::subtags::Variant::from_raw_unchecked({}) }}", - get_crate_name(), - self.into_raw().into_token_stream_string() - ) - } -} - -impl IntoTokenStream for subtags::Variants { - fn into_token_stream_string(self) -> String { - let variants = if let Some(variants) = self.into_raw() { - let v: Vec<_> = variants - .iter() - .map(|v| v.into_token_stream_string()) - .collect(); - format!("Some(Box::new([{}]))", v.join(", ")) - } else { - "None".to_string() - }; - format!( - "unsafe {{ {}::subtags::Variants::from_raw_unchecked({}) }}", - get_crate_name(), - variants - ) - } -} - -impl IntoTokenStream for Option -where - T: IntoTokenStream, -{ - fn into_token_stream_string(self) -> String { - if let Some(v) = self { - format!("Some({})", v.into_token_stream_string()) - } else { - "None".to_string() - } - } -} diff --git a/components/locid/macros/tests/macros.rs b/components/locid/macros/tests/macros.rs deleted file mode 100644 index 16bd9d4f1fe..00000000000 --- a/components/locid/macros/tests/macros.rs +++ /dev/null @@ -1,61 +0,0 @@ -// This file is part of ICU4X. For terms of use, please see the file -// called LICENSE at the top level of the ICU4X source tree -// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). - -use icu_locid::subtags; -use icu_locid::LanguageIdentifier; -use icu_locid_macros::*; - -const LANG_PL: subtags::Language = language!("pL"); -const SCRIPT_LATN: subtags::Script = script!("lAtN"); -const REGION_US: subtags::Region = region!("us"); -const VARIANT_MACOS: subtags::Variant = variant!("MACOS"); -const LANGID: LanguageIdentifier = langid!("de-Arab-AT"); - -#[test] -fn language() { - let lang = language!("Pl"); - - assert_eq!(lang, "pl"); - assert_eq!(LANG_PL, "pl"); - assert_eq!(lang, LANG_PL); -} - -#[test] -fn script() { - let script = script!("latn"); - - assert_eq!(script, "Latn"); - assert_eq!(SCRIPT_LATN, "Latn"); - assert_eq!(script, SCRIPT_LATN); -} - -#[test] -fn region() { - let region = region!("us"); - - assert_eq!(region, "US"); - assert_eq!(REGION_US, "US"); - assert_eq!(region, REGION_US); -} - -#[test] -fn variant() { - let variant = variant!("macOS"); - - assert_eq!(variant, "macos"); - assert_eq!(VARIANT_MACOS, "macos"); - assert_eq!(variant, VARIANT_MACOS); -} - -#[test] -fn langid() { - let langid = langid!("de_Arab_aT"); - - assert_eq!(langid.to_string(), "de-Arab-AT"); - assert_eq!(LANGID.to_string(), "de-Arab-AT"); - assert_eq!(langid, LANGID); - - let langid_with_variant = langid!("de_Arab_aT-macOS"); - assert_eq!(langid_with_variant.to_string(), "de-Arab-AT-macos"); -} diff --git a/components/locid/src/macros.rs b/components/locid/src/macros.rs index a21078ee235..a592725599c 100644 --- a/components/locid/src/macros.rs +++ b/components/locid/src/macros.rs @@ -7,7 +7,7 @@ //! # Examples //! //! ```rust -//! use icu_locid::macros::{language, region, langid}; +//! use icu_locid::{language, region, langid}; //! //! let lid = langid!("EN_US"); //! @@ -23,7 +23,7 @@ /// /// ``` /// use icu::locid::subtags::Language; -/// use icu::locid::macros::language; +/// use icu::locid::language; /// /// const DE: Language = language!("DE"); /// @@ -55,7 +55,7 @@ macro_rules! language { /// /// ``` /// use icu::locid::subtags::Script; -/// use icu::locid::macros::script; +/// use icu::locid::script; /// /// const ARAB: Script = script!("aRAB"); /// @@ -87,7 +87,7 @@ macro_rules! script { /// /// ``` /// use icu::locid::subtags::Region; -/// use icu::locid::macros::region; +/// use icu::locid::region; /// /// const CN: Region = region!("cn"); /// @@ -119,7 +119,7 @@ macro_rules! region { /// /// ``` /// use icu::locid::subtags::Variant; -/// use icu::locid::macros::variant; +/// use icu::locid::variant; /// /// const POSIX: Variant = variant!("Posix"); /// @@ -151,7 +151,7 @@ macro_rules! variant { /// /// ``` /// use icu::locid::LanguageIdentifier; -/// use icu::locid::macros::langid; +/// use icu::locid::langid; /// /// const DE_AT: LanguageIdentifier = langid!("de_at"); /// diff --git a/components/plurals/Cargo.toml b/components/plurals/Cargo.toml index df95898129e..4ecee92258f 100644 --- a/components/plurals/Cargo.toml +++ b/components/plurals/Cargo.toml @@ -46,7 +46,6 @@ icu = { path = "../icu", default-features = false } icu_benchmark_macros = { version = "0.5", path = "../../tools/benchmark/macros" } icu_provider = { version = "0.5", path = "../../provider/core" } icu_locid = { version = "0.5", path = "../locid" } -icu_locid_macros = { version = "0.5", path = "../locid/macros" } icu_testdata = { version = "0.5", path = "../../provider/testdata", features = ["static"] } serde = { version = "1.0", features = ["derive"] } serde_json = { version = "1.0" } diff --git a/components/plurals/README.md b/components/plurals/README.md index 19a4b95e937..60c202e53b8 100644 --- a/components/plurals/README.md +++ b/components/plurals/README.md @@ -22,7 +22,7 @@ appropriate [`Plural Category`]. ## Examples ```rust -use icu::locid::macros::langid; +use icu::locid::langid; use icu::plurals::{PluralRules, PluralRuleType, PluralCategory}; let lid = langid!("en"); diff --git a/components/plurals/benches/pluralrules.rs b/components/plurals/benches/pluralrules.rs index be0b905d655..92134fe44da 100644 --- a/components/plurals/benches/pluralrules.rs +++ b/components/plurals/benches/pluralrules.rs @@ -30,7 +30,7 @@ fn pluralrules(c: &mut Criterion) { #[cfg(feature = "bench")] { use criterion::black_box; - use icu_locid_macros::langid; + use icu_locid::langid; c.bench_function("plurals/pluralrules/construct/fs", |b| { b.iter(|| { diff --git a/components/plurals/examples/elevator_floors.rs b/components/plurals/examples/elevator_floors.rs index 177dbc6c58a..fe0819f6b36 100644 --- a/components/plurals/examples/elevator_floors.rs +++ b/components/plurals/examples/elevator_floors.rs @@ -9,7 +9,7 @@ icu_benchmark_macros::static_setup!(); -use icu_locid_macros::langid; +use icu_locid::langid; use icu_plurals::{PluralCategory, PluralRules}; const VALUES: &[usize] = &[0, 2, 25, 1, 3, 2, 4, 10, 7, 0]; diff --git a/components/plurals/examples/unread_emails.rs b/components/plurals/examples/unread_emails.rs index f767bab57f7..23ecf0a93c8 100644 --- a/components/plurals/examples/unread_emails.rs +++ b/components/plurals/examples/unread_emails.rs @@ -9,7 +9,7 @@ icu_benchmark_macros::static_setup!(); -use icu_locid_macros::langid; +use icu_locid::langid; use icu_plurals::{PluralCategory, PluralRules}; const VALUES: &[usize] = &[0, 2, 25, 1, 3, 2, 4, 10, 7, 0]; diff --git a/components/plurals/src/lib.rs b/components/plurals/src/lib.rs index 3dce56ba697..902a557a415 100644 --- a/components/plurals/src/lib.rs +++ b/components/plurals/src/lib.rs @@ -24,7 +24,7 @@ //! # Examples //! //! ``` -//! use icu::locid::macros::langid; +//! use icu::locid::langid; //! use icu::plurals::{PluralRules, PluralRuleType, PluralCategory}; //! //! let lid = langid!("en"); @@ -125,7 +125,7 @@ pub enum PluralRuleType { /// # Examples /// /// ``` -/// use icu::locid::macros::langid; +/// use icu::locid::langid; /// use icu::plurals::{PluralRules, PluralRuleType, PluralCategory}; /// use icu_provider::inv::InvariantDataProvider; /// @@ -254,7 +254,7 @@ impl PluralCategory { /// # Examples /// /// ``` -/// use icu::locid::macros::langid; +/// use icu::locid::langid; /// use icu::plurals::{PluralRules, PluralRuleType, PluralCategory}; /// use icu_provider::inv::InvariantDataProvider; /// @@ -284,7 +284,7 @@ impl PluralRules { /// # Examples /// /// ``` - /// use icu::locid::macros::langid; + /// use icu::locid::langid; /// use icu::plurals::{PluralRules, PluralRuleType}; /// use icu_provider::inv::InvariantDataProvider; /// @@ -324,7 +324,7 @@ impl PluralRules { /// # Examples /// /// ``` - /// use icu::locid::macros::langid; + /// use icu::locid::langid; /// use icu::plurals::{PluralRules, PluralCategory}; /// /// let lid = langid!("ru"); @@ -371,7 +371,7 @@ impl PluralRules { /// # Examples /// /// ``` - /// use icu::locid::macros::langid; + /// use icu::locid::langid; /// use icu::plurals::{PluralRules, PluralCategory}; /// /// let lid = langid!("ru"); @@ -411,7 +411,7 @@ impl PluralRules { /// # Examples /// /// ``` - /// use icu::locid::macros::langid; + /// use icu::locid::langid; /// use icu::plurals::{PluralRules, PluralRuleType, PluralCategory}; /// use icu_provider::inv::InvariantDataProvider; /// @@ -441,7 +441,7 @@ impl PluralRules { /// /// ``` /// use std::convert::TryFrom; - /// use icu::locid::macros::langid; + /// use icu::locid::langid; /// use icu::plurals::{PluralRules, PluralRuleType}; /// use icu::plurals::{PluralCategory, PluralOperands}; /// use icu_provider::inv::InvariantDataProvider; @@ -495,7 +495,7 @@ impl PluralRules { /// # Examples /// /// ``` - /// use icu::locid::macros::langid; + /// use icu::locid::langid; /// use icu::plurals::{PluralRules, PluralRuleType, PluralCategory}; /// use icu_provider::inv::InvariantDataProvider; /// diff --git a/components/plurals/tests/plurals.rs b/components/plurals/tests/plurals.rs index 9b65b2c8e27..7b29567c6a3 100644 --- a/components/plurals/tests/plurals.rs +++ b/components/plurals/tests/plurals.rs @@ -2,7 +2,7 @@ // called LICENSE at the top level of the ICU4X source tree // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). -use icu_locid_macros::langid; +use icu_locid::langid; use icu_plurals::{ provider::CardinalV1Marker, rules::runtime::ast::Rule, PluralCategory, PluralRuleType, PluralRules, diff --git a/docs/posts/20201015_ICU4X_Project_Announcement.md b/docs/posts/20201015_ICU4X_Project_Announcement.md index 6bad70b43d1..ebba6b90d4f 100644 --- a/docs/posts/20201015_ICU4X_Project_Announcement.md +++ b/docs/posts/20201015_ICU4X_Project_Announcement.md @@ -95,7 +95,7 @@ icu_provider_fs = "0.1" and then: ```rust -use icu::locid::macros::langid; +use icu::locid::langid; use icu::datetime::{DateTimeFormat, date::MockDateTime, options::length}; use icu_provider_fs::FsDataProvider; diff --git a/docs/tutorials/intro.md b/docs/tutorials/intro.md index ae0f6452d7b..ce82050bd2f 100644 --- a/docs/tutorials/intro.md +++ b/docs/tutorials/intro.md @@ -94,7 +94,7 @@ For that purpose, ICU4X provides a macro one can use to parse it at compilation ```rust use icu::locid::Locale; -use icu::locid::macros::langid; +use icu::locid::langid; fn main() { let loc: Locale = langid!("ES-AR").into(); @@ -190,7 +190,7 @@ fn main() { While this app doesn't do anything on its own yet, we now have a loaded data provider, and can use it to format a date: ```rust -use icu::locid::macros::langid; +use icu::locid::langid; use icu::locid::Locale; use icu::datetime::{DateTimeFormat, mock::datetime::MockDateTime, options::length}; use icu_provider_fs::FsDataProvider; diff --git a/experimental/list_formatter/Cargo.toml b/experimental/list_formatter/Cargo.toml index 14e9389620f..1540e97b86e 100644 --- a/experimental/list_formatter/Cargo.toml +++ b/experimental/list_formatter/Cargo.toml @@ -34,7 +34,6 @@ criterion = "0.3.3" serde_json = "1" postcard = { version = "0.7", features = ["use-std"] } icu_testdata = { version = "0.5", path = "../../provider/testdata", features = ["static"] } -icu_locid_macros = { version = "0.5", path = "../../components/locid/macros"} icu_benchmark_macros = { version = "0.5", path = "../../tools/benchmark/macros" } [lib] diff --git a/experimental/list_formatter/README.md b/experimental/list_formatter/README.md index 03f7ad9eba0..39d95bef16c 100644 --- a/experimental/list_formatter/README.md +++ b/experimental/list_formatter/README.md @@ -10,7 +10,7 @@ writeable::Writeable)s as lists in a locale-sensitive way. ```rust use icu_list::{ListFormatter, ListStyle}; use icu_locid::Locale; -use icu_locid_macros::langid; +use icu_locid::langid; use writeable::Writeable; let locale: Locale = langid!("es").into(); diff --git a/experimental/list_formatter/examples/and_list.rs b/experimental/list_formatter/examples/and_list.rs index a482db0bdb0..1e4296428b4 100644 --- a/experimental/list_formatter/examples/and_list.rs +++ b/experimental/list_formatter/examples/and_list.rs @@ -7,7 +7,7 @@ icu_benchmark_macros::static_setup!(); use icu_list::{ListFormatter, ListStyle}; -use icu_locid_macros::langid; +use icu_locid::langid; use writeable::Writeable; #[no_mangle] diff --git a/experimental/list_formatter/src/lib.rs b/experimental/list_formatter/src/lib.rs index 5a5c2f8aca9..f15eb410c36 100644 --- a/experimental/list_formatter/src/lib.rs +++ b/experimental/list_formatter/src/lib.rs @@ -14,7 +14,7 @@ //! ``` //! use icu_list::{ListFormatter, ListStyle}; //! use icu_locid::Locale; -//! use icu_locid_macros::langid; +//! use icu_locid::langid; //! use writeable::Writeable; //! //! let locale: Locale = langid!("es").into(); diff --git a/ffi/diplomat/Cargo.toml b/ffi/diplomat/Cargo.toml index d96da3e0d6e..6d401c1e0c8 100644 --- a/ffi/diplomat/Cargo.toml +++ b/ffi/diplomat/Cargo.toml @@ -62,7 +62,6 @@ fixed_decimal = { path = "../../utils/fixed_decimal", features = ["ryu"] } icu_decimal = { path = "../../components/decimal/" } icu_locale_canonicalizer = { path = "../../components/locale_canonicalizer" } icu_locid = { path = "../../components/locid" } -icu_locid_macros = { path = "../../components/locid/macros" } icu_plurals = { path = "../../components/plurals/" } icu_properties = { path = "../../components/properties/" } icu_provider = { path = "../../provider/core", features = ["serde"] } diff --git a/ffi/diplomat/src/locale.rs b/ffi/diplomat/src/locale.rs index 9770ede3ba9..7a84be57a39 100644 --- a/ffi/diplomat/src/locale.rs +++ b/ffi/diplomat/src/locale.rs @@ -34,12 +34,12 @@ pub mod ffi { /// Construct an [`ICU4XLocale`] for the English language. pub fn create_en() -> Box { - Box::new(ICU4XLocale(icu_locid_macros::langid!("en").into())) + Box::new(ICU4XLocale(icu_locid::langid!("en").into())) } /// Construct an [`ICU4XLocale`] for the Bangla language. pub fn create_bn() -> Box { - Box::new(ICU4XLocale(icu_locid_macros::langid!("bn").into())) + Box::new(ICU4XLocale(icu_locid::langid!("bn").into())) } /// Construct a default undefined [`ICU4XLocale`] "und". diff --git a/provider/blob/Cargo.toml b/provider/blob/Cargo.toml index b550b8b3864..b636bef8901 100644 --- a/provider/blob/Cargo.toml +++ b/provider/blob/Cargo.toml @@ -43,7 +43,6 @@ litemap = { version = "0.3.0", path = "../../utils/litemap/", optional = true } [dev-dependencies] icu = { version = "0.5", path = "../../components/icu" } icu_testdata = { version = "0.5", path = "../../provider/testdata", features = ["static"] } -icu_locid_macros = { version = "0.5", path = "../../components/locid/macros" } [lib] path = "src/lib.rs" diff --git a/provider/blob/examples/simple_static.rs b/provider/blob/examples/simple_static.rs index fccc69ba6c8..32eb9c67732 100644 --- a/provider/blob/examples/simple_static.rs +++ b/provider/blob/examples/simple_static.rs @@ -2,7 +2,7 @@ // called LICENSE at the top level of the ICU4X source tree // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). -use icu::locid::macros::langid; +use icu::locid::langid; use icu::plurals::{PluralCategory, PluralRuleType, PluralRules}; fn main() { diff --git a/provider/blob/src/blob_data_provider.rs b/provider/blob/src/blob_data_provider.rs index cb2d94f0c41..ce69c1f32f3 100644 --- a/provider/blob/src/blob_data_provider.rs +++ b/provider/blob/src/blob_data_provider.rs @@ -22,7 +22,7 @@ use zerovec::maps::{KeyError, ZeroMap2dBorrowed}; /// # Examples /// /// ``` -/// use icu_locid_macros::langid; +/// use icu_locid::langid; /// use icu_provider::prelude::*; /// use icu_provider::hello_world::*; /// use icu_provider_blob::BlobDataProvider; diff --git a/provider/blob/src/static_data_provider.rs b/provider/blob/src/static_data_provider.rs index aefa00b3eec..902a4a45d3d 100644 --- a/provider/blob/src/static_data_provider.rs +++ b/provider/blob/src/static_data_provider.rs @@ -26,7 +26,7 @@ use zerovec::maps::{KeyError, ZeroMap2dBorrowed}; /// use icu_provider::prelude::*; /// use icu_provider::hello_world::*; /// use icu_provider_blob::StaticDataProvider; -/// use icu_locid_macros::langid; +/// use icu_locid::langid; /// /// const HELLO_WORLD_BLOB: &[u8] = include_bytes!(concat!( /// env!("CARGO_MANIFEST_DIR"), @@ -76,7 +76,7 @@ impl StaticDataProvider { /// use icu_provider::prelude::*; /// use icu_provider::hello_world::*; /// use icu_provider_blob::StaticDataProvider; - /// use icu_locid_macros::langid; + /// use icu_locid::langid; /// /// let stub_provider = StaticDataProvider::new_empty(); /// diff --git a/provider/cldr/Cargo.toml b/provider/cldr/Cargo.toml index c5bc78eb1ad..69481cd5f2c 100644 --- a/provider/cldr/Cargo.toml +++ b/provider/cldr/Cargo.toml @@ -55,7 +55,6 @@ smallstr = { version = "0.2", features = ["serde"] } smallvec = "1.6" tinystr = { path = "../../utils/tinystr", version = "0.5.0", features = ["alloc", "serde", "zerovec"], default-features = false } displaydoc = { version = "0.2.3", default-features = false } -icu_locid_macros = { version = "0.5", path = "../../components/locid/macros" } icu_provider_uprops = { version = "0.5", path = "../../provider/uprops", features = ["log"] } icu_properties = { version = "0.5", path = "../../components/properties", features = ["provider_serde", "std"] } zerovec = { version = "0.6", path = "../../utils/zerovec"} @@ -69,7 +68,6 @@ log = { version = "0.4", optional = true } [dev-dependencies] mktemp = "0.4" -icu_locid_macros = { version = "0.5", path = "../../components/locid/macros" } icu_testdata = { version = "0.5", path = "../../provider/testdata" } writeable = { version = "0.3", path = "../../utils/writeable" } diff --git a/provider/cldr/src/transform/calendar/japanese.rs b/provider/cldr/src/transform/calendar/japanese.rs index 530da7c48e4..51c37925f29 100644 --- a/provider/cldr/src/transform/calendar/japanese.rs +++ b/provider/cldr/src/transform/calendar/japanese.rs @@ -7,7 +7,7 @@ use crate::error::Error; use crate::reader::open_reader; use crate::CldrPaths; use icu_calendar::provider::*; -use icu_locid_macros::langid; +use icu_locid::langid; use icu_provider::iter::IterableResourceProvider; use icu_provider::prelude::*; use litemap::LiteMap; diff --git a/provider/cldr/src/transform/datetime/patterns.rs b/provider/cldr/src/transform/datetime/patterns.rs index 04f28030b1e..0bdacf16dd8 100644 --- a/provider/cldr/src/transform/datetime/patterns.rs +++ b/provider/cldr/src/transform/datetime/patterns.rs @@ -250,7 +250,7 @@ impl From<&cldr_serde::ca::Dates> for DatePatternsV1<'_> { #[test] fn test_basic() { - use icu_locid_macros::langid; + use icu_locid::langid; let cldr_paths = crate::cldr_paths::for_test(); let provider = DatePatternsProvider::try_from(&cldr_paths as &dyn CldrPaths) @@ -273,7 +273,7 @@ fn test_basic() { #[test] fn test_with_numbering_system() { - use icu_locid_macros::langid; + use icu_locid::langid; let cldr_paths = crate::cldr_paths::for_test(); let provider = DatePatternsProvider::try_from(&cldr_paths as &dyn CldrPaths) diff --git a/provider/cldr/src/transform/datetime/skeletons.rs b/provider/cldr/src/transform/datetime/skeletons.rs index f0f64d6d7b3..ab788e145b2 100644 --- a/provider/cldr/src/transform/datetime/skeletons.rs +++ b/provider/cldr/src/transform/datetime/skeletons.rs @@ -137,7 +137,7 @@ impl From<&cldr_serde::ca::DateTimeFormats> for DateSkeletonPatternsV1<'_> { #[test] fn test_datetime_skeletons() { use icu_datetime::pattern::runtime::{Pattern, PluralPattern}; - use icu_locid_macros::langid; + use icu_locid::langid; use icu_plurals::PluralCategory; let cldr_paths = crate::cldr_paths::for_test(); diff --git a/provider/cldr/src/transform/datetime/symbols.rs b/provider/cldr/src/transform/datetime/symbols.rs index 10d415cdb58..6ae86bd8cf1 100644 --- a/provider/cldr/src/transform/datetime/symbols.rs +++ b/provider/cldr/src/transform/datetime/symbols.rs @@ -218,7 +218,7 @@ symbols_from!( #[test] fn test_basic() { - use icu_locid_macros::langid; + use icu_locid::langid; let cldr_paths = crate::cldr_paths::for_test(); let provider = DateSymbolsProvider::try_from(&cldr_paths as &dyn CldrPaths).unwrap(); @@ -245,7 +245,7 @@ fn test_basic() { #[test] fn unalias_contexts() { - use icu_locid_macros::langid; + use icu_locid::langid; let cldr_paths = crate::cldr_paths::for_test(); let provider = DateSymbolsProvider::try_from(&cldr_paths as &dyn CldrPaths).unwrap(); diff --git a/provider/cldr/src/transform/decimal/mod.rs b/provider/cldr/src/transform/decimal/mod.rs index fde165df1e9..f0b1c580513 100644 --- a/provider/cldr/src/transform/decimal/mod.rs +++ b/provider/cldr/src/transform/decimal/mod.rs @@ -182,7 +182,7 @@ impl TryFrom<&cldr_serde::numbers::Numbers> for DecimalSymbolsV1<'static> { #[test] fn test_basic() { - use icu_locid_macros::langid; + use icu_locid::langid; let cldr_paths = crate::cldr_paths::for_test(); let provider = NumbersProvider::try_from(&cldr_paths as &dyn CldrPaths).unwrap(); diff --git a/provider/cldr/src/transform/list/mod.rs b/provider/cldr/src/transform/list/mod.rs index b90d968fc1f..3a5f0aa3af4 100644 --- a/provider/cldr/src/transform/list/mod.rs +++ b/provider/cldr/src/transform/list/mod.rs @@ -7,7 +7,7 @@ use crate::error::Error; use crate::reader::{get_langid_subdirectories, get_langid_subdirectory, open_reader}; use crate::CldrPaths; use icu_list::provider::*; -use icu_locid_macros::langid; +use icu_locid::langid; use icu_provider::iter::IterableResourceProvider; use icu_provider::prelude::*; use std::path::PathBuf; @@ -171,7 +171,7 @@ impl>> IterableRes mod tests { use super::*; use icu_list::{ListFormatter, ListStyle}; - use icu_locid_macros::langid; + use icu_locid::langid; use writeable::assert_writeable_eq; macro_rules! test { diff --git a/provider/cldr/src/transform/locale_canonicalizer/likely_subtags.rs b/provider/cldr/src/transform/locale_canonicalizer/likely_subtags.rs index ff143f8bbf3..fd9ab42e268 100644 --- a/provider/cldr/src/transform/locale_canonicalizer/likely_subtags.rs +++ b/provider/cldr/src/transform/locale_canonicalizer/likely_subtags.rs @@ -139,7 +139,7 @@ impl From<&cldr_serde::likely_subtags::Resource> for LikelySubtagsV1 { #[test] fn test_basic() { - use icu_locid_macros::langid; + use icu_locid::langid; let cldr_paths = crate::cldr_paths::for_test(); let provider = LikelySubtagsProvider::try_from(&cldr_paths as &dyn CldrPaths).unwrap(); diff --git a/provider/cldr/src/transform/plurals/mod.rs b/provider/cldr/src/transform/plurals/mod.rs index fe5f7122646..cad3bf8176a 100644 --- a/provider/cldr/src/transform/plurals/mod.rs +++ b/provider/cldr/src/transform/plurals/mod.rs @@ -147,7 +147,7 @@ impl From<&cldr_serde::plurals::LocalePluralRules> for PluralRulesV1<'static> { #[test] fn test_basic() { - use icu_locid_macros::langid; + use icu_locid::langid; let cldr_paths = crate::cldr_paths::for_test(); let provider = PluralsProvider::try_from(&cldr_paths as &dyn CldrPaths).unwrap(); diff --git a/provider/cldr/src/transform/time_zones/mod.rs b/provider/cldr/src/transform/time_zones/mod.rs index 67d95343f0d..6e4c4bf8303 100644 --- a/provider/cldr/src/transform/time_zones/mod.rs +++ b/provider/cldr/src/transform/time_zones/mod.rs @@ -109,7 +109,7 @@ mod tests { #[test] fn basic_cldr_time_zones() { - use icu_locid_macros::langid; + use icu_locid::langid; let cldr_paths = crate::cldr_paths::for_test(); let provider = TimeZonesProvider::try_from(&cldr_paths as &dyn CldrPaths).unwrap(); diff --git a/provider/core/Cargo.toml b/provider/core/Cargo.toml index c997272e658..9151c39f745 100644 --- a/provider/core/Cargo.toml +++ b/provider/core/Cargo.toml @@ -72,4 +72,3 @@ postcard = { version = "0.7.0", default-features = false, optional = true } [dev-dependencies] serde_json = "1.0" static_assertions = "1.1" -icu_locid_macros = { version = "0.5", path = "../../components/locid/macros" } diff --git a/provider/core/src/buf.rs b/provider/core/src/buf.rs index ebf5819c8ed..8e877139955 100644 --- a/provider/core/src/buf.rs +++ b/provider/core/src/buf.rs @@ -26,7 +26,7 @@ impl DataMarker for BufferMarker { /// # #[cfg(feature = "deserialize_json")] { /// use icu_provider::prelude::*; /// use icu_provider::hello_world::*; -/// use icu_locid_macros::langid; +/// use icu_locid::langid; /// /// let buffer_provider = HelloWorldProvider::new_with_placeholder_data() /// .into_json_provider(); diff --git a/provider/core/src/data_provider.rs b/provider/core/src/data_provider.rs index 25c489e9c98..44d844294b7 100644 --- a/provider/core/src/data_provider.rs +++ b/provider/core/src/data_provider.rs @@ -45,7 +45,7 @@ impl DataRequest { /// /// ``` /// use icu_provider::prelude::*; - /// use icu_locid_macros::langid; + /// use icu_locid::langid; /// /// const FOO_BAR: ResourceKey = icu_provider::resource_key!("foo/bar@1"); /// diff --git a/provider/core/src/filter/impls.rs b/provider/core/src/filter/impls.rs index 992919dd4c7..6a31402d0de 100644 --- a/provider/core/src/filter/impls.rs +++ b/provider/core/src/filter/impls.rs @@ -26,7 +26,7 @@ where /// use icu_provider::filter::Filterable; /// use icu_provider::iter::*; /// use icu_locid::LanguageIdentifier; - /// use icu_locid_macros::{language, langid}; + /// use icu_locid::{language, langid}; /// /// let provider = HelloWorldProvider::new_with_placeholder_data() /// .filterable("Demo no-English filter") @@ -99,7 +99,7 @@ where /// use icu_provider::prelude::*; /// use icu_provider::hello_world::*; /// use icu_provider::filter::Filterable; - /// use icu_locid_macros::langid; + /// use icu_locid::langid; /// /// let allowlist = vec![langid!("de"), langid!("zh")]; /// let provider = HelloWorldProvider::new_with_placeholder_data() @@ -162,7 +162,7 @@ where /// use icu_provider::prelude::*; /// use icu_provider::hello_world::*; /// use icu_provider::filter::Filterable; - /// use icu_locid_macros::langid; + /// use icu_locid::langid; /// /// let provider = HelloWorldProvider::new_with_placeholder_data() /// .filterable("Demo require-langid filter") diff --git a/provider/core/src/filter/mod.rs b/provider/core/src/filter/mod.rs index 76702f91184..c6e1327e793 100644 --- a/provider/core/src/filter/mod.rs +++ b/provider/core/src/filter/mod.rs @@ -24,7 +24,7 @@ //! use icu_provider::prelude::*; //! use icu_provider::hello_world::*; //! use icu_provider::filter::Filterable; -//! use icu_locid_macros::language; +//! use icu_locid::language; //! //! // Only return German data from a HelloWorldProvider: //! HelloWorldProvider::new_with_placeholder_data() diff --git a/provider/core/src/fork/by_key.rs b/provider/core/src/fork/by_key.rs index a523a6ca189..7ec68140bdf 100644 --- a/provider/core/src/fork/by_key.rs +++ b/provider/core/src/fork/by_key.rs @@ -27,7 +27,7 @@ use alloc::vec::Vec; /// # #[cfg(feature = "deserialize_json")] { /// use icu_provider::prelude::*; /// use icu_provider::hello_world::*; -/// use icu_locid_macros::langid; +/// use icu_locid::langid; /// use icu_provider::fork::by_key::ForkByKeyProvider; /// /// struct DummyBufferProvider; @@ -64,7 +64,7 @@ use alloc::vec::Vec; /// # #[cfg(feature = "deserialize_json")] { /// use icu_provider::prelude::*; /// use icu_provider::hello_world::*; -/// use icu_locid_macros::{language, langid}; +/// use icu_locid::{language, langid}; /// use icu_provider::filter::Filterable; /// use icu_provider::fork::by_key::ForkByKeyProvider; /// @@ -190,7 +190,7 @@ where /// # #[cfg(feature = "deserialize_json")] { /// use icu_provider::prelude::*; /// use icu_provider::hello_world::*; -/// use icu_locid_macros::{language, langid}; +/// use icu_locid::{language, langid}; /// use icu_provider::filter::Filterable; /// use icu_provider::fork::by_key::MultiForkByKeyProvider; /// diff --git a/provider/core/src/hello_world.rs b/provider/core/src/hello_world.rs index f5eefb8101a..028bd1b47de 100644 --- a/provider/core/src/hello_world.rs +++ b/provider/core/src/hello_world.rs @@ -55,7 +55,7 @@ impl ResourceMarker for HelloWorldV1Marker { /// ``` /// use icu_provider::hello_world::*; /// use icu_provider::prelude::*; -/// use icu_locid_macros::langid; +/// use icu_locid::langid; /// /// let provider = HelloWorldProvider::new_with_placeholder_data(); /// @@ -196,22 +196,22 @@ fn test_iter() { assert_eq!( supported_langids, vec![ - icu_locid_macros::langid!("bn"), - icu_locid_macros::langid!("cs"), - icu_locid_macros::langid!("de"), - icu_locid_macros::langid!("el"), - icu_locid_macros::langid!("en"), - icu_locid_macros::langid!("eo"), - icu_locid_macros::langid!("fa"), - icu_locid_macros::langid!("fi"), - icu_locid_macros::langid!("is"), - icu_locid_macros::langid!("ja"), - icu_locid_macros::langid!("la"), - icu_locid_macros::langid!("pt"), - icu_locid_macros::langid!("ro"), - icu_locid_macros::langid!("ru"), - icu_locid_macros::langid!("vi"), - icu_locid_macros::langid!("zh") + icu_locid::langid!("bn"), + icu_locid::langid!("cs"), + icu_locid::langid!("de"), + icu_locid::langid!("el"), + icu_locid::langid!("en"), + icu_locid::langid!("eo"), + icu_locid::langid!("fa"), + icu_locid::langid!("fi"), + icu_locid::langid!("is"), + icu_locid::langid!("ja"), + icu_locid::langid!("la"), + icu_locid::langid!("pt"), + icu_locid::langid!("ro"), + icu_locid::langid!("ru"), + icu_locid::langid!("vi"), + icu_locid::langid!("zh") ] ); } diff --git a/provider/core/src/resource.rs b/provider/core/src/resource.rs index e02637b271c..6c262be37e9 100644 --- a/provider/core/src/resource.rs +++ b/provider/core/src/resource.rs @@ -488,7 +488,7 @@ mod tests { } fn get_options_test_cases() -> [OptionsTestCase; 3] { - use icu_locid_macros::langid; + use icu_locid::langid; [ OptionsTestCase { options: ResourceOptions { diff --git a/provider/fs/Cargo.toml b/provider/fs/Cargo.toml index 216073d0028..901b0844ca5 100644 --- a/provider/fs/Cargo.toml +++ b/provider/fs/Cargo.toml @@ -55,7 +55,6 @@ static_assertions = { version = "1.1", optional = true } [dev-dependencies] icu_benchmark_macros = { version = "0.5", path = "../../tools/benchmark/macros" } -icu_locid_macros = { version = "0.5", path = "../../components/locid/macros" } icu_plurals = { version = "0.5", path = "../../components/plurals" } serde-json-core = { version = "0.4", features = ["std"] } criterion = "0.3.3" diff --git a/provider/fs/benches/provider_fs.rs b/provider/fs/benches/provider_fs.rs index 0891903bcbe..9a0af2a3bb0 100644 --- a/provider/fs/benches/provider_fs.rs +++ b/provider/fs/benches/provider_fs.rs @@ -4,7 +4,7 @@ use criterion::{black_box, criterion_group, criterion_main, Criterion}; -use icu_locid_macros::langid; +use icu_locid::langid; use icu_plurals::provider::*; use icu_provider::prelude::*; use icu_provider_fs::FsDataProvider; diff --git a/provider/fs/tests/test_file_io.rs b/provider/fs/tests/test_file_io.rs index 5092e25cac9..bcca70ab945 100644 --- a/provider/fs/tests/test_file_io.rs +++ b/provider/fs/tests/test_file_io.rs @@ -4,8 +4,8 @@ #![allow(unused_imports)] +use icu_locid::langid; use icu_locid::LanguageIdentifier; -use icu_locid_macros::langid; use icu_plurals::{provider::*, rules::runtime::ast::Rule}; use icu_provider::prelude::*; use icu_provider::serde::*; diff --git a/provider/testdata/Cargo.toml b/provider/testdata/Cargo.toml index 3251d880266..47bc7652670 100644 --- a/provider/testdata/Cargo.toml +++ b/provider/testdata/Cargo.toml @@ -151,7 +151,6 @@ displaydoc = { version = "0.2.3", default-features = false, optional = true } writeable = { version = "0.3", path = "../../utils/writeable", optional = true } [dev-dependencies] -icu_locid_macros = { version = "0.5", path = "../../components/locid/macros" } icu_plurals = { version = "0.5", path = "../../components/plurals" } [features] diff --git a/provider/testdata/README.md b/provider/testdata/README.md index 0c71ec2a162..5de8675e311 100644 --- a/provider/testdata/README.md +++ b/provider/testdata/README.md @@ -40,7 +40,7 @@ The following commands are also available: ```rust use std::borrow::Cow; use icu_provider::prelude::*; -use icu_locid_macros::langid; +use icu_locid::langid; let data_provider = icu_testdata::get_provider(); diff --git a/provider/testdata/src/lib.rs b/provider/testdata/src/lib.rs index 408780f5ce5..51fbf5cff74 100644 --- a/provider/testdata/src/lib.rs +++ b/provider/testdata/src/lib.rs @@ -42,7 +42,7 @@ //! ``` //! use std::borrow::Cow; //! use icu_provider::prelude::*; -//! use icu_locid_macros::langid; +//! use icu_locid::langid; //! //! let data_provider = icu_testdata::get_provider(); //! diff --git a/utils/litemap/Cargo.toml b/utils/litemap/Cargo.toml index 4492d2ceb56..4d0ba6f9b8b 100644 --- a/utils/litemap/Cargo.toml +++ b/utils/litemap/Cargo.toml @@ -39,7 +39,6 @@ bytecheck = "0.6" bincode = "1" postcard = { version = "0.7", features = ["use-std"] } icu_locid = { version = "0.5", path = "../../components/locid" } -icu_locid_macros = { version = "0.5", path = "../../components/locid/macros" } icu_benchmark_macros = { version = "0.5", path = "../../tools/benchmark/macros" } criterion = "0.3.4" diff --git a/utils/litemap/examples/language_names_hash_map.rs b/utils/litemap/examples/language_names_hash_map.rs index 6778b5f05d3..910071d601c 100644 --- a/utils/litemap/examples/language_names_hash_map.rs +++ b/utils/litemap/examples/language_names_hash_map.rs @@ -11,8 +11,8 @@ icu_benchmark_macros::static_setup!(); +use icu_locid::language; use icu_locid::subtags::Language; -use icu_locid_macros::language; use std::collections::HashMap; const DATA: [(Language, &str); 11] = [ diff --git a/utils/litemap/examples/language_names_lite_map.rs b/utils/litemap/examples/language_names_lite_map.rs index 72f2769140a..05aff2b5f3a 100644 --- a/utils/litemap/examples/language_names_lite_map.rs +++ b/utils/litemap/examples/language_names_lite_map.rs @@ -11,8 +11,8 @@ icu_benchmark_macros::static_setup!(); +use icu_locid::language; use icu_locid::subtags::Language; -use icu_locid_macros::language; use litemap::LiteMap; const DATA: [(Language, &str); 11] = [ From 9c67c6929a63e9e6ab365e1e068894d089028ba9 Mon Sep 17 00:00:00 2001 From: Robert Bastian Date: Wed, 23 Feb 2022 18:00:28 +0100 Subject: [PATCH 03/13] clippy --- components/locid/src/extensions/mod.rs | 3 +-- components/locid/src/extensions/other/mod.rs | 6 +----- components/locid/src/extensions/private/mod.rs | 2 +- components/locid/src/extensions/transform/mod.rs | 2 +- components/locid/src/extensions/unicode/mod.rs | 4 +--- components/locid/src/parser/langid.rs | 10 ++++------ components/locid/src/parser/mod.rs | 6 ++++-- 7 files changed, 13 insertions(+), 20 deletions(-) diff --git a/components/locid/src/extensions/mod.rs b/components/locid/src/extensions/mod.rs index 0272ab56b7a..047e5f6ce95 100644 --- a/components/locid/src/extensions/mod.rs +++ b/components/locid/src/extensions/mod.rs @@ -56,7 +56,6 @@ pub use transform::Transform; pub use unicode::Unicode; use alloc::vec::Vec; -use core::iter::Peekable; use crate::parser::ParserError; use crate::parser::SubtagIterator; @@ -136,7 +135,7 @@ impl Extensions { && self.other.is_empty() } - pub(crate) fn try_from_iter<'a>(iter: &mut SubtagIterator<'a>) -> Result { + pub(crate) fn try_from_iter(iter: &mut SubtagIterator) -> Result { let mut unicode = None; let mut transform = None; let mut private = None; diff --git a/components/locid/src/extensions/other/mod.rs b/components/locid/src/extensions/other/mod.rs index 99127297cbf..d82243356d7 100644 --- a/components/locid/src/extensions/other/mod.rs +++ b/components/locid/src/extensions/other/mod.rs @@ -27,7 +27,6 @@ use alloc::boxed::Box; use crate::parser::ParserError; use crate::parser::SubtagIterator; use alloc::vec::Vec; -use core::iter::Peekable; pub use key::Key; /// A list of [`Other Use Extensions`] as defined in [`Unicode Locale @@ -76,10 +75,7 @@ impl Other { Self((ext, input.into_boxed_slice())) } - pub(crate) fn try_from_iter<'a>( - ext: u8, - iter: &mut SubtagIterator<'a>, - ) -> Result { + pub(crate) fn try_from_iter(ext: u8, iter: &mut SubtagIterator) -> Result { debug_assert!(ext.is_ascii_alphabetic()); let mut keys = Vec::new(); diff --git a/components/locid/src/extensions/private/mod.rs b/components/locid/src/extensions/private/mod.rs index ecab691735b..8ddfe031dc8 100644 --- a/components/locid/src/extensions/private/mod.rs +++ b/components/locid/src/extensions/private/mod.rs @@ -125,7 +125,7 @@ impl Private { self.0 = None; } - pub(crate) fn try_from_iter<'a>(iter: &mut SubtagIterator<'a>) -> Result { + pub(crate) fn try_from_iter(iter: &mut SubtagIterator) -> Result { let keys = iter.map(Key::from_bytes).collect::, _>>()?; Ok(Self::from_vec_unchecked(keys)) diff --git a/components/locid/src/extensions/transform/mod.rs b/components/locid/src/extensions/transform/mod.rs index e4f0f8a6f1b..c21b21dfe33 100644 --- a/components/locid/src/extensions/transform/mod.rs +++ b/components/locid/src/extensions/transform/mod.rs @@ -117,7 +117,7 @@ impl Transform { self.lang.is_none() && self.fields.is_empty() } - pub(crate) fn try_from_iter<'a>(iter: &mut SubtagIterator<'a>) -> Result { + pub(crate) fn try_from_iter(iter: &mut SubtagIterator) -> Result { let mut tlang = None; let mut tfields = vec![]; diff --git a/components/locid/src/extensions/unicode/mod.rs b/components/locid/src/extensions/unicode/mod.rs index 90894bc4808..8785be1997c 100644 --- a/components/locid/src/extensions/unicode/mod.rs +++ b/components/locid/src/extensions/unicode/mod.rs @@ -45,8 +45,6 @@ pub use value::Value; use crate::parser::ParserError; use crate::parser::SubtagIterator; -use core::iter::Peekable; - /// Unicode Extensions provide information about user preferences in a given locale. /// /// A list of [`Unicode BCP47 U Extensions`] as defined in [`Unicode Locale @@ -115,7 +113,7 @@ impl Unicode { self.keywords.is_empty() && self.attributes.is_empty() } - pub(crate) fn try_from_iter<'a>(iter: &mut SubtagIterator<'a>) -> Result { + pub(crate) fn try_from_iter(iter: &mut SubtagIterator) -> Result { let mut attributes = vec![]; let mut keywords = vec![]; diff --git a/components/locid/src/parser/langid.rs b/components/locid/src/parser/langid.rs index 2bac66d4dd3..28ef19e91d0 100644 --- a/components/locid/src/parser/langid.rs +++ b/components/locid/src/parser/langid.rs @@ -2,8 +2,6 @@ // called LICENSE at the top level of the ICU4X source tree // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). -use core::iter::Peekable; - pub use super::errors::ParserError; use crate::parser::{get_subtag_iterator, SubtagIterator}; use crate::subtags; @@ -24,8 +22,8 @@ enum ParserPosition { Variant, } -pub fn parse_language_identifier_from_iter<'a>( - iter: &mut SubtagIterator<'a>, +pub fn parse_language_identifier_from_iter( + iter: &mut SubtagIterator, mode: ParserMode, ) -> Result { let language; @@ -107,8 +105,8 @@ pub fn parse_language_identifier( parse_language_identifier_from_iter(&mut iter, mode) } -pub const fn parse_language_identifier_without_variants_from_iter<'a>( - mut iter: SubtagIterator<'a>, +pub const fn parse_language_identifier_without_variants_from_iter( + mut iter: SubtagIterator, mode: ParserMode, ) -> Result< ( diff --git a/components/locid/src/parser/mod.rs b/components/locid/src/parser/mod.rs index bc5dd73cfb7..5a56a0e639d 100644 --- a/components/locid/src/parser/mod.rs +++ b/components/locid/src/parser/mod.rs @@ -36,8 +36,10 @@ pub struct SubtagIterator<'a> { r_cursor: usize, } +pub type ManualSlice<'a> = (&'a [u8], usize, usize); + impl<'a> SubtagIterator<'a> { - pub const fn next_manual(mut self) -> (Self, Option<(&'a [u8], usize, usize)>) { + pub const fn next_manual(mut self) -> (Self, Option>) { if self.l_cursor == self.r_cursor { (self, None) } else { @@ -59,7 +61,7 @@ impl<'a> SubtagIterator<'a> { } } - pub const fn peek_manual(&self) -> Option<(&'a [u8], usize, usize)> { + pub const fn peek_manual(&self) -> Option> { if self.l_cursor == self.r_cursor { None } else { From 2c0ccd0de26bcc38a99059c820e614905cc419c4 Mon Sep 17 00:00:00 2001 From: Robert Bastian Date: Wed, 23 Feb 2022 18:04:01 +0100 Subject: [PATCH 04/13] doc --- components/locid/src/macros.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/components/locid/src/macros.rs b/components/locid/src/macros.rs index a592725599c..1d747939916 100644 --- a/components/locid/src/macros.rs +++ b/components/locid/src/macros.rs @@ -2,12 +2,12 @@ // called LICENSE at the top level of the ICU4X source tree // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). -//! This module provides convenience macros for [`icu_locid`]. +//! This module provides convenience macros. //! //! # Examples //! //! ```rust -//! use icu_locid::{language, region, langid}; +//! use crate::{language, region, langid}; //! //! let lid = langid!("EN_US"); //! @@ -34,7 +34,7 @@ /// assert_eq!(DE, de); /// ``` /// -/// [`Language`]: icu_locid::subtags::Language +/// [`Language`]: crate::subtags::Language #[macro_export] macro_rules! language { ($language:literal) => {{ @@ -66,7 +66,7 @@ macro_rules! language { /// assert_eq!(ARAB, arab); /// ``` /// -/// [`Script`]: icu_locid::subtags::Script +/// [`Script`]: crate::subtags::Script #[macro_export] macro_rules! script { ($script:literal) => {{ @@ -98,7 +98,7 @@ macro_rules! script { /// assert_eq!(CN, cn); /// ``` /// -/// [`Region`]: icu_locid::subtags::Region +/// [`Region`]: crate::subtags::Region #[macro_export] macro_rules! region { ($region:literal) => {{ @@ -130,7 +130,7 @@ macro_rules! region { /// assert_eq!(POSIX, posix); /// ``` /// -/// [`Variant`]: icu_locid::subtags::Variant +/// [`Variant`]: crate::subtags::Variant #[macro_export] macro_rules! variant { ($variant:literal) => {{ @@ -165,7 +165,7 @@ macro_rules! variant { /// *Note*: As of Rust 1.47, the macro cannot produce language identifier /// with variants in the const mode pending [`Heap Allocations in Constants`]. /// -/// [`LanguageIdentifier`]: icu_locid::LanguageIdentifier +/// [`LanguageIdentifier`]: crate::LanguageIdentifier /// [`Heap Allocations in Constants`]: https://github.com/rust-lang/const-eval/issues/20 #[macro_export] macro_rules! langid { From dd9516f592009202255ef06f42af9b84aa0c49be Mon Sep 17 00:00:00 2001 From: Robert Bastian Date: Thu, 24 Feb 2022 13:46:50 +0100 Subject: [PATCH 05/13] doc --- components/locid/src/macros.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/locid/src/macros.rs b/components/locid/src/macros.rs index 1d747939916..26608b2cc11 100644 --- a/components/locid/src/macros.rs +++ b/components/locid/src/macros.rs @@ -7,7 +7,7 @@ //! # Examples //! //! ```rust -//! use crate::{language, region, langid}; +//! use icu::locid::{language, region, langid}; //! //! let lid = langid!("EN_US"); //! From 10a588a704b438a331c2ce1cb2e5a4a81d239c29 Mon Sep 17 00:00:00 2001 From: Robert Bastian Date: Tue, 15 Mar 2022 22:54:12 +0100 Subject: [PATCH 06/13] fix --- components/icu/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/icu/src/lib.rs b/components/icu/src/lib.rs index c1993ece701..0275db8d69b 100644 --- a/components/icu/src/lib.rs +++ b/components/icu/src/lib.rs @@ -485,7 +485,7 @@ pub mod list { //! ``` //! use icu_list::{ListFormatter, ListStyle}; //! use icu_locid::Locale; - //! use icu_locid_macros::langid; + //! use icu_locid::langid; //! use writeable::Writeable; //! //! let locale: Locale = langid!("es").into(); From b20b35ad181a4eb7a4ae286d553e20051d211974 Mon Sep 17 00:00:00 2001 From: Robert Bastian Date: Wed, 16 Mar 2022 07:11:57 +0100 Subject: [PATCH 07/13] comments --- components/locid/src/langid.rs | 4 +- components/locid/src/parser/mod.rs | 55 +++++++++++++----------- components/locid/src/subtags/language.rs | 26 ++++------- components/locid/src/subtags/region.rs | 17 ++++---- components/locid/src/subtags/script.rs | 11 ++--- components/locid/src/subtags/variant.rs | 14 ++---- 6 files changed, 56 insertions(+), 71 deletions(-) diff --git a/components/locid/src/langid.rs b/components/locid/src/langid.rs index b6315fd5fd5..becb3879ee8 100644 --- a/components/locid/src/langid.rs +++ b/components/locid/src/langid.rs @@ -89,8 +89,8 @@ impl LanguageIdentifier { } #[doc(hidden)] - // We cannot return `Result`, as it's currently impossible to unwrap non-Copy - // types in a const context. See [rust-lang#73255](https://github.com/rust-lang/rust/issues/73255). + // The return type should be `Result` once the `const_precise_live_drops` + // is stabilized ([rust-lang#73255](https://github.com/rust-lang/rust/issues/73255)). pub const fn from_bytes_without_variants( v: &[u8], ) -> Result< diff --git a/components/locid/src/parser/mod.rs b/components/locid/src/parser/mod.rs index 5a56a0e639d..0510bbcfadb 100644 --- a/components/locid/src/parser/mod.rs +++ b/components/locid/src/parser/mod.rs @@ -13,59 +13,62 @@ pub use langid::{ }; pub use locale::parse_locale; -pub const fn get_subtag_iterator(t: &[u8]) -> SubtagIterator { - let mut l_cursor = 0; - while l_cursor < t.len() && (t[l_cursor] == b'_' || t[l_cursor] == b'-') { - l_cursor += 1; +pub const fn get_subtag_iterator(slice: &[u8]) -> SubtagIterator { + let mut current_start = 0; + while current_start < slice.len() + && (slice[current_start] == b'_' || slice[current_start] == b'-') + { + current_start += 1; } - let mut r_cursor = l_cursor; - while r_cursor < t.len() && t[r_cursor] != b'_' && t[r_cursor] != b'-' { - r_cursor += 1; + let mut current_end = current_start; + while current_end < slice.len() && slice[current_end] != b'_' && slice[current_end] != b'-' { + current_end += 1; } SubtagIterator { - t, - l_cursor, - r_cursor, + slice, + current_start, + current_end, } } #[derive(Copy, Clone, Debug)] pub struct SubtagIterator<'a> { - t: &'a [u8], - l_cursor: usize, - r_cursor: usize, + slice: &'a [u8], + current_start: usize, + current_end: usize, } pub type ManualSlice<'a> = (&'a [u8], usize, usize); impl<'a> SubtagIterator<'a> { pub const fn next_manual(mut self) -> (Self, Option>) { - if self.l_cursor == self.r_cursor { + if self.current_start == self.current_end { (self, None) } else { - let r = (self.t, self.l_cursor, self.r_cursor); - self.l_cursor = self.r_cursor; - while self.l_cursor < self.t.len() - && (self.t[self.l_cursor] == b'_' || self.t[self.l_cursor] == b'-') + let r = (self.slice, self.current_start, self.current_end); + self.current_start = self.current_end; + while self.current_start < self.slice.len() + && (self.slice[self.current_start] == b'_' + || self.slice[self.current_start] == b'-') { - self.l_cursor += 1; + self.current_start += 1; } - self.r_cursor = self.l_cursor; - while self.r_cursor < self.t.len() - && self.t[self.r_cursor] != b'_' - && self.t[self.r_cursor] != b'-' + self.current_end = self.current_start; + while self.current_end < self.slice.len() + && self.slice[self.current_end] != b'_' + && self.slice[self.current_end] != b'-' { - self.r_cursor += 1; + self.current_end += 1; } (self, Some(r)) } } pub const fn peek_manual(&self) -> Option> { - if self.l_cursor == self.r_cursor { + if self.current_start == self.current_end { None } else { - Some((self.t, self.l_cursor, self.r_cursor)) + Some((self.slice, self.current_start, self.current_end)) } } diff --git a/components/locid/src/subtags/language.rs b/components/locid/src/subtags/language.rs index 84a7bc8347b..9e24abdb42b 100644 --- a/components/locid/src/subtags/language.rs +++ b/components/locid/src/subtags/language.rs @@ -75,25 +75,17 @@ impl Language { return Err(ParserError::InvalidLanguage); } - let s = match TinyAsciiStr::from_bytes_manual_slice(v, start, end) { - Ok(s) => s, - _ => return Err(ParserError::InvalidLanguage), - }; - - if !s.is_ascii_alphabetic() { - return Err(ParserError::InvalidLanguage); - } - - let value = s.to_ascii_lowercase(); - if slen == 3 - && value.all_bytes()[0] == UND_VALUE.all_bytes()[0] - && value.all_bytes()[1] == UND_VALUE.all_bytes()[1] - && value.all_bytes()[2] == UND_VALUE.all_bytes()[2] + && v[start] == UND_VALUE.all_bytes()[0] + && v[start + 1] == UND_VALUE.all_bytes()[1] + && v[start + 2] == UND_VALUE.all_bytes()[2] { - Ok(Self(None)) - } else { - Ok(Self(Some(value))) + return Ok(Self(None)); + } + + match TinyAsciiStr::from_bytes_manual_slice(v, start, end) { + Ok(s) if s.is_ascii_alphabetic() => Ok(Self(Some(s.to_ascii_lowercase()))), + _ => Err(ParserError::InvalidLanguage), } } diff --git a/components/locid/src/subtags/region.rs b/components/locid/src/subtags/region.rs index eafff0210ee..033558912f1 100644 --- a/components/locid/src/subtags/region.rs +++ b/components/locid/src/subtags/region.rs @@ -53,14 +53,15 @@ impl Region { start: usize, end: usize, ) -> Result { - match ( - end - start, - TinyAsciiStr::from_bytes_manual_slice(v, start, end), - ) { - (REGION_ALPHA_LENGTH, Ok(s)) if s.is_ascii_alphabetic() => { - Ok(Self(s.to_ascii_uppercase())) - } - (REGION_NUM_LENGTH, Ok(s)) if s.is_ascii_numeric() => Ok(Self(s)), + match end - start { + REGION_ALPHA_LENGTH => match TinyAsciiStr::from_bytes_manual_slice(v, start, end) { + Ok(s) if s.is_ascii_alphabetic() => Ok(Self(s.to_ascii_uppercase())), + _ => Err(ParserError::InvalidSubtag), + }, + REGION_NUM_LENGTH => match TinyAsciiStr::from_bytes_manual_slice(v, start, end) { + Ok(s) if s.is_ascii_numeric() => Ok(Self(s)), + _ => Err(ParserError::InvalidSubtag), + }, _ => Err(ParserError::InvalidSubtag), } } diff --git a/components/locid/src/subtags/script.rs b/components/locid/src/subtags/script.rs index 1a992ad9107..990754db872 100644 --- a/components/locid/src/subtags/script.rs +++ b/components/locid/src/subtags/script.rs @@ -56,15 +56,10 @@ impl Script { return Err(ParserError::InvalidSubtag); } - let s = match TinyAsciiStr::from_bytes_manual_slice(v, start, end) { - Ok(s) => s, - _ => return Err(ParserError::InvalidSubtag), - }; - - if !s.is_ascii_alphabetic() { - return Err(ParserError::InvalidSubtag); + match TinyAsciiStr::from_bytes_manual_slice(v, start, end) { + Ok(s) if s.is_ascii_alphabetic() => Ok(Self(s.to_ascii_titlecase())), + _ => Err(ParserError::InvalidSubtag), } - Ok(Self(s.to_ascii_titlecase())) } /// Safely creates a [`Script`] from a reference to its raw format diff --git a/components/locid/src/subtags/variant.rs b/components/locid/src/subtags/variant.rs index f8210d07e86..cc56d92d4b1 100644 --- a/components/locid/src/subtags/variant.rs +++ b/components/locid/src/subtags/variant.rs @@ -60,20 +60,14 @@ impl Variant { return Err(ParserError::InvalidSubtag); } - let s = match TinyAsciiStr::from_bytes_manual_slice(v, start, end) { - Ok(s) => s, - _ => return Err(ParserError::InvalidSubtag), - }; - - if !s.is_ascii_alphanumeric() { - return Err(ParserError::InvalidSubtag); - } - if slen == VARIANT_NUM_ALPHA_LENGTH && !v[start].is_ascii_digit() { return Err(ParserError::InvalidSubtag); } - Ok(Self(s.to_ascii_lowercase())) + match TinyAsciiStr::from_bytes_manual_slice(v, start, end) { + Ok(s) if s.is_ascii_alphanumeric() => Ok(Self(s.to_ascii_lowercase())), + _ => Err(ParserError::InvalidSubtag), + } } /// Safely creates a [`Variant`] from a reference to its raw format From 82b2872d7671fd3481d21280b40d06b31e56a21e Mon Sep 17 00:00:00 2001 From: Robert Bastian Date: Wed, 16 Mar 2022 09:30:50 +0100 Subject: [PATCH 08/13] macro --- components/locid/src/subtags/language.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/locid/src/subtags/language.rs b/components/locid/src/subtags/language.rs index ef9bb53a1cc..d62e36de553 100644 --- a/components/locid/src/subtags/language.rs +++ b/components/locid/src/subtags/language.rs @@ -44,7 +44,7 @@ pub struct Language(TinyAsciiStr<{ *LANGUAGE_LENGTH.end() }>); const LANGUAGE_LENGTH: RangeInclusive = 2..=3; // TODO(#348): Change this to invoke a const function. // Safe because "und" is a valid language subtag -const UND: Language = Language(unsafe { TinyAsciiStr::from_bytes_unchecked(*b"und") }); +const UND: Language = crate::language!("und"); impl Language { /// A constructor which takes a utf8 slice, parses it and From a3f6912d0b6d408dc2a31f49a45f5ac849b7b972 Mon Sep 17 00:00:00 2001 From: Robert Bastian Date: Mon, 21 Mar 2022 18:57:10 -0700 Subject: [PATCH 09/13] doc --- components/locid/src/subtags/region.rs | 2 +- components/locid/src/subtags/script.rs | 2 +- components/locid/src/subtags/variant.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/components/locid/src/subtags/region.rs b/components/locid/src/subtags/region.rs index 033558912f1..40b6b833fb2 100644 --- a/components/locid/src/subtags/region.rs +++ b/components/locid/src/subtags/region.rs @@ -237,7 +237,7 @@ unsafe impl zerovec::ule::ULE for Region { /// /// ``` /// use icu::locid::subtags::Region; -/// use icu::locid::macros::region; +/// use icu::locid::region; /// use zerovec::ZeroVec; /// /// let zv = ZeroVec::::parse_byte_slice(b"GB\0419001DE\0") diff --git a/components/locid/src/subtags/script.rs b/components/locid/src/subtags/script.rs index 990754db872..754bba4175c 100644 --- a/components/locid/src/subtags/script.rs +++ b/components/locid/src/subtags/script.rs @@ -216,7 +216,7 @@ unsafe impl zerovec::ule::ULE for Script { /// /// ``` /// use icu::locid::subtags::Script; -/// use icu::locid::macros::script; +/// use icu::locid::script; /// use zerovec::ZeroVec; /// /// let zv = ZeroVec::