Skip to content

Commit

Permalink
Convenience properties API (#6066)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertbastian authored Feb 7, 2025
1 parent ea6b265 commit 785fade
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
8 changes: 8 additions & 0 deletions components/properties/src/code_point_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,4 +358,12 @@ pub trait EnumeratedProperty: crate::private::Sealed + TrieValue {
const NAME: &'static [u8];
/// The abbreviated name of this property, if it exists, otherwise the name
const SHORT_NAME: &'static [u8];

/// Convenience method for `CodePointMapData::new().get(ch)`
///
/// ✨ *Enabled with the `compiled_data` Cargo feature.*
#[cfg(feature = "compiled_data")]
fn for_char(ch: char) -> Self {
CodePointMapData::new().get(ch)
}
}
10 changes: 9 additions & 1 deletion components/properties/src/code_point_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ impl<'a> CodePointSetDataBorrowed<'a> {
/// [`CodePointSetData`]: crate::sets::CodePointSetData
/// [`TR44`]: https://www.unicode.org/reports/tr44
/// [`TR18`]: https://www.unicode.org/reports/tr18
pub trait BinaryProperty: crate::private::Sealed {
pub trait BinaryProperty: crate::private::Sealed + Sized {
#[doc(hidden)]
type DataMarker: DataMarker<DataStruct = PropertyCodePointSet<'static>>;
#[doc(hidden)]
Expand All @@ -222,6 +222,14 @@ pub trait BinaryProperty: crate::private::Sealed {
const NAME: &'static [u8];
/// The abbreviated name of this property, if it exists, otherwise the name
const SHORT_NAME: &'static [u8];

/// Convenience method for `CodePointSetData::new().contains(ch)`
///
/// ✨ *Enabled with the `compiled_data` Cargo feature.*
#[cfg(feature = "compiled_data")]
fn for_char(ch: char) -> bool {
CodePointSetData::new::<Self>().contains(ch)
}
}

#[cfg(test)]
Expand Down
24 changes: 24 additions & 0 deletions components/properties/src/names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,30 @@ pub trait NamedEnumeratedProperty: ParseableEnumeratedProperty {
fn nep_short_identity_static(
stat: &'static Self::DataStructShortBorrowed<'static>,
) -> &'static Self::DataStructShort;

/// Convenience method for `PropertyParser::new().get_loose(s)`
///
/// ✨ *Enabled with the `compiled_data` Cargo feature.*
#[cfg(feature = "compiled_data")]
fn try_from_str(s: &str) -> Option<Self> {
PropertyParser::new().get_loose(s)
}
/// Convenience method for `PropertyNamesLong::new().get(*self).unwrap()`
///
/// ✨ *Enabled with the `compiled_data` Cargo feature.*
#[cfg(feature = "compiled_data")]
fn long_name(&self) -> &'static str {
PropertyNamesLong::new().get(*self).unwrap_or("unreachable")
}
/// Convenience method for `PropertyNamesShort::new().get(*self).unwrap()`
///
/// ✨ *Enabled with the `compiled_data` Cargo feature.*
#[cfg(feature = "compiled_data")]
fn short_name(&self) -> &'static str {
PropertyNamesShort::new()
.get(*self)
.unwrap_or("unreachable")
}
}

macro_rules! impl_value_getter {
Expand Down
5 changes: 5 additions & 0 deletions ffi/capi/tests/missing_apis.txt
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,11 @@ icu::properties::PropertyNamesShortBorrowed::get_locale_script#FnInStruct
icu::properties::PropertyNamesShortBorrowed::new#FnInStruct
icu::properties::props::BidiMirroringGlyph#Struct
icu::properties::props::BidiPairedBracketType#Enum
icu::properties::props::BinaryProperty::for_char#FnInTrait
icu::properties::props::EnumeratedProperty::for_char#FnInTrait
icu::properties::props::NamedEnumeratedProperty::long_name#FnInTrait
icu::properties::props::NamedEnumeratedProperty::short_name#FnInTrait
icu::properties::props::NamedEnumeratedProperty::try_from_str#FnInTrait
icu::timezone::TimeZoneBcp47Iter#Struct
icu::timezone::TimeZoneBcp47Iter::next#FnInStruct
icu::timezone::TimeZoneCanonicalIanaIter#Struct
Expand Down

0 comments on commit 785fade

Please sign in to comment.