Use 'or_else' to tidy up 'Template::show()'.

This commit is contained in:
Sergio Benitez 2018-07-02 19:14:06 -07:00
parent ce8817dd3c
commit 88d3b59c71

View File

@ -287,15 +287,13 @@ impl Template {
pub fn show<S, C>(rocket: &Rocket, name: S, context: C) -> Option<String> pub fn show<S, C>(rocket: &Rocket, name: S, context: C) -> Option<String>
where S: Into<Cow<'static, str>>, C: Serialize where S: Into<Cow<'static, str>>, C: Serialize
{ {
let ctxt = match rocket.state::<Context>() { let ctxt = rocket.state::<Context>().or_else(|| {
Some(ctxt) => ctxt, warn!("Uninitialized template context: missing fairing.");
None => { info!("To use templates, you must attach `Template::fairing()`.");
warn!("Uninitialized template context: missing fairing."); info!("See the `Template` documentation for more information.");
info!("To use templates, you must attach `Template::fairing()`."); None
info!("See the `Template` documentation for more information."); })?;
return None;
}
};
Template::render(name, context).finalize(&ctxt).ok().map(|v| v.0) Template::render(name, context).finalize(&ctxt).ok().map(|v| v.0)
} }