Skip to main content

IdentifierNotFoundPredicate

Struct IdentifierNotFoundPredicate 

Source
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<HelloWorldV1> for SingleLocaleProvider {
    fn load(&self, req: DataRequest) -> Result<DataResponse<HelloWorldV1>, DataError> {
        if *req.id.locale != self.0 {
            return Err(DataErrorKind::IdentifierNotFound.with_req(HelloWorldV1::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<HelloWorldV1> = 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<HelloWorldV1> = 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::<HelloWorldV1>::load(
    &provider,
    DataRequest {
        id: DataIdentifierBorrowed::for_locale(&langid!("en").into()),
        ..Default::default()
    }
)
.expect_err("No English data");

Trait Implementations§

Source§

impl Debug for IdentifierNotFoundPredicate

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for IdentifierNotFoundPredicate

Source§

impl ForkByErrorPredicate for IdentifierNotFoundPredicate

Source§

const UNIT_ERROR: DataErrorKind = DataErrorKind::IdentifierNotFound

The error to return if there are zero providers.
Source§

fn test( &self, _: DataMarkerInfo, _: Option<DataRequest<'_>>, err: DataError, ) -> bool

This function is called when a data request fails and there are additional providers that could possibly fulfill the request. Read more
Source§

impl PartialEq for IdentifierNotFoundPredicate

Source§

fn eq(&self, other: &IdentifierNotFoundPredicate) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for IdentifierNotFoundPredicate

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.