Struct rocket_lang::Config
source · [−]Expand description
This struct allows for customization of LangCode’s
behavior.
The precedence for every configuration is:
- custom closure
- from url
- accept language header
- wildcard
If none of these are able to produce an Ok value, an error is returned. Note that returning errors is discouraged, as it may lead to a poor user experience.
Custom closure
This configuration has the biggest precedence. There is full access to the incoming request.
let config = Config::new().custom(|request: &Request| {
Ok(lang_from_request(request))
});Url
The url method can be used to specify which segment should be interpreted as a language code. Negative indexes can be used to refer to positions relative to the last segment. Thus -1 corresponds to the last segment, -2 the second to last, and so on.
examples
// we use -1 to specify that the last segment is our language code
let config = Config::new().url(-1);
// we have to specify which language we want on the handler
#[get("/index/en")
fn english_language() {
/* ... */
}
// we can handle all languages at once with a wildcard segment.
// if we visit with an invalid language code ("/index/not-a-lang-code/"), an error is returned.
#[get("/index/<_>", rank = 2)
fn any_language() {
/* ... */
}Accept Language
The accept language header qualities can be set by indexing into the config struct. By default, all values are set to 0.0. These values should correspond to a number between 0.0 and 1.0 specifying the quality of that language support of your site.
let config = Config::new();
config[En] = 0.3;
config[Ar] = 1.0;Wildcard
The wildcard will be used to create a value if none of the previous attempts succeeded.
Note that wildcards are useful for single language applications, but they may not scale as well as url resolution.
By default the wildcard is set to None.
let config = Config::new().url(1).wildcard(Es);Fields
wildcard: Option<LangCode>Implementations
sourceimpl Config
impl Config
sourcepub fn wildcard(self, lang: LangCode) -> Self
pub fn wildcard(self, lang: LangCode) -> Self
The wildcard is used as a last resort if all other options failed.
sourcepub fn url(self, position: i32) -> Self
pub fn url(self, position: i32) -> Self
Takes the Config structure by value and returns a new one with the url configuration set. The position parameter determines which path segment will be interpreted as a language code. Negative positions will be interpreted as being relative to the last path segment.
Trait Implementations
sourceimpl Fairing for Config
impl Fairing for Config
sourcefn on_ignite<'life0, 'async_trait>(
&'life0 self,
rocket: Rocket<Build>
) -> Pin<Box<dyn Future<Output = Result> + Send + 'async_trait>> where
'life0: 'async_trait,
Self: 'async_trait,
fn on_ignite<'life0, 'async_trait>(
&'life0 self,
rocket: Rocket<Build>
) -> Pin<Box<dyn Future<Output = Result> + Send + 'async_trait>> where
'life0: 'async_trait,
Self: 'async_trait,
The ignite callback. Returns Ok if ignition should proceed and Err
if ignition and launch should be aborted. Read more
sourcefn on_request<'life0, 'life1, 'life2, 'life3, 'life4, 'async_trait>(
&'life0 self,
req: &'life1 mut Request<'life2>,
_data: &'life3 mut Data<'life4>
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>> where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
'life4: 'async_trait,
Self: 'async_trait,
fn on_request<'life0, 'life1, 'life2, 'life3, 'life4, 'async_trait>(
&'life0 self,
req: &'life1 mut Request<'life2>,
_data: &'life3 mut Data<'life4>
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>> where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
'life4: 'async_trait,
Self: 'async_trait,
The request callback. Read more
sourcefn on_liftoff<'life0, 'life1, 'async_trait>(
&'life0 self,
_rocket: &'life1 Rocket<Orbit>
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait, Global>> where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
fn on_liftoff<'life0, 'life1, 'async_trait>(
&'life0 self,
_rocket: &'life1 Rocket<Orbit>
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait, Global>> where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
The liftoff callback. Read more
sourcefn on_response<'r, 'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
_req: &'r Request<'life1>,
_res: &'life2 mut Response<'r>
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait, Global>> where
'r: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Self: 'async_trait,
fn on_response<'r, 'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
_req: &'r Request<'life1>,
_res: &'life2 mut Response<'r>
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait, Global>> where
'r: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Self: 'async_trait,
The response callback. Read more
Auto Trait Implementations
impl !RefUnwindSafe for Config
impl Send for Config
impl Sync for Config
impl Unpin for Config
impl !UnwindSafe for Config
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<T> IntoCollection<T> for T
impl<T> IntoCollection<T> for T
fn into_collection<A>(self) -> SmallVec<A> where
A: Array<Item = T>,
fn into_collection<A>(self) -> SmallVec<A> where
A: Array<Item = T>,
Converts self into a collection.
fn mapped<U, F, A>(self, f: F) -> SmallVec<A> where
F: FnMut(T) -> U,
A: Array<Item = U>,
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into)Uses borrowed data to replace owned data, usually by cloning. Read more
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
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
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber to this type, returning a
WithDispatch wrapper. Read more