Struct icu_provider_adapters::filter::FilterDataProvider
source · pub struct FilterDataProvider<D, F>{
pub inner: D,
pub predicate: F,
pub filter_name: &'static str,
}
Expand description
A data provider that selectively filters out data requests.
Data requests that are rejected by the filter will return a [DataError
] with kind
Filtered
, and they will not be returned
by [IterableDynamicDataProvider::iter_ids_for_marker
].
Although this struct can be created directly, the traits in this module provide helper functions for common filtering patterns.
Fields§
§inner: D
The data provider to which we delegate requests.
predicate: F
The predicate function. A return value of true
indicates that the request should
proceed as normal; a return value of false
will reject the request.
filter_name: &'static str
A name for this filter, used in error messages.
Implementations§
source§impl<D> FilterDataProvider<D, fn(_: DataIdentifierBorrowed<'_>) -> bool>
impl<D> FilterDataProvider<D, fn(_: DataIdentifierBorrowed<'_>) -> bool>
sourcepub fn new(provider: D, filter_name: &'static str) -> Self
pub fn new(provider: D, filter_name: &'static str) -> Self
Creates a FilterDataProvider
that does not do any filtering.
Filters can be added using Self::with_filter
.
source§impl<D, F> FilterDataProvider<D, F>
impl<D, F> FilterDataProvider<D, F>
sourcepub fn with_filter<'a>(
self,
predicate: impl Fn(DataIdentifierBorrowed<'_>) -> bool + Sync + 'a,
) -> FilterDataProvider<D, Box<dyn Fn(DataIdentifierBorrowed<'_>) -> bool + Sync + 'a>>where
F: 'a,
pub fn with_filter<'a>(
self,
predicate: impl Fn(DataIdentifierBorrowed<'_>) -> bool + Sync + 'a,
) -> FilterDataProvider<D, Box<dyn Fn(DataIdentifierBorrowed<'_>) -> bool + Sync + 'a>>where
F: 'a,
Filter out data requests with certain langids according to the predicate function. The
predicate should return true
to allow a langid and false
to reject a langid.
§Examples
use icu_locale::LanguageIdentifier;
use icu_locale::{langid, subtags::language};
use icu_provider::hello_world::*;
use icu_provider::prelude::*;
use icu_provider_adapters::filter::FilterDataProvider;
let provider = FilterDataProvider::new(HelloWorldProvider, "Demo no-English filter")
.with_filter(|id| id.locale.language != language!("en"));
// German requests should succeed:
let de = DataIdentifierCow::from_locale(langid!("de").into());
let response: Result<DataResponse<HelloWorldV1Marker>, _> =
provider.load(DataRequest {
id: de.as_borrowed(),
..Default::default()
});
assert!(matches!(response, Ok(_)));
// English requests should fail:
let en = DataIdentifierCow::from_locale(langid!("en-US").into());
let response: Result<DataResponse<HelloWorldV1Marker>, _> =
provider.load(DataRequest {
id: en.as_borrowed(),
..Default::default()
});
let response: Result<DataResponse<HelloWorldV1Marker>, _> =
provider.load(DataRequest {
id: en.as_borrowed(),
..Default::default()
});
assert!(matches!(
response,
Err(DataError {
kind: DataErrorKind::IdentifierNotFound,
..
})
));
// English should not appear in the iterator result:
let available_ids = provider
.iter_ids()
.expect("Should successfully make an iterator of supported locales");
assert!(available_ids.contains(&DataIdentifierCow::from_locale(langid!("de").into())));
assert!(!available_ids.contains(&DataIdentifierCow::from_locale(langid!("en").into())));
Trait Implementations§
source§impl<D, F> AnyProvider for FilterDataProvider<D, F>
impl<D, F> AnyProvider for FilterDataProvider<D, F>
source§impl<D, F, M> DataProvider<M> for FilterDataProvider<D, F>
impl<D, F, M> DataProvider<M> for FilterDataProvider<D, F>
source§impl<D: Debug, F> Debug for FilterDataProvider<D, F>
impl<D: Debug, F> Debug for FilterDataProvider<D, F>
source§impl<D, F, M> DryDataProvider<M> for FilterDataProvider<D, F>
impl<D, F, M> DryDataProvider<M> for FilterDataProvider<D, F>
source§impl<D, F, M> DynamicDataProvider<M> for FilterDataProvider<D, F>
impl<D, F, M> DynamicDataProvider<M> for FilterDataProvider<D, F>
source§impl<D, F, M> DynamicDryDataProvider<M> for FilterDataProvider<D, F>
impl<D, F, M> DynamicDryDataProvider<M> for FilterDataProvider<D, F>
source§impl<P0, F> ExportableProvider for FilterDataProvider<P0, F>
impl<P0, F> ExportableProvider for FilterDataProvider<P0, F>
source§fn supported_markers(&self) -> HashSet<DataMarkerInfo>
fn supported_markers(&self) -> HashSet<DataMarkerInfo>
source§impl<M, D, F> IterableDataProvider<M> for FilterDataProvider<D, F>
impl<M, D, F> IterableDataProvider<M> for FilterDataProvider<D, F>
source§impl<M, D, F> IterableDynamicDataProvider<M> for FilterDataProvider<D, F>
impl<M, D, F> IterableDynamicDataProvider<M> for FilterDataProvider<D, F>
source§fn iter_ids_for_marker(
&self,
marker: DataMarkerInfo,
) -> Result<BTreeSet<DataIdentifierCow<'_>>, DataError>
fn iter_ids_for_marker( &self, marker: DataMarkerInfo, ) -> Result<BTreeSet<DataIdentifierCow<'_>>, DataError>
DataMarkerInfo
], returns a set of [DataIdentifierCow
].Auto Trait Implementations§
impl<D, F> Freeze for FilterDataProvider<D, F>
impl<D, F> RefUnwindSafe for FilterDataProvider<D, F>where
D: RefUnwindSafe,
F: RefUnwindSafe,
impl<D, F> Send for FilterDataProvider<D, F>
impl<D, F> Sync for FilterDataProvider<D, F>
impl<D, F> Unpin for FilterDataProvider<D, F>
impl<D, F> UnwindSafe for FilterDataProvider<D, F>where
D: UnwindSafe,
F: UnwindSafe,
Blanket Implementations§
§impl<P> AsDeserializingBufferProvider for Pwhere
P: BufferProvider + ?Sized,
impl<P> AsDeserializingBufferProvider for Pwhere
P: BufferProvider + ?Sized,
§fn as_deserializing(&self) -> DeserializingBufferProvider<'_, P>
fn as_deserializing(&self) -> DeserializingBufferProvider<'_, P>
Wrap this [BufferProvider
] in a [DeserializingBufferProvider
].
This requires enabling the deserialization Cargo feature for the expected format(s):
deserialize_json
deserialize_postcard_1
deserialize_bincode_1
§impl<P> AsDowncastingAnyProvider for Pwhere
P: AnyProvider + ?Sized,
impl<P> AsDowncastingAnyProvider for Pwhere
P: AnyProvider + ?Sized,
§fn as_downcasting(&self) -> DowncastingAnyProvider<'_, P>
fn as_downcasting(&self) -> DowncastingAnyProvider<'_, P>
DynamicDataProvider<M>
when called on AnyProvider
§impl<P> AsDynamicDataProviderAnyMarkerWrap for Pwhere
P: DynamicDataProvider<AnyMarker> + ?Sized,
impl<P> AsDynamicDataProviderAnyMarkerWrap for Pwhere
P: DynamicDataProvider<AnyMarker> + ?Sized,
§fn as_any_provider(&self) -> DynamicDataProviderAnyMarkerWrap<'_, P>
fn as_any_provider(&self) -> DynamicDataProviderAnyMarkerWrap<'_, P>
AnyProvider
when called on DynamicDataProvider<AnyMarker>
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more