From e3ede6a71ed96cfe78c2883a2fac1d0f9425b668 Mon Sep 17 00:00:00 2001 From: René Kijewski Date: Fri, 7 Jan 2022 00:53:54 +0100 Subject: Move extension_to_mime_type() to askama_shared --- askama_shared/src/input.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'askama_shared/src/input.rs') diff --git a/askama_shared/src/input.rs b/askama_shared/src/input.rs index 03c62d0..7d28a7e 100644 --- a/askama_shared/src/input.rs +++ b/askama_shared/src/input.rs @@ -3,6 +3,7 @@ use crate::{CompileError, Config, Syntax}; use std::path::{Path, PathBuf}; use std::str::FromStr; +use mime::Mime; use quote::ToTokens; pub struct TemplateInput<'a> { @@ -239,6 +240,32 @@ impl FromStr for Print { } } +#[doc(hidden)] +pub fn extension_to_mime_type(ext: &str) -> Mime { + let basic_type = mime_guess::from_ext(ext).first_or_octet_stream(); + for (simple, utf_8) in &TEXT_TYPES { + if &basic_type == simple { + return utf_8.clone(); + } + } + basic_type +} + +const TEXT_TYPES: [(Mime, Mime); 6] = [ + (mime::TEXT_PLAIN, mime::TEXT_PLAIN_UTF_8), + (mime::TEXT_HTML, mime::TEXT_HTML_UTF_8), + (mime::TEXT_CSS, mime::TEXT_CSS_UTF_8), + (mime::TEXT_CSV, mime::TEXT_CSV_UTF_8), + ( + mime::TEXT_TAB_SEPARATED_VALUES, + mime::TEXT_TAB_SEPARATED_VALUES_UTF_8, + ), + ( + mime::APPLICATION_JAVASCRIPT, + mime::APPLICATION_JAVASCRIPT_UTF_8, + ), +]; + #[cfg(test)] mod tests { use super::*; -- cgit