Skip to content

Commit 847296d

Browse files
committed
Introduce icu_preferences
1 parent 31e085a commit 847296d

30 files changed

+1235
-0
lines changed

Cargo.lock

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ members = [
6565
"utils/ixdtf",
6666
"utils/litemap",
6767
"utils/pattern",
68+
"utils/preferences",
6869
"utils/resb",
6970
"utils/tinystr",
7071
"utils/tzif",

components/locid/src/extensions/unicode/keywords.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,11 @@ impl Keywords {
358358
}
359359
}
360360

361+
/// Produce an ordered iterator over key-value pairs
362+
pub fn iter(&self) -> impl Iterator<Item = (&Key, &Value)> {
363+
self.0.iter()
364+
}
365+
361366
pub(crate) fn for_each_subtag_str<E, F>(&self, f: &mut F) -> Result<(), E>
362367
where
363368
F: FnMut(&str) -> Result<(), E>,

components/locid/src/extensions/unicode/value.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,15 @@ macro_rules! extensions_unicode_value {
191191
);
192192
R
193193
}};
194+
($value:literal, $value2:literal) => {{
195+
let v: &str = concat!($value, "-", $value2);
196+
let R: $crate::extensions::unicode::Value =
197+
match $crate::extensions::unicode::Value::try_from_bytes(v.as_bytes()) {
198+
Ok(r) => r,
199+
_ => panic!(concat!("Invalid Unicode extension value: ", $value)),
200+
};
201+
R
202+
}};
194203
}
195204
#[doc(inline)]
196205
pub use extensions_unicode_value as value;

utils/preferences/Cargo.toml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# This file is part of ICU4X. For terms of use, please see the file
2+
# called LICENSE at the top level of the ICU4X source tree
3+
# (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4+
5+
[package]
6+
name = "icu_preferences"
7+
description = "API for resolving preferences"
8+
version = "0.0.1"
9+
categories = ["internationalization"]
10+
license-file = "LICENSE"
11+
12+
authors.workspace = true
13+
edition.workspace = true
14+
include.workspace = true
15+
repository.workspace = true
16+
rust-version.workspace = true
17+
18+
[package.metadata.workspaces]
19+
independent = true
20+
21+
[package.metadata.docs.rs]
22+
all-features = true
23+
24+
[dependencies]
25+
icu_locid = { workspace = true }
26+
tinystr = { workspace = true }
27+
28+
[dev-dependencies]
29+
icu_datetime = { workspace = true }

utils/preferences/LICENSE

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
UNICODE LICENSE V3
2+
3+
COPYRIGHT AND PERMISSION NOTICE
4+
5+
Copyright © 2020-2023 Unicode, Inc.
6+
7+
NOTICE TO USER: Carefully read the following legal agreement. BY
8+
DOWNLOADING, INSTALLING, COPYING OR OTHERWISE USING DATA FILES, AND/OR
9+
SOFTWARE, YOU UNEQUIVOCALLY ACCEPT, AND AGREE TO BE BOUND BY, ALL OF THE
10+
TERMS AND CONDITIONS OF THIS AGREEMENT. IF YOU DO NOT AGREE, DO NOT
11+
DOWNLOAD, INSTALL, COPY, DISTRIBUTE OR USE THE DATA FILES OR SOFTWARE.
12+
13+
Permission is hereby granted, free of charge, to any person obtaining a
14+
copy of data files and any associated documentation (the "Data Files") or
15+
software and any associated documentation (the "Software") to deal in the
16+
Data Files or Software without restriction, including without limitation
17+
the rights to use, copy, modify, merge, publish, distribute, and/or sell
18+
copies of the Data Files or Software, and to permit persons to whom the
19+
Data Files or Software are furnished to do so, provided that either (a)
20+
this copyright and permission notice appear with all copies of the Data
21+
Files or Software, or (b) this copyright and permission notice appear in
22+
associated Documentation.
23+
24+
THE DATA FILES AND SOFTWARE ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
25+
KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
27+
THIRD PARTY RIGHTS.
28+
29+
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS NOTICE
30+
BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES,
31+
OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
32+
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
33+
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THE DATA
34+
FILES OR SOFTWARE.
35+
36+
Except as contained in this notice, the name of a copyright holder shall
37+
not be used in advertising or otherwise to promote the sale, use or other
38+
dealings in these Data Files or Software without prior written
39+
authorization of the copyright holder.
40+
41+
42+
43+
Portions of ICU4X may have been adapted from ICU4C and/or ICU4J.
44+
ICU 1.8.1 to ICU 57.1 © 1995-2016 International Business Machines Corporation and others.

utils/preferences/README.md

Lines changed: 59 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod unicode;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// This file is part of ICU4X. For terms of use, please see the file
2+
// called LICENSE at the top level of the ICU4X source tree
3+
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4+
5+
#[non_exhaustive]
6+
pub enum Error {
7+
UnknownKeyword,
8+
UnknownKeywordValue,
9+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// This file is part of ICU4X. For terms of use, please see the file
2+
// called LICENSE at the top level of the ICU4X source tree
3+
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4+
5+
#![allow(non_snake_case)]
6+
7+
use crate::enum_keyword;
8+
9+
// https://github.com/unicode-org/cldr/blob/main/common/bcp47/calendar.xml
10+
enum_keyword!(IslamicCalendar {
11+
"umalqura" => Umalqura,
12+
"tbla" => Tbla,
13+
"civil" => Civil,
14+
"rgsa" => Rgsa
15+
});
16+
17+
enum_keyword!(Calendar {
18+
"buddhist" => Buddhist,
19+
"chinese" => Chinese,
20+
"coptic" => Coptic,
21+
"dangi" => Dangi,
22+
"ethioaa" => Ethioaa,
23+
"ethiopic" => Ethiopic,
24+
"gregory" => Gregory,
25+
"hebrew" => Hebrew,
26+
"indian" => Indian,
27+
"islamic" => Islamic(IslamicCalendar) {
28+
"umalqura" => Umalqura
29+
},
30+
"iso8601" => Iso8601,
31+
"japanese" => Japanese,
32+
"persian" => Persian,
33+
"roc" => Roc
34+
}, "ca");
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// This file is part of ICU4X. For terms of use, please see the file
2+
// called LICENSE at the top level of the ICU4X source tree
3+
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4+
5+
crate::enum_keyword!(Collation {
6+
"standard" => Standard,
7+
"search" => Search,
8+
"phonetic" => Phonetic,
9+
"pinyin" => Pinyin,
10+
"searchjl" => Searchjl
11+
}, "co");
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// This file is part of ICU4X. For terms of use, please see the file
2+
// called LICENSE at the top level of the ICU4X source tree
3+
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4+
5+
use crate::struct_keyword;
6+
use icu_locid::extensions::unicode::Value;
7+
use tinystr::TinyAsciiStr;
8+
9+
struct_keyword!(Currency, "cu", TinyAsciiStr<3>, |input: &Value| {
10+
//XXX: Validate
11+
let i = TinyAsciiStr::from_str(&input.to_string()).unwrap();
12+
Ok(Currency(i))
13+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// This file is part of ICU4X. For terms of use, please see the file
2+
// called LICENSE at the top level of the ICU4X source tree
3+
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4+
5+
crate::enum_keyword!(CurrencyFormat {
6+
"standard" => Standard,
7+
"account" => Account
8+
}, "cf");
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// This file is part of ICU4X. For terms of use, please see the file
2+
// called LICENSE at the top level of the ICU4X source tree
3+
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4+
5+
use crate::struct_keyword;
6+
use icu_locid::extensions::unicode::Value;
7+
use icu_locid::subtags::Script;
8+
use std::str::FromStr;
9+
10+
struct_keyword!(DictionaryBreak, "dx", Script, |input: &Value| {
11+
let i = Script::from_str(&input.to_string()).unwrap();
12+
Ok(DictionaryBreak(i))
13+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// This file is part of ICU4X. For terms of use, please see the file
2+
// called LICENSE at the top level of the ICU4X source tree
3+
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4+
5+
use crate::enum_keyword;
6+
7+
enum_keyword!(Emoji {
8+
"emoji" => Emoji,
9+
"text" => Text,
10+
"default" => Default
11+
}, "em");
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// This file is part of ICU4X. For terms of use, please see the file
2+
// called LICENSE at the top level of the ICU4X source tree
3+
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4+
5+
use crate::enum_keyword;
6+
7+
enum_keyword!(FirstDay {
8+
"sun" => Sun,
9+
"mon" => Mon,
10+
"tue" => Tue,
11+
"wed" => Wed,
12+
"thu" => Thu,
13+
"fri" => Fri,
14+
"sat" => Sat
15+
}, "fw");
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// This file is part of ICU4X. For terms of use, please see the file
2+
// called LICENSE at the top level of the ICU4X source tree
3+
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4+
5+
use crate::enum_keyword;
6+
7+
enum_keyword!(HourCycle {
8+
"h11" => H11,
9+
"h12" => H12,
10+
"h23" => H23,
11+
"h24" => H24,
12+
}, "hc");
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// This file is part of ICU4X. For terms of use, please see the file
2+
// called LICENSE at the top level of the ICU4X source tree
3+
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4+
5+
use crate::enum_keyword;
6+
7+
enum_keyword!(LineBreak {
8+
"strict" => Strict,
9+
"normal" => Normal,
10+
"loose" => Loose,
11+
}, "lb");
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// This file is part of ICU4X. For terms of use, please see the file
2+
// called LICENSE at the top level of the ICU4X source tree
3+
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4+
5+
use crate::extensions::unicode::errors::Error;
6+
use crate::keyword_list;
7+
use crate::preferences::PreferenceKey;
8+
use icu_locid::extensions::unicode;
9+
10+
keyword_list!(Keyword {
11+
calendar::Calendar,
12+
currency_format::CurrencyFormat,
13+
collation::Collation,
14+
currency::Currency,
15+
dictionary_break::DictionaryBreak,
16+
emoji::Emoji,
17+
first_day::FirstDay,
18+
hour_cycle::HourCycle,
19+
line_break::LineBreak,
20+
numbering_system::NumberingSystem
21+
// lw line break word
22+
// ms meaurement system
23+
// mu measurement unit
24+
// rg region override
25+
// sd region subdivision
26+
// ss sentence supression
27+
// tz timezone
28+
// va variant
29+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// This file is part of ICU4X. For terms of use, please see the file
2+
// called LICENSE at the top level of the ICU4X source tree
3+
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4+
5+
use crate::extensions::unicode::errors::Error;
6+
use crate::struct_keyword;
7+
use icu_locid::extensions::unicode::Value;
8+
use tinystr::TinyAsciiStr;
9+
10+
struct_keyword!(NumberingSystem, "nu", TinyAsciiStr<8>, |input: &Value| {
11+
//XXX: Perf
12+
let s = input.to_string();
13+
let v = TinyAsciiStr::from_str(&s);
14+
if let Ok(v) = v {
15+
Ok(NumberingSystem(v))
16+
} else {
17+
Err(Error::UnknownKeywordValue)
18+
}
19+
});

0 commit comments

Comments
 (0)