Skip to content

Commit

Permalink
Introduce icu_preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
zbraniecki committed May 8, 2024
1 parent c626de0 commit fe5cd8b
Show file tree
Hide file tree
Showing 29 changed files with 1,148 additions and 0 deletions.
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
5 changes: 5 additions & 0 deletions components/locid/src/extensions/unicode/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,11 @@ impl Keywords {
}
}

/// Produce an ordered iterator over key-value pairs
pub fn iter(&self) -> impl Iterator<Item = (&Key, &Value)> {
self.0.iter()
}

pub(crate) fn for_each_subtag_str<E, F>(&self, f: &mut F) -> Result<(), E>
where
F: FnMut(&str) -> Result<(), E>,
Expand Down
9 changes: 9 additions & 0 deletions components/locid/src/extensions/unicode/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,15 @@ macro_rules! extensions_unicode_value {
);
R
}};
($value:literal, $value2:literal) => {{
let v: &str = concat!($value, "-", $value2);
let R: $crate::extensions::unicode::Value =
match $crate::extensions::unicode::Value::try_from_bytes(v.as_bytes()) {
Ok(r) => r,
_ => panic!(concat!("Invalid Unicode extension value: ", $value)),
};
R
}};
}
#[doc(inline)]
pub use extensions_unicode_value as value;
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_locid = { 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.
59 changes: 59 additions & 0 deletions utils/preferences/README.md

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

1 change: 1 addition & 0 deletions utils/preferences/src/extensions/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod unicode;
9 changes: 9 additions & 0 deletions utils/preferences/src/extensions/unicode/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// 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 ).

#[non_exhaustive]
pub enum Error {
UnknownKeyword,
UnknownKeywordValue,
}
35 changes: 35 additions & 0 deletions utils/preferences/src/extensions/unicode/keywords/calendar.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 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 crate::enum_keyword;
use crate::extensions::unicode::errors::Error;
use crate::preferences::PreferenceKey;
use icu_locid::extensions::unicode;

// https://github.com/unicode-org/cldr/blob/main/common/bcp47/calendar.xml
enum_keyword!(IslamicCalendar {
"umalqura" => Umalqura,
"tbla" => Tbla,
"civil" => Civil,
"rgsa" => Rgsa
});

enum_keyword!(Calendar {
"buddhist" => Buddhist,
"chinese" => Chinese,
"coptic" => Coptic,
"dangi" => Dangi,
"ethioaa" => Ethioaa,
"ethiopic" => Ethiopic,
"gregory" => Gregory,
"hebrew" => Hebrew,
"indian" => Indian,
"islamic" => Islamic(IslamicCalendar) {
"umalqura" => Umalqura
},
"iso8601" => Iso8601,
"japanese" => Japanese,
"persian" => Persian,
"roc" => Roc
}, "ca");
15 changes: 15 additions & 0 deletions utils/preferences/src/extensions/unicode/keywords/collation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// 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 crate::enum_keyword;
use crate::extensions::unicode::errors::Error;
use icu_locid::extensions::unicode;

enum_keyword!(Collation {
"standard" => Standard,
"search" => Search,
"phonetic" => Phonetic,
"pinyin" => Pinyin,
"searchjl" => Searchjl
});
17 changes: 17 additions & 0 deletions utils/preferences/src/extensions/unicode/keywords/currency.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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 crate::extensions::unicode::errors::Error;
use crate::preferences::PreferenceKey;
use crate::struct_keyword;
use icu_locid::extensions::unicode;
use tinystr::TinyAsciiStr;

struct_keyword!(Currency, "cu", TinyAsciiStr<3>, convert_value_to_currency);

fn convert_value_to_currency(input: &unicode::Value) -> Result<Currency, Error> {
//XXX: Validate
let i = TinyAsciiStr::from_str(&input.to_string()).unwrap();
Ok(Currency(i))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// 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 crate::enum_keyword;
use crate::extensions::unicode::errors::Error;
use icu_locid::extensions::unicode;

enum_keyword!(CurrencyFormat {
"standard" => Standard,
"account" => Account
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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 crate::extensions::unicode::errors::Error;
use crate::preferences::PreferenceKey;
use crate::struct_keyword;
use icu_locid::extensions::unicode;
use icu_locid::subtags::Script;
use std::str::FromStr;

struct_keyword!(DictionaryBreak, "dx", Script, convert_value_to_db);

fn convert_value_to_db(input: &unicode::Value) -> Result<DictionaryBreak, Error> {
let i = Script::from_str(&input.to_string()).unwrap();
Ok(DictionaryBreak(i))
}
13 changes: 13 additions & 0 deletions utils/preferences/src/extensions/unicode/keywords/emoji.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// 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 crate::enum_keyword;
use crate::extensions::unicode::errors::Error;
use icu_locid::extensions::unicode;

enum_keyword!(Emoji {
"emoji" => Emoji,
"text" => Text,
"default" => Default
});
17 changes: 17 additions & 0 deletions utils/preferences/src/extensions/unicode/keywords/first_day.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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 crate::enum_keyword;
use crate::extensions::unicode::errors::Error;
use icu_locid::extensions::unicode;

enum_keyword!(FirstDay {
"sun" => Sun,
"mon" => Mon,
"tue" => Tue,
"wed" => Wed,
"thu" => Thu,
"fri" => Fri,
"sat" => Sat
});
16 changes: 16 additions & 0 deletions utils/preferences/src/extensions/unicode/keywords/hour_cycle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// 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 crate::enum_keyword;
use crate::extensions::unicode::errors::Error;
use crate::preferences::PreferenceKey;
use icu_locid::extensions::unicode;

enum_keyword!(HourCycle,
{
H11 => "h11",
H12 => "h12",
H23 => "h23",
H24 => "h24"
}, "hc");
14 changes: 14 additions & 0 deletions utils/preferences/src/extensions/unicode/keywords/line_break.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// 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 crate::enum_keyword;
use crate::extensions::unicode::errors::Error;
use icu_locid::extensions::unicode;

enum_keyword!(LineBreak,
{
Strict => "strict",
Normal => "normal",
Loose => "loose"
});

0 comments on commit fe5cd8b

Please sign in to comment.