Skip to content

Commit 9dd49be

Browse files
authored
Add FFI for Property Enums (#5841)
Fixes #4686 We removed BidiAuxiliaryProps, it seems, so this doesn't add that. It also doesn't add enum-to-name at the moment, we can add later if we wish.
1 parent 2e57d63 commit 9dd49be

File tree

98 files changed

+10656
-21
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+10656
-21
lines changed

components/properties/src/props.rs

+77-18
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub use crate::bidi::{BidiMirroringGlyph, BidiPairedBracketType};
3030
/// ...
3131
/// }
3232
/// ```
33-
/// Produces `const ALL_CONSTS = &[("Neutral", 0u16), ...];` by
33+
/// Produces `const ALL_VALUES = &[("Neutral", 0u16), ...];` by
3434
/// explicitly casting first field of the struct to u16.
3535
macro_rules! create_const_array {
3636
(
@@ -46,11 +46,19 @@ macro_rules! create_const_array {
4646
$v const $i: $t = $e;
4747
)*
4848

49-
#[cfg(test)]
50-
const ALL_CONSTS: &'static [(&'static str, u16)] = &[
51-
$((stringify!($i), $enum_ty::$i.0 as u16)),*
49+
/// All possible values of this enum in the Unicode version
50+
/// from this ICU4X release.
51+
pub const ALL_VALUES: &'static [$enum_ty] = &[
52+
$($enum_ty::$i),*
5253
];
5354
}
55+
56+
57+
impl From<$enum_ty> for u16 {
58+
fn from(other: $enum_ty) -> Self {
59+
other.0 as u16
60+
}
61+
}
5462
}
5563
}
5664

@@ -269,6 +277,42 @@ pub(crate) mod gc {
269277

270278
pub use gc::GeneralCategory;
271279

280+
impl GeneralCategory {
281+
/// All possible values of this enum
282+
pub const ALL_VALUES: &'static [GeneralCategory] = &[
283+
GeneralCategory::Unassigned,
284+
GeneralCategory::UppercaseLetter,
285+
GeneralCategory::LowercaseLetter,
286+
GeneralCategory::TitlecaseLetter,
287+
GeneralCategory::ModifierLetter,
288+
GeneralCategory::OtherLetter,
289+
GeneralCategory::NonspacingMark,
290+
GeneralCategory::SpacingMark,
291+
GeneralCategory::EnclosingMark,
292+
GeneralCategory::DecimalNumber,
293+
GeneralCategory::LetterNumber,
294+
GeneralCategory::OtherNumber,
295+
GeneralCategory::SpaceSeparator,
296+
GeneralCategory::LineSeparator,
297+
GeneralCategory::ParagraphSeparator,
298+
GeneralCategory::Control,
299+
GeneralCategory::Format,
300+
GeneralCategory::PrivateUse,
301+
GeneralCategory::Surrogate,
302+
GeneralCategory::DashPunctuation,
303+
GeneralCategory::OpenPunctuation,
304+
GeneralCategory::ClosePunctuation,
305+
GeneralCategory::ConnectorPunctuation,
306+
GeneralCategory::InitialPunctuation,
307+
GeneralCategory::FinalPunctuation,
308+
GeneralCategory::OtherPunctuation,
309+
GeneralCategory::MathSymbol,
310+
GeneralCategory::CurrencySymbol,
311+
GeneralCategory::ModifierSymbol,
312+
GeneralCategory::OtherSymbol,
313+
];
314+
}
315+
272316
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash, Default)]
273317
/// Error value for `impl TryFrom<u8> for GeneralCategory`.
274318
#[non_exhaustive]
@@ -620,6 +664,7 @@ impl From<GeneralCategoryGroup> for u32 {
620664
#[repr(transparent)]
621665
pub struct Script(pub u16);
622666

667+
create_const_array! {
623668
#[allow(missing_docs)] // These constants don't need individual documentation.
624669
#[allow(non_upper_case_globals)]
625670
impl Script {
@@ -789,6 +834,7 @@ impl Script {
789834
pub const Yi: Script = Script(41);
790835
pub const ZanabazarSquare: Script = Script(177);
791836
}
837+
}
792838

793839
make_enumerated_property! {
794840
name: "Script";
@@ -933,6 +979,7 @@ make_enumerated_property! {
933979
#[repr(transparent)]
934980
pub struct LineBreak(pub u8);
935981

982+
create_const_array! {
936983
#[allow(missing_docs)] // These constants don't need individual documentation.
937984
#[allow(non_upper_case_globals)]
938985
impl LineBreak {
@@ -987,6 +1034,7 @@ impl LineBreak {
9871034
pub const ViramaFinal: LineBreak = LineBreak(46); // name=VF"
9881035
pub const Virama: LineBreak = LineBreak(47); // name=VI"
9891036
}
1037+
}
9901038

9911039
make_enumerated_property! {
9921040
name: "Line_Break";
@@ -1026,6 +1074,7 @@ make_enumerated_property! {
10261074
#[repr(transparent)]
10271075
pub struct GraphemeClusterBreak(pub u8);
10281076

1077+
create_const_array! {
10291078
#[allow(missing_docs)] // These constants don't need individual documentation.
10301079
#[allow(non_upper_case_globals)]
10311080
impl GraphemeClusterBreak {
@@ -1052,6 +1101,7 @@ impl GraphemeClusterBreak {
10521101
pub const GlueAfterZwj: GraphemeClusterBreak = GraphemeClusterBreak(16); // name="GAZ"
10531102
pub const ZWJ: GraphemeClusterBreak = GraphemeClusterBreak(17); // name="ZWJ"
10541103
}
1104+
}
10551105

10561106
make_enumerated_property! {
10571107
name: "Grapheme_Cluster_Break";
@@ -2956,19 +3006,28 @@ mod test_enumerated_property_completeness {
29563006
use super::*;
29573007
use alloc::collections::BTreeMap;
29583008

2959-
fn check_enum<'a>(
3009+
fn check_enum<'a, T: NamedEnumeratedProperty>(
29603010
lookup: &crate::provider::names::PropertyValueNameToEnumMapV1<'static>,
2961-
consts: impl IntoIterator<Item = &'a (&'static str, u16)>,
2962-
) {
3011+
consts: impl IntoIterator<Item = &'a T>,
3012+
) where
3013+
u16: From<T>,
3014+
{
29633015
let mut data: BTreeMap<_, _> = lookup
29643016
.map
29653017
.iter()
29663018
.map(|(name, value)| (value, (name, "Data")))
29673019
.collect();
29683020

2969-
let consts = consts
2970-
.into_iter()
2971-
.map(|(name, value)| (*value as usize, (name.to_string(), "Consts")));
3021+
let names = crate::PropertyNamesLong::<T>::new();
3022+
let consts = consts.into_iter().map(|value| {
3023+
(
3024+
u16::from(*value) as usize,
3025+
(
3026+
names.get(*value).unwrap_or("<unknown>").to_string(),
3027+
"Consts",
3028+
),
3029+
)
3030+
});
29723031

29733032
let mut diff = Vec::new();
29743033
for t @ (value, _) in consts {
@@ -2994,63 +3053,63 @@ mod test_enumerated_property_completeness {
29943053
fn test_ea() {
29953054
check_enum(
29963055
crate::provider::Baked::SINGLETON_EAST_ASIAN_WIDTH_NAME_TO_VALUE_V2_MARKER,
2997-
EastAsianWidth::ALL_CONSTS,
3056+
EastAsianWidth::ALL_VALUES,
29983057
);
29993058
}
30003059

30013060
#[test]
30023061
fn test_ccc() {
30033062
check_enum(
30043063
crate::provider::Baked::SINGLETON_CANONICAL_COMBINING_CLASS_NAME_TO_VALUE_V2_MARKER,
3005-
CanonicalCombiningClass::ALL_CONSTS,
3064+
CanonicalCombiningClass::ALL_VALUES,
30063065
);
30073066
}
30083067

30093068
#[test]
30103069
fn test_jt() {
30113070
check_enum(
30123071
crate::provider::Baked::SINGLETON_JOINING_TYPE_NAME_TO_VALUE_V2_MARKER,
3013-
JoiningType::ALL_CONSTS,
3072+
JoiningType::ALL_VALUES,
30143073
);
30153074
}
30163075

30173076
#[test]
30183077
fn test_insc() {
30193078
check_enum(
30203079
crate::provider::Baked::SINGLETON_INDIC_SYLLABIC_CATEGORY_NAME_TO_VALUE_V2_MARKER,
3021-
IndicSyllabicCategory::ALL_CONSTS,
3080+
IndicSyllabicCategory::ALL_VALUES,
30223081
);
30233082
}
30243083

30253084
#[test]
30263085
fn test_sb() {
30273086
check_enum(
30283087
crate::provider::Baked::SINGLETON_SENTENCE_BREAK_NAME_TO_VALUE_V2_MARKER,
3029-
SentenceBreak::ALL_CONSTS,
3088+
SentenceBreak::ALL_VALUES,
30303089
);
30313090
}
30323091

30333092
#[test]
30343093
fn test_wb() {
30353094
check_enum(
30363095
crate::provider::Baked::SINGLETON_WORD_BREAK_NAME_TO_VALUE_V2_MARKER,
3037-
WordBreak::ALL_CONSTS,
3096+
WordBreak::ALL_VALUES,
30383097
);
30393098
}
30403099

30413100
#[test]
30423101
fn test_bc() {
30433102
check_enum(
30443103
crate::provider::Baked::SINGLETON_BIDI_CLASS_NAME_TO_VALUE_V2_MARKER,
3045-
BidiClass::ALL_CONSTS,
3104+
BidiClass::ALL_VALUES,
30463105
);
30473106
}
30483107

30493108
#[test]
30503109
fn test_hst() {
30513110
check_enum(
30523111
crate::provider::Baked::SINGLETON_HANGUL_SYLLABLE_TYPE_NAME_TO_VALUE_V2_MARKER,
3053-
HangulSyllableType::ALL_CONSTS,
3112+
HangulSyllableType::ALL_VALUES,
30543113
);
30553114
}
30563115
}

ffi/capi/bindings/c/BidiClass.d.h

+44
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ffi/capi/bindings/c/BidiClass.h

+28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ffi/capi/bindings/c/CanonicalCombiningClass.d.h

+79
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)