pub struct IdentifierNotFoundPredicate;
Expand description
A predicate that allows forking providers to search for a provider that supports a
particular locale, based on whether it returns [DataErrorKind::IdentifierNotFound
].
§Examples
use icu_provider_adapters::fork::ForkByErrorProvider;
use icu_provider_adapters::fork::predicates::IdentifierNotFoundPredicate;
use icu_provider::prelude::*;
use icu_provider::hello_world::*;
use icu_locale::langid;
struct SingleLocaleProvider(DataLocale);
impl DataProvider<HelloWorldV1Marker> for SingleLocaleProvider {
fn load(&self, req: DataRequest) -> Result<DataResponse<HelloWorldV1Marker>, DataError> {
if *req.id.locale != self.0 {
return Err(DataErrorKind::IdentifierNotFound.with_req(HelloWorldV1Marker::INFO, req));
}
HelloWorldProvider.load(req)
}
}
let provider_de = SingleLocaleProvider(langid!("de").into());
let provider_ro = SingleLocaleProvider(langid!("ro").into());
// Create the forking provider:
let provider = ForkByErrorProvider::new_with_predicate(
provider_de,
provider_ro,
IdentifierNotFoundPredicate
);
// Test that we can load both "de" and "ro" data:
let german_hello_world: DataResponse<HelloWorldV1Marker> = provider
.load(DataRequest {
id: DataIdentifierBorrowed::for_locale(&langid!("de").into()),
..Default::default()
})
.expect("Loading should succeed");
assert_eq!("Hallo Welt", german_hello_world.payload.get().message);
let romanian_hello_world: DataResponse<HelloWorldV1Marker> = provider
.load(DataRequest {
id: DataIdentifierBorrowed::for_locale(&langid!("ro").into()),
..Default::default()
})
.expect("Loading should succeed");
assert_eq!("Salut, lume", romanian_hello_world.payload.get().message);
// We should not be able to load "en" data because it is not in either provider:
DataProvider::<HelloWorldV1Marker>::load(
&provider,
DataRequest {
id: DataIdentifierBorrowed::for_locale(&langid!("en").into()),
..Default::default()
}
)
.expect_err("No English data");
Trait Implementations§
source§impl Debug for IdentifierNotFoundPredicate
impl Debug for IdentifierNotFoundPredicate
source§impl ForkByErrorPredicate for IdentifierNotFoundPredicate
impl ForkByErrorPredicate for IdentifierNotFoundPredicate
source§const UNIT_ERROR: DataErrorKind = DataErrorKind::IdentifierNotFound
const UNIT_ERROR: DataErrorKind = DataErrorKind::IdentifierNotFound
The error to return if there are zero providers.
impl Eq for IdentifierNotFoundPredicate
impl StructuralPartialEq for IdentifierNotFoundPredicate
Auto Trait Implementations§
impl Freeze for IdentifierNotFoundPredicate
impl RefUnwindSafe for IdentifierNotFoundPredicate
impl Send for IdentifierNotFoundPredicate
impl Sync for IdentifierNotFoundPredicate
impl Unpin for IdentifierNotFoundPredicate
impl UnwindSafe for IdentifierNotFoundPredicate
Blanket Implementations§
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
Mutably borrows from an owned value. Read more
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>
Converts
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>
Converts
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