Remove EXTENSION constant from Template

I doubt that is needed in any circumstance, and it makes implementing
enum variants harder.
This commit is contained in:
René Kijewski 2024-11-18 16:50:07 +01:00
parent c6f8bfabc5
commit 968b80fcb8
6 changed files with 1 additions and 124 deletions

View File

@ -134,9 +134,6 @@ pub trait Template: fmt::Display {
}
}
/// The template's extension, if provided
const EXTENSION: Option<&'static str>;
/// Provides a rough estimate of the expanded length of the rendered template. Larger
/// values result in higher memory usage but fewer reallocations. Smaller values result in the
/// opposite. This value only affects [`render`]. It does not take effect when calling
@ -168,8 +165,6 @@ impl<T: Template + ?Sized> Template for &T {
T::write_into(self, writer)
}
const EXTENSION: Option<&'static str> = T::EXTENSION;
const SIZE_HINT: usize = T::SIZE_HINT;
const MIME_TYPE: &'static str = T::MIME_TYPE;
@ -188,9 +183,6 @@ pub trait DynTemplate {
/// Renders the template to the given `writer` io buffer
fn dyn_write_into(&self, writer: &mut dyn io::Write) -> io::Result<()>;
/// Helper function to inspect the template's extension
fn extension(&self) -> Option<&'static str>;
/// Provides a conservative estimate of the expanded length of the rendered template
fn size_hint(&self) -> usize;
@ -212,10 +204,6 @@ impl<T: Template> DynTemplate for T {
<Self as Template>::write_into(self, writer)
}
fn extension(&self) -> Option<&'static str> {
Self::EXTENSION
}
fn size_hint(&self) -> usize {
Self::SIZE_HINT
}
@ -271,8 +259,6 @@ mod tests {
Ok(writer.write_str("test")?)
}
const EXTENSION: Option<&'static str> = Some("txt");
const SIZE_HINT: usize = 4;
const MIME_TYPE: &'static str = "text/plain; charset=utf-8";

View File

@ -171,12 +171,8 @@ impl<'a> Generator<'a> {
"\
rinja::Result::Ok(())\
}}\
const EXTENSION:\
rinja::helpers::core::option::Option<&'static rinja::helpers::core::primitive::str> =\
rinja::helpers::core::option::Option::{:?};\
const SIZE_HINT: rinja::helpers::core::primitive::usize = {size_hint}usize;\
const MIME_TYPE: &'static rinja::helpers::core::primitive::str = {:?};",
self.input.extension(),
self.input.mime_type,
));

View File

@ -26,7 +26,6 @@ pub(crate) struct TemplateInput<'a> {
pub(crate) block: Option<&'a str>,
pub(crate) print: Print,
pub(crate) escaper: &'a str,
pub(crate) ext: Option<&'a str>,
pub(crate) mime_type: String,
pub(crate) path: Arc<Path>,
pub(crate) fields: Vec<String>,
@ -108,7 +107,7 @@ impl TemplateInput<'_> {
})?;
let mime_type =
extension_to_mime_type(ext_default_to_path(ext.as_deref(), &path).unwrap_or("txt"))
extension_to_mime_type(ext.as_deref().or_else(|| extension(&path)).unwrap_or("txt"))
.to_string();
let empty_punctuated = Punctuated::new();
@ -139,7 +138,6 @@ impl TemplateInput<'_> {
block: block.as_deref(),
print: *print,
escaper,
ext: ext.as_deref(),
mime_type,
path,
fields,
@ -272,11 +270,6 @@ impl TemplateInput<'_> {
}
Ok(())
}
#[inline]
pub(crate) fn extension(&self) -> Option<&str> {
ext_default_to_path(self.ext, &self.path)
}
}
#[derive(Debug)]
@ -539,11 +532,6 @@ impl<I: Iterator, E> Iterator for ResultIter<I, E> {
impl<I: FusedIterator, E> FusedIterator for ResultIter<I, E> {}
#[inline]
fn ext_default_to_path<'a>(ext: Option<&'a str>, path: &'a Path) -> Option<&'a str> {
ext.or_else(|| extension(path))
}
fn extension(path: &Path) -> Option<&str> {
let ext = path.extension()?.to_str()?;
if JINJA_EXTENSIONS.contains(&ext) {

View File

@ -30,9 +30,6 @@ fn compare(jinja: &str, expected: &str, fields: &[(&str, &str)], size_hint: usiz
#expected
rinja::Result::Ok(())
}
const EXTENSION:
rinja::helpers::core::option::Option<&'static rinja::helpers::core::primitive::str> =
rinja::helpers::core::option::Option::Some("txt");
const SIZE_HINT: rinja::helpers::core::primitive::usize = #size_hint;
const MIME_TYPE: &'static rinja::helpers::core::primitive::str = "text/plain; charset=utf-8";
}

View File

@ -1,89 +0,0 @@
use rinja::Template;
#[test]
fn test_path_ext_html() {
#[derive(Template)]
#[template(path = "foo.html")]
struct PathHtml;
let t = PathHtml;
assert_eq!(t.render().unwrap(), "foo.html");
assert_eq!(PathHtml::EXTENSION, Some("html"));
}
#[test]
fn test_path_ext_jinja() {
#[derive(Template)]
#[template(path = "foo.jinja")]
struct PathJinja;
let t = PathJinja;
assert_eq!(t.render().unwrap(), "foo.jinja");
assert_eq!(PathJinja::EXTENSION, Some("jinja"));
}
#[test]
fn test_path_ext_html_jinja() {
#[derive(Template)]
#[template(path = "foo.html.jinja")]
struct PathHtmlJinja;
let t = PathHtmlJinja;
assert_eq!(t.render().unwrap(), "foo.html.jinja");
assert_eq!(PathHtmlJinja::EXTENSION, Some("html"));
}
#[test]
fn test_path_ext_html_and_ext_txt() {
#[derive(Template)]
#[template(path = "foo.html", ext = "txt")]
struct PathHtmlAndExtTxt;
let t = PathHtmlAndExtTxt;
assert_eq!(t.render().unwrap(), "foo.html");
assert_eq!(PathHtmlAndExtTxt::EXTENSION, Some("txt"));
}
#[test]
fn test_path_ext_jinja_and_ext_txt() {
#[derive(Template)]
#[template(path = "foo.jinja", ext = "txt")]
struct PathJinjaAndExtTxt;
let t = PathJinjaAndExtTxt;
assert_eq!(t.render().unwrap(), "foo.jinja");
assert_eq!(PathJinjaAndExtTxt::EXTENSION, Some("txt"));
}
#[test]
fn test_path_ext_html_jinja_and_ext_txt() {
#[derive(Template)]
#[template(path = "foo.html.jinja", ext = "txt")]
struct PathHtmlJinjaAndExtTxt;
let t = PathHtmlJinjaAndExtTxt;
assert_eq!(t.render().unwrap(), "foo.html.jinja");
assert_eq!(PathHtmlJinjaAndExtTxt::EXTENSION, Some("txt"));
}
#[test]
fn test_path_ext_rinja() {
#[derive(Template)]
#[template(path = "foo.rinja")]
struct PathRinja;
let t = PathRinja;
assert_eq!(t.render().unwrap(), "foo.rinja");
assert_eq!(PathRinja::EXTENSION, Some("rinja"));
}
#[test]
fn test_path_ext_html_rinja() {
#[derive(Template)]
#[template(path = "foo.html.rinja")]
struct PathHtmlRinja;
let t = PathHtmlRinja;
assert_eq!(t.render().unwrap(), "foo.html.rinja");
assert_eq!(PathHtmlRinja::EXTENSION, Some("html"));
}

View File

@ -31,7 +31,6 @@ fn test_variables() {
Iñtërnâtiônàlizætiøn is important\n\
in vars too: Iñtërnâtiônàlizætiøn"
);
assert_eq!(VariablesTemplate::EXTENSION, Some("html"));
}
#[test]