/// Implement every integration for the given item pub(crate) fn impl_everything(ast: &DeriveInput, buf: &mut Buffer) {
impl_display(ast, buf);
impl_fast_writable(ast, buf);
}
/// Writes header for the `impl` for `TraitFromPathName` or `Template` for the given item pub(crate) fn write_header(ast: &DeriveInput, buf: &mut Buffer, target: impl Display) { let (impl_generics, orig_ty_generics, where_clause) = ast.generics.split_for_impl();
let ident = &ast.ident;
buf.write(format_args!( "impl {} {} for {} {{",
quote!(#impl_generics),
target,
quote!(#ident#orig_ty_generics#where_clause),
));
}
/// Implement `Display` for the given item. fn impl_display(ast: &DeriveInput, buf: &mut Buffer) { let ident = &ast.ident;
buf.write(format_args!( "\ /// Implement the [`format!()`][askama::helpers::std::format] trait for [`{}`]\n\ ///\n\ /// Please be aware of the rendering performance notice in the \
[`Template`][askama::Template] trait.\n\ ",
quote!(#ident),
));
write_header(ast, buf, "askama::helpers::core::fmt::Display");
buf.write( "\ #[inline]\ fn fmt(\
&self,\
f: &mut askama::helpers::core::fmt::Formatter<'_>\
) -> askama::helpers::core::fmt::Result {\
askama::Template::render_into(self, f)\
.map_err(|_| askama::helpers::core::fmt::Error)\
}\
}",
);
}
/// Similar to `write!(dest, "{src:?}")`, but only escapes the strictly needed characters, /// and without the surrounding `"…"` quotation marks. fn string_escape(dest: &mut String, src: &str) { // SAFETY: we will only push valid str slices let dest = unsafe { dest.as_mut_vec() }; let src = src.as_bytes(); letmut last = 0;
// According to <https://doc.rust-lang.org/reference/tokens.html#string-literals>, every // character is valid except `" \ IsolatedCR`. We don't test if the `\r` is isolated or not, // but always escape it. for x in memchr::memchr3_iter(b'\\', b'"', b'\r', src) {
dest.extend(&src[last..x]);
dest.extend(match src[x] {
b'\\' => br"\\",
b'\"' => br#"\""#,
_ => br"\r",
});
last = x + 1;
}
dest.extend(&src[last..]);
}
fn set_default<S, T, A>(dest: &mut S, parent: &mut S, mut access: A) where
T: Clone,
A: FnMut(&mut S) -> &mut Option<T>,
{ let dest = access(dest); if dest.is_none() { iflet Some(parent) = access(parent) {
*dest = Some(parent.clone());
}
}
}
/// Generates a `struct` to contain the data of an enum variant fn type_for_enum_variant(
enum_ast: &DeriveInput,
enum_generics: &Generics,
var: &Variant,
) -> DeriveInput { let enum_id = &enum_ast.ident; let (_, ty_generics, _) = enum_ast.generics.split_for_impl(); let lt = enum_generics.params.first().unwrap();
let id = &var.ident; let span = id.span(); let id = Ident::new(&format!("__Askama__{enum_id}__{id}"), span);
let phantom: Type = parse_quote! {
askama::helpers::core::marker::PhantomData < &#lt#enum_id#ty_generics >
}; let fields = match &var.fields {
Fields::Named(fields) => { letmut fields = fields.clone(); for f in fields.named.iter_mut() { let ty = &f.ty;
f.ty = parse_quote!(&#lt#ty);
} let id = Ident::new(&format!("__Askama__{enum_id}__phantom"), span);
fields.named.push(parse_quote!(#id: #phantom));
Fields::Named(fields)
}
Fields::Unnamed(fields) => { letmut fields = fields.clone(); for f in fields.unnamed.iter_mut() { let ty = &f.ty;
f.ty = parse_quote!(&#lt#ty);
}
fields.unnamed.push(parse_quote!(#phantom));
Fields::Unnamed(fields)
}
Fields::Unit => Fields::Unnamed(parse_quote!((#phantom))),
}; let semicolon = match &var.fields {
Fields::Named(_) => None,
_ => Some(Token),
};
/// Generates a `match` arm for an `enum` variant, that calls `<_ as EnumVariantTemplate>::render_into()` /// for that type and data fn variant_as_arm(
var_ast: &DeriveInput,
var: &Variant,
size_hint: usize,
render_into_arms: &mut TokenStream,
size_hint_arms: &mut TokenStream,
) { let var_id = &var_ast.ident; let ident = &var.ident; let span = ident.span();
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.