ARulebook

Struct ARulebook 

Source
pub struct ARulebook<A = ()> {
    pub rules: AFnRules,
    pub owned_values: A,
}
Expand description

A set of rules that govern how LocaleFallbackSolver should handle fallbacks.

This is the thread-safe version of Rulebook.

ARulebook<A>, regardless of type A, stores the rules as AFnRules, a vector of boxed dyn Fn(&LanguageIdentifier) -> Vec<LanguageIdentifier> + Send + Sync. Therefore, the actual correct name of this struct should be something along the lines of AFnsRulebook.

Obviously this rulebook can be used with the solver because it implements PolyL10nRulebook.

In addition, the default rulebook ARulebook::default() can and probably should be used for most situations you ever need to deal with.

Fields§

§rules: AFnRules§owned_values: A

Implementations§

Source§

impl ARulebook<Arc<Vec<ARulebook>>>

Source

pub fn from_rulebooks<I: Iterator<Item = ARulebook>>(rulebooks: I) -> Self

Combine multiple rulebooks into one.

See also: Self::from_ref_rulebooks.

§Examples
let rb1 = poly_l10n::ARulebook::from_fn(|l| {
  let mut l = l.clone();
  l.script = None;
  vec![l]
});
let rb2 = poly_l10n::ARulebook::from_fn(|l| {
  let mut l = l.clone();
  l.region = None;
  vec![l]
});
let rulebook = poly_l10n::ARulebook::from_rulebooks([rb1, rb2].into_iter());
let solv = poly_l10n::LocaleFallbackSolver { rulebook };

assert_eq!(
  solv.solve_locale(poly_l10n::langid!["zh-Hant-HK"]),
  poly_l10n::langid!["zh-HK", "zh-Hant", "zh"]
);
Source§

impl<RR, R> ARulebook<(Arc<Vec<RR>>, PhantomData<R>)>
where RR: AsRef<ARulebook<R>> + 'static + Send + Sync,

Source

pub fn from_ref_rulebooks<I: Iterator<Item = RR>>(rulebooks: I) -> Self

Combine multiple rulebooks into one. Each given rulebook r must implement AsRef::as_ref.

For the owned version, see Self::from_rulebooks.

§Examples
let rb1 = poly_l10n::ARulebook::from_fn(|l| {
  let mut l = l.clone();
  l.script = None;
  vec![l]
});
let rb2 = poly_l10n::ARulebook::from_fn(|l| {
  let mut l = l.clone();
  l.region = None;
  vec![l]
});
let (rb1, rb2) = (Arc::new(rb1), Arc::new(rb2));
let rulebook = poly_l10n::ARulebook::from_ref_rulebooks([rb1, rb2].iter().cloned());
let solv = poly_l10n::LocaleFallbackSolver { rulebook };

assert_eq!(
  solv.solve_locale(poly_l10n::langid!["zh-Hant-HK"]),
  poly_l10n::langid!["zh-HK", "zh-Hant", "zh"]
);
Source§

impl ARulebook

Source

pub fn from_fn<F: Fn(&LanguageIdentifier) -> Vec<LanguageIdentifier> + 'static + Send + Sync>( f: F, ) -> Self

Source

pub const fn from_fns(rules: AFnRules) -> Self

Source

pub fn from_map<M, LS>(map: M) -> Self
where M: for<'a> Index<&'a LanguageIdentifier, Output = LS> + 'static + Send + Sync, for<'b> &'b LS: IntoIterator<Item = &'b LanguageIdentifier>,

Convert a map (or anything that impl std::ops::Index<&LanguageIdentifier>) into a rulebook.

The output of the map must implement IntoIterator<Item = &LanguageIdentifier>.

While any valid arguments to this constructor are guaranteed to satisfy the trait PolyL10nRulebook, it could be useful to convert them to rulebooks, e.g. to combine multiple rulebooks using Self::from_rulebooks.

Trait Implementations§

Source§

impl<A: Debug> Debug for ARulebook<A>

Source§

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

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

impl Default for ARulebook

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<A> PolyL10nRulebook<'_> for ARulebook<A>

Auto Trait Implementations§

§

impl<A> Freeze for ARulebook<A>
where A: Freeze,

§

impl<A = ()> !RefUnwindSafe for ARulebook<A>

§

impl<A> Send for ARulebook<A>
where A: Send,

§

impl<A> Sync for ARulebook<A>
where A: Sync,

§

impl<A> Unpin for ARulebook<A>
where A: Unpin,

§

impl<A = ()> !UnwindSafe for ARulebook<A>

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more