Skip to content

Commit

Permalink
Make FFI DataProvider only support BufferProvider (unicode-org#5895)
Browse files Browse the repository at this point in the history
Fixes unicode-org#4639



<!--
Thank you for your pull request to ICU4X!

Reminder: try to use [Conventional
Comments](https://conventionalcomments.org/) to make comments clearer.

Please see
https://github.com/unicode-org/icu4x/blob/main/CONTRIBUTING.md for
general
information on contributing to ICU4X.
-->
  • Loading branch information
Manishearth authored Dec 17, 2024
1 parent 756afb6 commit 4bad030
Show file tree
Hide file tree
Showing 37 changed files with 560 additions and 714 deletions.
6 changes: 6 additions & 0 deletions components/datetime/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ pub enum DateTimeFormatterLoadError {
Data(DataError),
}

impl From<DataError> for DateTimeFormatterLoadError {
fn from(error: DataError) -> Self {
Self::Data(error)
}
}

/// An error from mixing calendar types in a formatter.
#[derive(Display, Debug, Copy, Clone, PartialEq)]
#[displaydoc("DateTimeFormatter for {this_kind} calendar was given a {date_kind:?} calendar")]
Expand Down
4 changes: 0 additions & 4 deletions ffi/capi/bindings/c/DataProvider.h

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

4 changes: 0 additions & 4 deletions ffi/capi/bindings/cpp/icu4x/DataProvider.d.hpp

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

14 changes: 0 additions & 14 deletions ffi/capi/bindings/cpp/icu4x/DataProvider.hpp

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

29 changes: 0 additions & 29 deletions ffi/capi/bindings/dart/DataProvider.g.dart

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

4 changes: 0 additions & 4 deletions ffi/capi/bindings/js/DataProvider.d.ts

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

20 changes: 0 additions & 20 deletions ffi/capi/bindings/js/DataProvider.mjs

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

6 changes: 3 additions & 3 deletions ffi/capi/src/bidi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ pub mod ffi {
use alloc::vec::Vec;
use core::fmt::Write;

use crate::errors::ffi::DataError;
use crate::provider::ffi::DataProvider;
#[cfg(feature = "buffer_provider")]
use crate::{errors::ffi::DataError, provider::ffi::DataProvider};

pub enum BidiDirection {
Ltr,
Expand Down Expand Up @@ -39,9 +39,9 @@ pub mod ffi {
/// Creates a new [`Bidi`] from locale data, and a particular data source.
#[diplomat::rust_link(icu::properties::bidi::BidiClassAdapter::new, FnInStruct)]
#[diplomat::attr(supports = fallible_constructors, named_constructor = "with_provider")]
#[cfg(feature = "buffer_provider")]
pub fn create_with_provider(provider: &DataProvider) -> Result<Box<Bidi>, DataError> {
Ok(Box::new(Bidi(call_constructor_unstable!(
icu_properties::CodePointMapData::new [m => Ok(m.static_to_owned())],
icu_properties::CodePointMapData::try_new_unstable,
provider,
)?)))
Expand Down
25 changes: 13 additions & 12 deletions ffi/capi/src/calendar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ pub mod ffi {
use alloc::sync::Arc;
use core::fmt::Write;

#[cfg(any(feature = "compiled_data", feature = "buffer_provider"))]
use crate::errors::ffi::DataError;
use crate::locale_core::ffi::Locale;
#[cfg(feature = "buffer_provider")]
use crate::provider::ffi::DataProvider;

/// The various calendar types currently supported by [`Calendar`]
Expand Down Expand Up @@ -126,34 +128,33 @@ pub mod ffi {
#[diplomat::rust_link(icu::calendar::AnyCalendar::try_new, FnInEnum)]
#[diplomat::attr(supports = fallible_constructors, named_constructor = "for_locale_with_provider")]
#[diplomat::demo(default_constructor)]
#[cfg(feature = "buffer_provider")]
pub fn create_for_locale_with_provider(
provider: &DataProvider,
locale: &Locale,
) -> Result<Box<Calendar>, DataError> {
let prefs = (&locale.0).into();

Ok(Box::new(Calendar(Arc::new(call_constructor!(
icu_calendar::AnyCalendar::try_new,
icu_calendar::AnyCalendar::try_new_with_any_provider,
icu_calendar::AnyCalendar::try_new_with_buffer_provider,
provider,
prefs
Ok(Box::new(Calendar(Arc::new(provider.call_constructor(
|provider| icu_calendar::AnyCalendar::try_new_with_buffer_provider(provider, prefs),
)?))))
}

/// Creates a new [`Calendar`] from the specified date and time, using a particular data source.
#[diplomat::rust_link(icu::calendar::AnyCalendar::new_for_kind, FnInEnum)]
#[diplomat::attr(supports = fallible_constructors, named_constructor = "for_kind_with_provider")]
#[cfg(feature = "buffer_provider")]
pub fn create_for_kind_with_provider(
provider: &DataProvider,
kind: AnyCalendarKind,
) -> Result<Box<Calendar>, DataError> {
Ok(Box::new(Calendar(Arc::new(call_constructor!(
icu_calendar::AnyCalendar::new_for_kind [r => Ok(r)],
icu_calendar::AnyCalendar::try_new_for_kind_with_any_provider,
icu_calendar::AnyCalendar::try_new_for_kind_with_buffer_provider,
provider,
kind.into()
Ok(Box::new(Calendar(Arc::new(provider.call_constructor(
|provider| {
icu_calendar::AnyCalendar::try_new_for_kind_with_buffer_provider(
provider,
kind.into(),
)
},
)?))))
}

Expand Down
24 changes: 11 additions & 13 deletions ffi/capi/src/casemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ use icu_casemap::titlecase::TitlecaseOptions;
pub mod ffi {
use alloc::boxed::Box;

use crate::{errors::ffi::DataError, locale_core::ffi::Locale, provider::ffi::DataProvider};
#[cfg(any(feature = "compiled_data", feature = "buffer_provider"))]
use crate::errors::ffi::DataError;
use crate::locale_core::ffi::Locale;
#[cfg(feature = "buffer_provider")]
use crate::provider::ffi::DataProvider;
use diplomat_runtime::DiplomatOption;

use writeable::Writeable;
Expand Down Expand Up @@ -65,12 +69,10 @@ pub mod ffi {
/// Construct a new CaseMapper instance using a particular data source.
#[diplomat::rust_link(icu::casemap::CaseMapper::new, FnInStruct)]
#[diplomat::attr(supports = fallible_constructors, named_constructor = "with_provider")]
#[cfg(feature = "buffer_provider")]
pub fn create_with_provider(provider: &DataProvider) -> Result<Box<CaseMapper>, DataError> {
Ok(Box::new(CaseMapper(call_constructor!(
icu_casemap::CaseMapper::new [r => Ok(r)],
icu_casemap::CaseMapper::try_new_with_any_provider,
Ok(Box::new(CaseMapper(provider.call_constructor(
icu_casemap::CaseMapper::try_new_with_buffer_provider,
provider,
)?)))
}
/// Returns the full lowercase mapping of the given string
Expand Down Expand Up @@ -230,14 +232,12 @@ pub mod ffi {
#[diplomat::rust_link(icu::casemap::CaseMapCloser::new, FnInStruct)]
#[diplomat::rust_link(icu::casemap::CaseMapCloser::new_with_mapper, FnInStruct, hidden)]
#[diplomat::attr(supports = fallible_constructors, named_constructor = "with_provider")]
#[cfg(feature = "buffer_provider")]
pub fn create_with_provider(
provider: &DataProvider,
) -> Result<Box<CaseMapCloser>, DataError> {
Ok(Box::new(CaseMapCloser(call_constructor!(
icu_casemap::CaseMapCloser::new [r => Ok(r)],
icu_casemap::CaseMapCloser::try_new_with_any_provider,
Ok(Box::new(CaseMapCloser(provider.call_constructor(
icu_casemap::CaseMapCloser::try_new_with_buffer_provider,
provider,
)?)))
}
/// Adds all simple case mappings and the full case folding for `c` to `builder`.
Expand Down Expand Up @@ -289,14 +289,12 @@ pub mod ffi {
#[diplomat::rust_link(icu::casemap::TitlecaseMapper::new, FnInStruct)]
#[diplomat::rust_link(icu::casemap::TitlecaseMapper::new_with_mapper, FnInStruct, hidden)]
#[diplomat::attr(supports = fallible_constructors, named_constructor = "with_provider")]
#[cfg(feature = "buffer_provider")]
pub fn create_with_provider(
provider: &DataProvider,
) -> Result<Box<TitlecaseMapper>, DataError> {
Ok(Box::new(TitlecaseMapper(call_constructor!(
icu_casemap::TitlecaseMapper::new [r => Ok(r)],
icu_casemap::TitlecaseMapper::try_new_with_any_provider,
Ok(Box::new(TitlecaseMapper(provider.call_constructor(
icu_casemap::TitlecaseMapper::try_new_with_buffer_provider,
provider,
)?)))
}
/// Returns the full titlecase mapping of the given string
Expand Down
22 changes: 14 additions & 8 deletions ffi/capi/src/collator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
pub mod ffi {
use alloc::boxed::Box;

use crate::{errors::ffi::DataError, locale_core::ffi::Locale, provider::ffi::DataProvider};
#[cfg(feature = "buffer_provider")]
use crate::provider::ffi::DataProvider;
#[cfg(any(feature = "compiled_data", feature = "buffer_provider"))]
use crate::{errors::ffi::DataError, locale_core::ffi::Locale};
use diplomat_runtime::DiplomatOption;

#[diplomat::opaque]
Expand Down Expand Up @@ -131,18 +134,21 @@ pub mod ffi {
#[diplomat::rust_link(icu::collator::CollatorPreferences, Struct, hidden)]
#[diplomat::attr(supports = fallible_constructors, constructor)]
#[diplomat::attr(supports = non_exhaustive_structs, rename = "create_with_provider")]
#[cfg(feature = "buffer_provider")]
pub fn create_v1_with_provider(
provider: &DataProvider,
locale: &Locale,
options: CollatorOptionsV1,
) -> Result<Box<Collator>, DataError> {
Ok(Box::new(Collator(call_constructor!(
icu_collator::Collator::try_new [r => Ok(r?.static_to_owned())],
icu_collator::Collator::try_new_with_any_provider,
icu_collator::Collator::try_new_with_buffer_provider,
provider,
icu_collator::CollatorPreferences::from(&locale.0),
icu_collator::CollatorOptions::from(options),
let options = options.into();
Ok(Box::new(Collator(provider.call_constructor(
|provider| {
icu_collator::Collator::try_new_with_buffer_provider(
provider,
(&locale.0).into(),
options,
)
},
)?)))
}
/// Compare two strings.
Expand Down
Loading

0 comments on commit 4bad030

Please sign in to comment.