Rulebook

Struct Rulebook 

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

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

For the thread-safe version, see ARulebook<A>.

Rulebook<A>, regardless of type A, stores the rules as FnRules, a vector of boxed dyn Fn(&LanguageIdentifier) -> Vec<LanguageIdentifier>. Therefore, the actual correct name of this struct should be something along the lines of FnsRulebook.

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

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

Fields§

§rules: FnRules§owned_values: A

Implementations§

Source§

impl Rulebook<Rc<Vec<Rulebook>>>

Source

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

Combine multiple rulebooks into one.

See also: Self::from_ref_rulebooks.

§Examples
let rb1 = poly_l10n::Rulebook::from_fn(|l| {
  let mut l = l.clone();
  l.script = None;
  vec![l]
});
let rb2 = poly_l10n::Rulebook::from_fn(|l| {
  let mut l = l.clone();
  l.region = None;
  vec![l]
});
let rulebook = poly_l10n::Rulebook::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> Rulebook<(Rc<Vec<RR>>, PhantomData<R>)>
where RR: AsRef<Rulebook<R>> + 'static,

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::Rulebook::from_fn(|l| {
  let mut l = l.clone();
  l.script = None;
  vec![l]
});
let rb2 = poly_l10n::Rulebook::from_fn(|l| {
  let mut l = l.clone();
  l.region = None;
  vec![l]
});
let (rb1, rb2) = (Rc::new(rb1), Rc::new(rb2));
let rulebook = poly_l10n::Rulebook::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 Rulebook

Source

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

Source

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

Source

pub fn from_map<M, LS>(map: M) -> Self
where M: for<'a> Index<&'a LanguageIdentifier, Output = LS> + 'static, 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 Rulebook<A>

Source§

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

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

impl Default for Rulebook

Source§

fn default() -> Self

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

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

Auto Trait Implementations§

§

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

§

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

§

impl<A = ()> !Send for Rulebook<A>

§

impl<A = ()> !Sync for Rulebook<A>

§

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

§

impl<A = ()> !UnwindSafe for Rulebook<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