Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add icu_preferences util #1833

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ members = [
"utils/ixdtf",
"utils/litemap",
"utils/pattern",
"utils/preferences",
"utils/resb",
"utils/tinystr",
"utils/tzif",
Expand Down
2 changes: 2 additions & 0 deletions components/locale_core/src/extensions/unicode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ mod attribute;
mod attributes;
mod key;
mod keywords;
mod subdivision;
mod value;

use core::cmp::Ordering;
Expand All @@ -41,6 +42,7 @@ pub use attributes::Attributes;
#[doc(inline)]
pub use key::{key, Key};
pub use keywords::Keywords;
pub use subdivision::{subdivision_suffix, SubdivisionId, SubdivisionSuffix};
#[doc(inline)]
pub use value::{value, Value};

Expand Down
114 changes: 114 additions & 0 deletions components/locale_core/src/extensions/unicode/subdivision.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// 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 core::str::FromStr;

use crate::parser::ParserError;
use crate::subtags::Region;

impl_tinystr_subtag!(
/// An subdivision suffix used in a set of [`SubdivisionId`].
///
/// An subdivision suffix has to be a sequence of alphanumerical characters no
/// shorter than one and no longer than four characters.
///
///
/// # Examples
///
/// ```
/// use icu::locid::extensions::unicode::{subdivision_suffix, SubdivisionSuffix};
///
/// let ss: SubdivisionSuffix =
/// "sct".parse().expect("Failed to parse a SubdivisionSuffix.");
///
/// assert_eq!(ss, subdivision_suffix!("sct"));
/// ```
SubdivisionSuffix,
extensions::unicode,
subdivision_suffix,
extensions_unicode_subdivision_suffix,
1..=4,
s,
s.is_ascii_alphanumeric(),
s.to_ascii_lowercase(),
s.is_ascii_alphanumeric() && s.is_ascii_lowercase(),
InvalidExtension,
["sct"],
["toolooong"],
);

/// A SubDivision Id as defined in [`Unicode Locale Identifier`].
///
/// [`Unicode Locale Identifier`]: https://unicode.org/reports/tr35/tr35.html#unicode_subdivision_id
///
/// # Examples
///
/// ```
/// ```
#[derive(Debug, PartialEq, Eq, Clone, Hash, PartialOrd, Ord)]
#[non_exhaustive]
pub struct SubdivisionId {
#[doc(hidden)]
pub region: Region,
#[doc(hidden)]
pub suffix: SubdivisionSuffix,
}

impl SubdivisionId {
#[doc(hidden)]
pub const fn new(region: Region, suffix: SubdivisionSuffix) -> Self {
Self { region, suffix }
}

#[doc(hidden)]
pub fn try_from_bytes(input: &[u8]) -> Result<Self, ParserError> {
let is_alpha = if let Some(b) = input.first() {
if b.is_ascii_digit() {
false
} else if b.is_ascii_alphabetic() {
true
} else {
return Err(ParserError::InvalidExtension);
}
} else {
return Err(ParserError::InvalidExtension);
};
let region_len = if is_alpha { 2 } else { 3 };
if input.len() < region_len + 1 {
return Err(ParserError::InvalidExtension);
}
let (region_bytes, suffix_bytes) = input.split_at(region_len);
let region =
Region::try_from_bytes(region_bytes).map_err(|_| ParserError::InvalidExtension)?;
let suffix = SubdivisionSuffix::try_from_bytes(suffix_bytes)?;
Ok(Self { region, suffix })
}
}

impl writeable::Writeable for SubdivisionId {
#[inline]
fn write_to<W: core::fmt::Write + ?Sized>(&self, sink: &mut W) -> core::fmt::Result {
sink.write_str(self.region.as_str())?;
sink.write_str(self.suffix.as_str())
}

#[inline]
fn writeable_length_hint(&self) -> writeable::LengthHint {
todo!()
}
#[inline]
fn write_to_string(&self) -> alloc::borrow::Cow<str> {
todo!()
}
}

writeable::impl_display_with_writeable!(SubdivisionId);

impl FromStr for SubdivisionId {
type Err = ParserError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::try_from_bytes(s.as_bytes())
}
}
55 changes: 54 additions & 1 deletion components/locale_core/src/extensions/unicode/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use crate::parser::{ParserError, SubtagIterator};
use crate::shortvec::ShortBoxSlice;
use crate::shortvec::{ShortBoxSlice, ShortBoxSliceIntoIter};
use crate::subtags::{subtag, Subtag};
use alloc::vec::Vec;
use core::str::FromStr;
Expand Down Expand Up @@ -66,11 +66,48 @@ impl Value {
self.0.single()
}

#[doc(hidden)]
pub fn into_single_subtag(self) -> Option<Subtag> {
self.0.into_single()
}

#[doc(hidden)]
pub fn as_subtags_slice(&self) -> &[Subtag] {
&self.0
}

#[doc(hidden)]
pub fn push_subtag(&mut self, subtag: Subtag) {
self.0.push(subtag);
}

#[doc(hidden)]
pub fn extend(&mut self, rest: &Self) {
for i in rest.0.iter() {
self.0.push(*i);
}
}

#[doc(hidden)]
pub fn len(&self) -> usize {
self.0.len()
}

#[doc(hidden)]
pub fn remove_subtag(&mut self, idx: usize) -> Option<Subtag> {
if self.0.len() < idx {
None
} else {
let item = self.0.remove(idx);
Some(item)
}
}

#[doc(hidden)]
pub fn get_subtag(&self, idx: usize) -> Option<&Subtag> {
self.0.get(idx)
}

#[doc(hidden)]
pub const fn from_subtag(subtag: Option<Subtag>) -> Self {
match subtag {
Expand Down Expand Up @@ -132,6 +169,22 @@ impl Value {
}
}

impl IntoIterator for Value {
type Item = Subtag;

type IntoIter = ShortBoxSliceIntoIter<Subtag>;

fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}

impl FromIterator<Subtag> for Value {
fn from_iter<T: IntoIterator<Item = Subtag>>(iter: T) -> Self {
Self(ShortBoxSlice::from_iter(iter.into_iter()))
}
}

impl FromStr for Value {
type Err = ParserError;

Expand Down
11 changes: 11 additions & 0 deletions components/locale_core/src/shortvec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,17 @@ impl<T> ShortBoxSlice<T> {
}
}

/// Destruct into a single element of the [`ShortBoxSlice`].
///
/// Returns `None` if empty or more than one element.
pub fn into_single(self) -> Option<T> {
use ShortBoxSliceInner::*;
match self.0 {
ZeroOne(Some(v)) => Some(v),
_ => None,
}
}

/// Returns the number of elements in the collection.
#[inline]
pub fn len(&self) -> usize {
Expand Down
5 changes: 5 additions & 0 deletions components/locale_core/src/subtags/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ impl Subtag {
2 <= v.len() && v.len() <= 8
}

#[doc(hidden)]
pub fn len(&self) -> usize {
self.0.len()
}

#[doc(hidden)]
pub fn from_tinystr_unvalidated(input: tinystr::TinyAsciiStr<8>) -> Self {
Self(input)
Expand Down
9 changes: 9 additions & 0 deletions components/locale_core/src/subtags/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use crate::subtags::Subtag;

impl_tinystr_subtag!(
/// A script subtag (examples: `"Latn"`, `"Arab"`, etc.)
///
Expand Down Expand Up @@ -31,3 +33,10 @@ impl_tinystr_subtag!(
["Latn"],
["Latin"],
);

impl Script {
#[doc(hidden)]
pub fn into_subtag(self) -> Subtag {
Subtag(self.0.resize())
}
}
29 changes: 29 additions & 0 deletions utils/preferences/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# 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_preferences"
description = "API for resolving preferences"
version = "0.0.1"
categories = ["internationalization"]
license-file = "LICENSE"

authors.workspace = true
edition.workspace = true
include.workspace = true
repository.workspace = true
rust-version.workspace = true

[package.metadata.workspaces]
independent = true

[package.metadata.docs.rs]
all-features = true

[dependencies]
icu_locale_core = { workspace = true }
tinystr = { workspace = true }

[dev-dependencies]
icu_datetime = { workspace = true }
44 changes: 44 additions & 0 deletions utils/preferences/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
UNICODE LICENSE V3

COPYRIGHT AND PERMISSION NOTICE

Copyright © 2020-2023 Unicode, Inc.

NOTICE TO USER: Carefully read the following legal agreement. BY
DOWNLOADING, INSTALLING, COPYING OR OTHERWISE USING DATA FILES, AND/OR
SOFTWARE, YOU UNEQUIVOCALLY ACCEPT, AND AGREE TO BE BOUND BY, ALL OF THE
TERMS AND CONDITIONS OF THIS AGREEMENT. IF YOU DO NOT AGREE, DO NOT
DOWNLOAD, INSTALL, COPY, DISTRIBUTE OR USE THE DATA FILES OR SOFTWARE.

Permission is hereby granted, free of charge, to any person obtaining a
copy of data files and any associated documentation (the "Data Files") or
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.


Portions of ICU4X may have been adapted from ICU4C and/or ICU4J.
ICU 1.8.1 to ICU 57.1 © 1995-2016 International Business Machines Corporation and others.
Loading
Loading