askama/testing/tests/reexported-askama.rs
René Kijewski 6ce85f318c Make code generator re-usable for other projects
This PR
* removes the crate `askama_derive_standalone`,
* makes `askama_derive` a normal library, and
* adds the proc-macro crate `askama_macros`,

Before, it was not possible for another crate to re-export
`askama::Template` in a useful way, because the generated code assumes
that it has access to an `extern crate askama`.

`askama_derive` will export the function `derive_template()` like
`askama_derive_standalone` did, but it has an additional argument to
accept a `TokenStream` that should contain (an) statement(s) to define
the identifier `askama`, e.g. `quote! { extern crate askama; }`.

The new proc-macro crate `askama_macros` now defines the derive-macro
`Template` by calling `askama_derive::derive_template()`.

Prior art: [`encase`] → [`encase_derive`] → [`encase_derive_impl`];
[2298a3e].

[`encase`]: <https://crates.io/crates/encase/0.11.0>
[`encase_derive`]: <https://crates.io/crates/encase_derive/0.11.0>
[`encase_derive_impl`]: <https://crates.io/crates/encase_derive_impl/0.11.0>
[2298a3e]: <2298a3efd5>
2025-05-17 13:20:21 +02:00

27 lines
715 B
Rust

#[doc(hidden)]
pub use askama as __askama;
#[rustfmt::skip] // FIXME: upstream bug <https://github.com/rust-lang/rust/issues/141053>
#[macro_export]
macro_rules! new_greeter {
($name:ident) => {
#[derive(Debug, $crate::__askama::Template)]
#[template(
ext = "txt",
source = "Hello, world!",
askama = $crate::__askama
)]
struct $name;
};
}
#[test]
fn test_reexported_askama() {
// This test is the an example of `askama::Template`'s doc.
// It cannot be executed in there.
// We show how an re-exported `askama` can be used in a macro.
new_greeter!(HelloWorld);
assert_eq!(HelloWorld.to_string(), "Hello, world!");
}