mirror of
https://github.com/askama-rs/askama.git
synced 2025-10-02 15:25:19 +00:00
derive: do not clone #[doc]
attributes
This commit is contained in:
parent
9527d14d7f
commit
0d39b84fc0
@ -290,16 +290,7 @@ pub(crate) struct TemplateArgs {
|
|||||||
|
|
||||||
impl TemplateArgs {
|
impl TemplateArgs {
|
||||||
pub(crate) fn new(ast: &syn::DeriveInput) -> Result<Self, CompileError> {
|
pub(crate) fn new(ast: &syn::DeriveInput) -> Result<Self, CompileError> {
|
||||||
// FIXME: implement once <https://github.com/rust-lang/rfcs/pull/3715> is stable
|
let args = PartialTemplateArgs::new(ast, &ast.attrs)?;
|
||||||
if let syn::Data::Union(data) = &ast.data {
|
|
||||||
return Err(CompileError::new_with_span(
|
|
||||||
"rinja templates are not supported for `union` types, only `struct` and `enum`",
|
|
||||||
None,
|
|
||||||
Some(data.union_token.span),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
let args = PartialTemplateArgs::new(&ast.attrs)?;
|
|
||||||
let Some(template) = args.template else {
|
let Some(template) = args.template else {
|
||||||
return Err(CompileError::no_file_info(
|
return Err(CompileError::no_file_info(
|
||||||
"no attribute `template` found",
|
"no attribute `template` found",
|
||||||
@ -315,9 +306,7 @@ impl TemplateArgs {
|
|||||||
(Source::Source(s.value().into()), Some(s.span()))
|
(Source::Source(s.value().into()), Some(s.span()))
|
||||||
}
|
}
|
||||||
#[cfg(feature = "code-in-doc")]
|
#[cfg(feature = "code-in-doc")]
|
||||||
Some(PartialTemplateArgsSource::InDoc(s)) => {
|
Some(PartialTemplateArgsSource::InDoc(span, source)) => (source, Some(span)),
|
||||||
source_from_docs(s.span(), &args.meta_docs, ast)?
|
|
||||||
}
|
|
||||||
None => {
|
None => {
|
||||||
return Err(CompileError::no_file_info(
|
return Err(CompileError::no_file_info(
|
||||||
#[cfg(not(feature = "code-in-doc"))]
|
#[cfg(not(feature = "code-in-doc"))]
|
||||||
@ -368,7 +357,7 @@ impl TemplateArgs {
|
|||||||
#[cfg(feature = "code-in-doc")]
|
#[cfg(feature = "code-in-doc")]
|
||||||
fn source_from_docs(
|
fn source_from_docs(
|
||||||
span: Span,
|
span: Span,
|
||||||
docs: &[Attribute],
|
docs: &[&Attribute],
|
||||||
ast: &syn::DeriveInput,
|
ast: &syn::DeriveInput,
|
||||||
) -> Result<(Source, Option<Span>), CompileError> {
|
) -> Result<(Source, Option<Span>), CompileError> {
|
||||||
let (source_span, source) = collect_comment_blocks(span, docs, ast)?;
|
let (source_span, source) = collect_comment_blocks(span, docs, ast)?;
|
||||||
@ -380,7 +369,7 @@ fn source_from_docs(
|
|||||||
#[cfg(feature = "code-in-doc")]
|
#[cfg(feature = "code-in-doc")]
|
||||||
fn collect_comment_blocks(
|
fn collect_comment_blocks(
|
||||||
span: Span,
|
span: Span,
|
||||||
docs: &[Attribute],
|
docs: &[&Attribute],
|
||||||
ast: &syn::DeriveInput,
|
ast: &syn::DeriveInput,
|
||||||
) -> Result<(Option<Span>, String), CompileError> {
|
) -> Result<(Option<Span>, String), CompileError> {
|
||||||
let mut source_span: Option<Span> = None;
|
let mut source_span: Option<Span> = None;
|
||||||
@ -652,7 +641,6 @@ pub(crate) fn get_template_source(
|
|||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub(crate) struct PartialTemplateArgs {
|
pub(crate) struct PartialTemplateArgs {
|
||||||
pub(crate) template: Option<Ident>,
|
pub(crate) template: Option<Ident>,
|
||||||
pub(crate) meta_docs: Vec<Attribute>,
|
|
||||||
pub(crate) source: Option<PartialTemplateArgsSource>,
|
pub(crate) source: Option<PartialTemplateArgsSource>,
|
||||||
pub(crate) block: Option<LitStr>,
|
pub(crate) block: Option<LitStr>,
|
||||||
pub(crate) print: Option<Print>,
|
pub(crate) print: Option<Print>,
|
||||||
@ -663,34 +651,54 @@ pub(crate) struct PartialTemplateArgs {
|
|||||||
pub(crate) whitespace: Option<Whitespace>,
|
pub(crate) whitespace: Option<Whitespace>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
pub(crate) enum PartialTemplateArgsSource {
|
pub(crate) enum PartialTemplateArgsSource {
|
||||||
Path(LitStr),
|
Path(LitStr),
|
||||||
Source(LitStr),
|
Source(LitStr),
|
||||||
#[cfg(feature = "code-in-doc")]
|
#[cfg(feature = "code-in-doc")]
|
||||||
InDoc(#[allow(dead_code)] LitBool),
|
InDoc(Span, Source),
|
||||||
}
|
}
|
||||||
|
|
||||||
// implement PartialTemplateArgs::new()
|
// implement PartialTemplateArgs::new()
|
||||||
const _: () = {
|
const _: () = {
|
||||||
impl PartialTemplateArgs {
|
impl PartialTemplateArgs {
|
||||||
pub(crate) fn new(attrs: &[Attribute]) -> Result<Self, CompileError> {
|
pub(crate) fn new(
|
||||||
new(attrs)
|
ast: &syn::DeriveInput,
|
||||||
|
attrs: &[Attribute],
|
||||||
|
) -> Result<Self, CompileError> {
|
||||||
|
new(ast, attrs)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn new(attrs: &[Attribute]) -> Result<PartialTemplateArgs, CompileError> {
|
fn new(
|
||||||
|
ast: &syn::DeriveInput,
|
||||||
|
attrs: &[Attribute],
|
||||||
|
) -> Result<PartialTemplateArgs, CompileError> {
|
||||||
|
// FIXME: implement once <https://github.com/rust-lang/rfcs/pull/3715> is stable
|
||||||
|
if let syn::Data::Union(data) = &ast.data {
|
||||||
|
return Err(CompileError::new_with_span(
|
||||||
|
"rinja templates are not supported for `union` types, only `struct` and `enum`",
|
||||||
|
None,
|
||||||
|
Some(data.union_token.span),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "code-in-doc")]
|
||||||
|
let mut meta_docs = vec![];
|
||||||
|
|
||||||
let mut this = PartialTemplateArgs::default();
|
let mut this = PartialTemplateArgs::default();
|
||||||
for attr in attrs {
|
for attr in attrs {
|
||||||
let Some(ident) = attr.path().get_ident() else {
|
let Some(ident) = attr.path().get_ident() else {
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
if ident == "doc" {
|
if ident == "template" {
|
||||||
this.meta_docs.push(attr.clone());
|
|
||||||
continue;
|
|
||||||
} else if ident == "template" {
|
|
||||||
this.template = Some(ident.clone());
|
this.template = Some(ident.clone());
|
||||||
} else {
|
} else {
|
||||||
|
#[cfg(feature = "code-in-doc")]
|
||||||
|
if ident == "doc" {
|
||||||
|
meta_docs.push(attr);
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -742,7 +750,10 @@ const _: () = {
|
|||||||
}
|
}
|
||||||
#[cfg(feature = "code-in-doc")]
|
#[cfg(feature = "code-in-doc")]
|
||||||
{
|
{
|
||||||
this.source = Some(PartialTemplateArgsSource::InDoc(value));
|
this.source = Some(PartialTemplateArgsSource::InDoc(
|
||||||
|
value.span(),
|
||||||
|
Source::Path("".into()),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
} else if ident == "block" {
|
} else if ident == "block" {
|
||||||
set_strlit_pair(ident, value, &mut this.block)?;
|
set_strlit_pair(ident, value, &mut this.block)?;
|
||||||
@ -766,6 +777,16 @@ const _: () = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "code-in-doc")]
|
||||||
|
if let Some(PartialTemplateArgsSource::InDoc(lit_span, _)) = this.source {
|
||||||
|
let (source, doc_span) = source_from_docs(lit_span, &meta_docs, ast)?;
|
||||||
|
this.source = Some(PartialTemplateArgsSource::InDoc(
|
||||||
|
doc_span.unwrap_or(lit_span),
|
||||||
|
source,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
Ok(this)
|
Ok(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user