Skip to content

Commit

Permalink
clean up provider
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Dec 13, 2024
1 parent 4eff12a commit 62acfbf
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 95 deletions.
12 changes: 1 addition & 11 deletions ffi/capi/src/locale_directionality.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,13 @@ pub mod ffi {
DataProviderInner::Destroyed => Err(icu_provider::DataError::custom(
"This provider has been destroyed",
))?,
DataProviderInner::Empty => {
icu_locale::LocaleDirectionality::try_new_with_expander_unstable(
&icu_provider_adapters::empty::EmptyDataProvider::new(),
expander.0.clone(),
)?
}
#[cfg(feature = "buffer_provider")]

DataProviderInner::Buffer(buffer_provider) => {
icu_locale::LocaleDirectionality::try_new_with_expander_unstable(
&buffer_provider.as_deserializing(),
expander.0.clone(),
)?
}
#[cfg(feature = "compiled_data")]
DataProviderInner::Compiled => {
icu_locale::LocaleDirectionality::new_with_expander(expander.0.clone())
}
})))
}

Expand Down
88 changes: 4 additions & 84 deletions ffi/capi/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

#[cfg(feature = "buffer_provider")]
use icu_provider::prelude::*;

#[cfg(feature = "buffer_provider")]
pub enum DataProviderInner {
Destroyed,
Empty,
#[cfg(feature = "compiled_data")]
Compiled,
#[cfg(feature = "buffer_provider")]
Buffer(alloc::boxed::Box<dyn BufferProvider + 'static>),
}

#[diplomat::bridge]
#[diplomat::abi_rename = "icu4x_{0}_mv1"]
#[diplomat::attr(auto, namespace = "icu4x")]
#[cfg(feature = "buffer_provider")]
pub mod ffi {
use alloc::boxed::Box;

Expand All @@ -27,27 +26,13 @@ pub mod ffi {
#[diplomat::rust_link(icu_provider, Mod)]
pub struct DataProvider(pub DataProviderInner);

#[cfg(feature = "buffer_provider")]
fn convert_buffer_provider<D: icu_provider::buf::BufferProvider + 'static>(
x: D,
) -> DataProvider {
DataProvider(super::DataProviderInner::Buffer(Box::new(x)))
}

impl DataProvider {
/// Constructs an [`DataProvider`] that uses compiled data.
///
/// Requires the `compiled_data` feature.
///
/// This provider cannot be modified or combined with other providers, so `enable_fallback`,
/// `enabled_fallback_with`, `fork_by_locale`, and `fork_by_key` will return `Err`s.
#[cfg(feature = "compiled_data")]
#[diplomat::attr(auto, named_constructor)]
#[diplomat::demo(default_constructor)]
pub fn compiled() -> Box<DataProvider> {
Box::new(Self(DataProviderInner::Compiled))
}

/// Constructs an `FsDataProvider` and returns it as an [`DataProvider`].
/// Requires the `provider_fs` Cargo feature.
/// Not supported in WASM.
Expand All @@ -71,7 +56,6 @@ pub mod ffi {

/// Constructs a `BlobDataProvider` and returns it as an [`DataProvider`].
#[diplomat::rust_link(icu_provider_blob::BlobDataProvider, Struct)]
#[cfg(feature = "buffer_provider")]
#[diplomat::attr(supports = fallible_constructors, named_constructor)]
#[diplomat::attr(not(supports = static_slices), disable)]
pub fn from_byte_slice(
Expand All @@ -82,18 +66,6 @@ pub mod ffi {
)))
}

/// Constructs an empty [`DataProvider`].
#[diplomat::rust_link(icu_provider_adapters::empty::EmptyDataProvider, Struct)]
#[diplomat::rust_link(
icu_provider_adapters::empty::EmptyDataProvider::new,
FnInStruct,
hidden
)]
#[diplomat::attr(auto, named_constructor)]
pub fn empty() -> Box<DataProvider> {
Box::new(DataProvider(DataProviderInner::Empty))
}

/// Creates a provider that tries the current provider and then, if the current provider
/// doesn't support the data key, another provider `other`.
///
Expand All @@ -118,14 +90,6 @@ pub mod ffi {
(Destroyed, _) | (_, Destroyed) => Err(icu_provider::DataError::custom(
"This provider has been destroyed",
))?,
#[cfg(feature = "compiled_data")]
(Compiled, _) | (_, Compiled) => Err(icu_provider::DataError::custom(
"The compiled provider cannot be modified",
))?,
(Empty, Empty) => DataProvider(DataProviderInner::Empty),
#[cfg(feature = "buffer_provider")]
(Empty, b) | (b, Empty) => DataProvider(b),
#[cfg(feature = "buffer_provider")]
(Buffer(a), Buffer(b)) => convert_buffer_provider(
icu_provider_adapters::fork::ForkByMarkerProvider::new(a, b),
),
Expand All @@ -148,14 +112,6 @@ pub mod ffi {
(Destroyed, _) | (_, Destroyed) => Err(icu_provider::DataError::custom(
"This provider has been destroyed",
))?,
#[cfg(feature = "compiled_data")]
(Compiled, _) | (_, Compiled) => Err(icu_provider::DataError::custom(
"The compiled provider cannot be modified",
))?,
(Empty, Empty) => DataProvider(DataProviderInner::Empty),
#[cfg(feature = "buffer_provider")]
(Empty, b) | (b, Empty) => DataProvider(b),
#[cfg(feature = "buffer_provider")]
(Buffer(a), Buffer(b)) => convert_buffer_provider(
icu_provider_adapters::fork::ForkByErrorProvider::new_with_predicate(
a,
Expand Down Expand Up @@ -187,12 +143,6 @@ pub mod ffi {
Destroyed => Err(icu_provider::DataError::custom(
"This provider has been destroyed",
))?,
#[cfg(feature = "compiled_data")]
Compiled => Err(icu_provider::DataError::custom(
"The compiled provider cannot be modified",
))?,
Empty => Err(icu_provider::DataErrorKind::MarkerNotFound.into_error())?,
#[cfg(feature = "buffer_provider")]
Buffer(inner) => convert_buffer_provider(
icu_provider_adapters::fallback::LocaleFallbackProvider::new(
inner,
Expand All @@ -205,30 +155,20 @@ pub mod ffi {
}
}

#[cfg(feature = "buffer_provider")]
macro_rules! load {
() => {
fn load(&self, req: DataRequest) -> Result<DataResponse<M>, DataError> {
use DataProviderInner::*;
match self {
Destroyed => Err(DataError::custom("This provider has been destroyed"))?,
Empty => icu_provider_adapters::empty::EmptyDataProvider::new().load(req),
#[cfg(feature = "buffer_provider")]
Buffer(buffer_provider) => buffer_provider.as_deserializing().load(req),
#[cfg(feature = "compiled_data")]
Compiled => unreachable!(),
}
}
};
}

#[cfg(not(feature = "buffer_provider"))]
impl<M> DataProvider<M> for DataProviderInner
where
M: DataMarker,
{
load!();
}

#[cfg(feature = "buffer_provider")]
impl<M> DataProvider<M> for DataProviderInner
where
Expand All @@ -250,16 +190,6 @@ macro_rules! call_constructor {
"This provider has been destroyed",
))?,
$crate::provider::DataProviderInner::Buffer(buffer_provider) => $buffer(buffer_provider, $($args,)*),
_ => unreachable!()
}
};
($buffer:path, $provider:expr $(, $args:expr)* $(,)?) => {
match &$provider.0 {
$crate::provider::DataProviderInner::Destroyed => Err(icu_provider::DataError::custom(
"This provider has been destroyed",
))?,
$crate::provider::DataProviderInner::Buffer(buffer_provider) => $buffer(buffer_provider, $($args,)*),
_ => unreachable!()
}
};
}
Expand All @@ -272,16 +202,6 @@ macro_rules! call_constructor_unstable {
"This provider has been destroyed",
))?,
$crate::provider::DataProviderInner::Buffer(buffer_provider) => $unstable(&icu_provider::buf::AsDeserializingBufferProvider::as_deserializing(buffer_provider), $($args,)*),
_ => unreachable!()
}
};
($unstable:path, $provider:expr $(, $args:expr)* $(,)?) => {
match &$provider.0 {
$crate::provider::DataProviderInner::Destroyed => Err(icu_provider::DataError::custom(
"This provider has been destroyed",
))?,
$crate::provider::DataProviderInner::Buffer(buffer_provider) => $unstable(&icu_provider::buf::AsDeserializingBufferProvider::as_deserializing(buffer_provider), $($args,)*),
_ => unreachable!()
}
};
}

0 comments on commit 62acfbf

Please sign in to comment.