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
1 changed files with 7 additions and 9 deletions

View File

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