Skip to content

Commit

Permalink
Using Option on FFI (#4635)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertbastian committed Feb 28, 2024
1 parent e4d418b commit 7b5b7cc
Show file tree
Hide file tree
Showing 13 changed files with 34 additions and 45 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

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

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,10 @@ icu_benchmark_macros = { path = "tools/benchmark/macros" }

# The version here can either be a `version = ".."` spec or `git = "https://github.com/rust-diplomat/diplomat", rev = ".."`
# Diplomat must be published preceding a new ICU4X release but may use git versions in between
diplomat = { git = "https://github.com/rust-diplomat/diplomat.git", rev = "a47173bb7af0b284ed18fdca0a1ce4a9fae08e6f" }
diplomat-runtime = { git = "https://github.com/rust-diplomat/diplomat.git", rev = "a47173bb7af0b284ed18fdca0a1ce4a9fae08e6f" }
diplomat_core = { git = "https://github.com/rust-diplomat/diplomat.git", rev = "a47173bb7af0b284ed18fdca0a1ce4a9fae08e6f" }
diplomat-tool = { git = "https://github.com/rust-diplomat/diplomat.git", rev = "a47173bb7af0b284ed18fdca0a1ce4a9fae08e6f" }
diplomat = { git = "https://github.com/rust-diplomat/diplomat.git", rev = "66c3e35d779f1f0743ce06a9f043017d6badf596" }
diplomat-runtime = { git = "https://github.com/rust-diplomat/diplomat.git", rev = "66c3e35d779f1f0743ce06a9f043017d6badf596" }
diplomat_core = { git = "https://github.com/rust-diplomat/diplomat.git", rev = "66c3e35d779f1f0743ce06a9f043017d6badf596" }
diplomat-tool = { git = "https://github.com/rust-diplomat/diplomat.git", rev = "66c3e35d779f1f0743ce06a9f043017d6badf596" }

# Size optimized builds
[profile.release-opt-size]
Expand Down
6 changes: 3 additions & 3 deletions docs/tutorials/c/Cargo.lock

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

2 changes: 1 addition & 1 deletion ffi/capi/bindings/cpp/ICU4XScriptExtensionsSet.hpp

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

4 changes: 2 additions & 2 deletions ffi/capi/bindings/dart/AnyCalendarKind.g.dart

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

2 changes: 1 addition & 1 deletion ffi/capi/bindings/dart/ScriptExtensionsSet.g.dart

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

2 changes: 1 addition & 1 deletion ffi/capi/bindings/js/ICU4XBidiInfo.mjs

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

1 change: 0 additions & 1 deletion ffi/capi/bindings/js/ICU4XPluralCategory.d.ts

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

6 changes: 2 additions & 4 deletions ffi/capi/bindings/js/ICU4XScriptExtensionsSet.d.ts

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

12 changes: 5 additions & 7 deletions ffi/capi/bindings/js/ICU4XScriptExtensionsSet.mjs

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

16 changes: 6 additions & 10 deletions ffi/capi/src/calendar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,16 @@ pub mod ffi {
impl ICU4XAnyCalendarKind {
/// Read the calendar type off of the -u-ca- extension on a locale.
///
/// Errors if there is no calendar on the locale or if the locale's calendar
/// Returns nothing if there is no calendar on the locale or if the locale's calendar
/// is not known or supported.
#[diplomat::rust_link(icu::calendar::AnyCalendarKind::get_for_locale, FnInEnum)]
pub fn get_for_locale(locale: &ICU4XLocale) -> Result<ICU4XAnyCalendarKind, ()> {
AnyCalendarKind::get_for_locale(&locale.0)
.map(Into::into)
.ok_or(())
pub fn get_for_locale(locale: &ICU4XLocale) -> Option<ICU4XAnyCalendarKind> {
AnyCalendarKind::get_for_locale(&locale.0).map(Into::into)
}

/// Obtain the calendar type given a BCP-47 -u-ca- extension string.
///
/// Errors if the calendar is not known or supported.
/// Returns nothing if the calendar is not known or supported.
#[diplomat::rust_link(icu::calendar::AnyCalendarKind::get_for_bcp47_value, FnInEnum)]
#[diplomat::rust_link(
icu::calendar::AnyCalendarKind::get_for_bcp47_string,
Expand All @@ -82,10 +80,8 @@ pub mod ffi {
FnInEnum,
hidden
)]
pub fn get_for_bcp47(s: &DiplomatStr) -> Result<ICU4XAnyCalendarKind, ()> {
AnyCalendarKind::get_for_bcp47_bytes(s)
.map(Into::into)
.ok_or(())
pub fn get_for_bcp47(s: &DiplomatStr) -> Option<ICU4XAnyCalendarKind> {
AnyCalendarKind::get_for_bcp47_bytes(s).map(Into::into)
}

/// Obtain the string suitable for use in the -u-ca- extension in a BCP47 locale.
Expand Down
6 changes: 2 additions & 4 deletions ffi/capi/src/pluralrules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ pub mod ffi {
/// [specified in TR35](https://unicode.org/reports/tr35/tr35-numbers.html#Language_Plural_Rules)
#[diplomat::rust_link(icu::plurals::PluralCategory::get_for_cldr_string, FnInEnum)]
#[diplomat::rust_link(icu::plurals::PluralCategory::get_for_cldr_bytes, FnInEnum)]
pub fn get_for_cldr_string(s: &DiplomatStr) -> Result<ICU4XPluralCategory, ()> {
PluralCategory::get_for_cldr_bytes(s)
.ok_or(())
.map(Into::into)
pub fn get_for_cldr_string(s: &DiplomatStr) -> Option<ICU4XPluralCategory> {
PluralCategory::get_for_cldr_bytes(s).map(Into::into)
}
}

Expand Down
6 changes: 3 additions & 3 deletions ffi/capi/src/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,10 @@ pub mod ffi {
self.0.array_len()
}

/// Get script at index, returning an error if out of bounds
/// Get script at index
#[diplomat::rust_link(icu::properties::script::ScriptExtensionsSet::iter, FnInStruct)]
pub fn script_at(&self, index: usize) -> Result<u16, ()> {
self.0.array_get(index).map(|x| x.0).ok_or(())
pub fn script_at(&self, index: usize) -> Option<u16> {
self.0.array_get(index).map(|x| x.0)
}
}
}

0 comments on commit 7b5b7cc

Please sign in to comment.