From 4ead8e8c9302ce67d322e51909f0897082483ca3 Mon Sep 17 00:00:00 2001 From: mash Date: Fri, 29 Jun 2018 13:23:15 +0000 Subject: Simplify escape mode determination --- askama_derive/src/input.rs | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) (limited to 'askama_derive/src/input.rs') diff --git a/askama_derive/src/input.rs b/askama_derive/src/input.rs index cfc8855..cf1ad90 100644 --- a/askama_derive/src/input.rs +++ b/askama_derive/src/input.rs @@ -4,7 +4,7 @@ use quote::ToTokens; use shared::path; -use std::path::{Path, PathBuf}; +use std::path::PathBuf; use syn; @@ -98,24 +98,6 @@ impl<'a> TemplateInput<'a> { } }; - let escaping = match escaping { - Some(m) => m, - None => { - let ext = match source { - Source::Path(ref p) => Path::new(p) - .extension() - .map(|s| s.to_str().unwrap()) - .unwrap_or(""), - Source::Source(_) => ext.as_ref().unwrap(), // Already panicked if None - }; - if HTML_EXTENSIONS.contains(&ext) { - EscapeMode::Html - } else { - EscapeMode::None - } - } - }; - let parent = match ast.data { syn::Data::Struct(syn::DataStruct { fields: syn::Fields::Named(ref fields), @@ -132,7 +114,7 @@ impl<'a> TemplateInput<'a> { ast, source, print, - escaping, + escaping: escaping.unwrap_or_else(|| EscapeMode::from_path(&path)), ext, parent, path, @@ -162,6 +144,17 @@ impl From for EscapeMode { } } +impl EscapeMode { + fn from_path(path: &PathBuf) -> EscapeMode { + let extension = path.extension().map(|s| s.to_str().unwrap()).unwrap_or(""); + if HTML_EXTENSIONS.contains(&extension) { + EscapeMode::Html + } else { + EscapeMode::None + } + } +} + #[derive(PartialEq)] pub enum Print { All, -- cgit