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: AImplementations§
Source§impl Rulebook<Rc<Vec<Rulebook>>>
impl Rulebook<Rc<Vec<Rulebook>>>
Sourcepub fn from_rulebooks<I: Iterator<Item = Rulebook>>(rulebooks: I) -> Self
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>)>
impl<RR, R> Rulebook<(Rc<Vec<RR>>, PhantomData<R>)>
Sourcepub fn from_ref_rulebooks<I: Iterator<Item = RR>>(rulebooks: I) -> Self
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
impl Rulebook
pub fn from_fn<F: Fn(&LanguageIdentifier) -> Vec<LanguageIdentifier> + 'static>( f: F, ) -> Self
pub const fn from_fns(rules: FnRules) -> Self
Sourcepub fn from_map<M, LS>(map: M) -> Selfwhere
M: for<'a> Index<&'a LanguageIdentifier, Output = LS> + 'static,
for<'b> &'b LS: IntoIterator<Item = &'b LanguageIdentifier>,
pub fn from_map<M, LS>(map: M) -> Selfwhere
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> PolyL10nRulebook<'_> for Rulebook<A>
impl<A> PolyL10nRulebook<'_> for Rulebook<A>
fn find_fallback_locale( &self, locale: &LanguageIdentifier, ) -> impl Iterator<Item = LanguageIdentifier>
fn find_fallback_locale_ref( &'s self, _: &LanguageIdentifier, ) -> impl Iterator<Item = &'s LanguageIdentifier>
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> 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> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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