Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove support for deprecated _parent field #613

Merged
merged 1 commit into from
Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 0 additions & 23 deletions askama_derive/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,6 @@ impl<'a> Generator<'a> {
// Takes a Context and generates the relevant implementations.
fn build(mut self, ctx: &'a Context<'_>) -> Result<String, CompileError> {
let mut buf = Buffer::new(0);
if !ctx.blocks.is_empty() {
if let Some(parent) = self.input.parent {
self.deref_to_parent(&mut buf, parent)?;
}
};

self.impl_template(ctx, &mut buf)?;
self.impl_display(&mut buf)?;
Expand Down Expand Up @@ -376,24 +371,6 @@ impl<'a> Generator<'a> {
Ok(())
}

// Implement `Deref<Parent>` for an inheriting context struct.
fn deref_to_parent(
&mut self,
buf: &mut Buffer,
parent_type: &syn::Type,
) -> Result<(), CompileError> {
self.write_header(buf, "::std::ops::Deref", None)?;
buf.writeln(&format!(
"type Target = {};",
parent_type.into_token_stream()
))?;
buf.writeln("#[inline]")?;
buf.writeln("fn deref(&self) -> &Self::Target {")?;
buf.writeln("&self._parent")?;
buf.writeln("}")?;
buf.writeln("}")
}

// Implement `Display` for the given context struct.
fn impl_display(&mut self, buf: &mut Buffer) -> Result<(), CompileError> {
self.write_header(buf, "::std::fmt::Display", None)?;
Expand Down
26 changes: 1 addition & 25 deletions askama_derive/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@ pub(crate) struct TemplateInput<'a> {
pub(crate) escaper: &'a str,
pub(crate) ext: Option<String>,
pub(crate) mime_type: String,
pub(crate) parent: Option<&'a syn::Type>,
pub(crate) path: PathBuf,
}

impl TemplateInput<'_> {
/// Extract the template metadata from the `DeriveInput` structure. This
/// mostly recovers the data for the `TemplateInput` fields from the
/// `template()` attribute list fields; it also finds the of the `_parent`
/// field, if any.
/// `template()` attribute list fields.
pub(crate) fn new<'n>(
ast: &'n syn::DeriveInput,
config: &'n Config<'_>,
Expand All @@ -51,27 +49,6 @@ impl TemplateInput<'_> {
}
};

// Check to see if a `_parent` field was defined on the context
// struct, and store the type for it for use in the code generator.
let parent = match ast.data {
syn::Data::Struct(syn::DataStruct {
fields: syn::Fields::Named(ref fields),
..
}) => fields
.named
.iter()
.find(|f| f.ident.as_ref().filter(|name| *name == "_parent").is_some())
.map(|f| &f.ty),
_ => None,
};

if parent.is_some() {
eprint!(
" --> in struct {}\n = use of deprecated field '_parent'\n",
ast.ident
);
}

// Validate syntax
let syntax = syntax.map_or_else(
|| Ok(config.syntaxes.get(config.default_syntax).unwrap()),
Expand Down Expand Up @@ -117,7 +94,6 @@ impl TemplateInput<'_> {
escaper,
ext,
mime_type,
parent,
path,
})
}
Expand Down
44 changes: 40 additions & 4 deletions testing/tests/inheritance.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::ops::Deref;

use askama::Template;

#[derive(Template)]
Expand All @@ -9,7 +11,15 @@ struct BaseTemplate<'a> {
#[derive(Template)]
#[template(path = "child.html")]
struct ChildTemplate<'a> {
_parent: BaseTemplate<'a>,
_parent: &'a BaseTemplate<'a>,
}

impl<'a> Deref for ChildTemplate<'a> {
type Target = BaseTemplate<'a>;

fn deref(&self) -> &Self::Target {
self._parent
}
}

#[test]
Expand All @@ -21,7 +31,7 @@ fn test_use_base_directly() {
#[test]
fn test_simple_extends() {
let t = ChildTemplate {
_parent: BaseTemplate { title: "Bar" },
_parent: &BaseTemplate { title: "Bar" },
};
assert_eq!(
t.render().unwrap(),
Expand All @@ -43,6 +53,7 @@ fn test_empty_child() {

pub mod parent {
use askama::Template;

#[derive(Template)]
#[template(path = "base.html")]
pub struct BaseTemplate<'a> {
Expand All @@ -53,17 +64,26 @@ pub mod parent {
pub mod child {
use super::parent::*;
use askama::Template;

#[derive(Template)]
#[template(path = "child.html")]
pub struct ChildTemplate<'a> {
pub _parent: BaseTemplate<'a>,
pub _parent: &'a BaseTemplate<'a>,
}

impl<'a> std::ops::Deref for ChildTemplate<'a> {
type Target = BaseTemplate<'a>;

fn deref(&self) -> &Self::Target {
self._parent
}
}
}

#[test]
fn test_different_module() {
let t = child::ChildTemplate {
_parent: parent::BaseTemplate { title: "a" },
_parent: &parent::BaseTemplate { title: "a" },
};
assert_eq!(
t.render().unwrap(),
Expand All @@ -81,6 +101,14 @@ struct NestedChildTemplate {
_parent: NestedBaseTemplate,
}

impl Deref for NestedChildTemplate {
type Target = NestedBaseTemplate;

fn deref(&self) -> &Self::Target {
&self._parent
}
}

#[test]
fn test_nested_blocks() {
let t = NestedChildTemplate {
Expand Down Expand Up @@ -109,6 +137,14 @@ struct DeepKidTemplate {
item: String,
}

impl Deref for DeepKidTemplate {
type Target = DeepMidTemplate;

fn deref(&self) -> &Self::Target {
&self._parent
}
}

#[test]
fn test_deep() {
let t = DeepKidTemplate {
Expand Down