🚧 Working on 0.1.0 version

This commit is contained in:
Manuel Cillero 2024-11-30 11:35:48 +01:00
parent 9f62955acb
commit 41cedf2541
28 changed files with 3473 additions and 535 deletions

View file

@ -3,28 +3,23 @@ name = "pagetop-macros"
version = "0.0.13"
edition = "2021"
description = "A collection of procedural macros that boost PageTop development."
homepage = "https://pagetop.cillero.es"
repository = "https://github.com/manuelcillero/pagetop"
license = "MIT OR Apache-2.0"
description = """\
A collection of macros that boost PageTop development.\
"""
categories = ["development-tools::procedural-macro-helpers", "web-programming"]
keywords = ["pagetop", "macros", "proc-macros", "codegen"]
authors = [
"Manuel Cillero <manuel@cillero.es>"
]
categories = [
"development-tools::procedural-macro-helpers", "web-programming"
]
keywords = [
"pagetop", "macros", "proc-macros", "codegen"
]
repository = "https://github.com/manuelcillero/pagetop"
homepage = "https://pagetop.cillero.es"
license = "MIT OR Apache-2.0"
authors = ["Manuel Cillero <manuel@cillero.es>"]
[lib]
proc-macro = true
[dependencies]
concat-string = "1.0.1"
proc-macro2 = "1.0"
proc-macro-crate = "3.1.0"
proc-macro2 = "1.0.92"
proc-macro-crate = "3.2.0"
proc-macro-error = "1.0.4"
quote = "1.0"
syn = { version = "2.0", features = ["full"] }
quote = "1.0.37"
syn = { version = "2.0.90", features = ["full"] }

View file

@ -2,7 +2,7 @@
<h1>PageTop Macros</h1>
<p>A collection of procedural macros that boost PageTop development.</p>
<p>A collection of macros that boost PageTop development.</p>
[![License](https://img.shields.io/badge/license-MIT%2FApache-blue.svg?style=for-the-badge)](#-license)
[![API Docs](https://img.shields.io/docsrs/pagetop-macros?label=API%20Docs&style=for-the-badge&logo=Docs.rs)](https://docs.rs/pagetop-macros)
@ -25,11 +25,16 @@ frequent changes. Production use is not recommended until version **0.1.0**.
# 🔖 Credits
This crate includes an adapted version of [maud-macros](https://crates.io/crates/maud_macros),
version [0.25.0](https://github.com/lambda-fairy/maud/tree/v0.25.0/maud_macros), by
[Chris Wong](https://crates.io/users/lambda-fairy). This adaptation integrates its functionalities
into **PageTop**, eliminating the need for direct references to `maud` in the `Cargo.toml` file of
each project.
This crate includes an adapted version of [maud-macros](https://crates.io/crates/maud_macros)
(version [0.25.0](https://github.com/lambda-fairy/maud/tree/v0.25.0/maud_macros)) by
[Chris Wong](https://crates.io/users/lambda-fairy).
Additionally, the [SmartDefault](https://crates.io/crates/smart_default) crate (version 0.7.1) by
[Jane Doe](https://crates.io/users/jane-doe) has been embedded as `AutoDefault`, simplifying
`Default` implementations.
Both eliminate the need to explicitly reference `maud` or `smart_default` in the `Cargo.toml` file
of each project.
# 📜 License

View file

@ -1,18 +1,11 @@
mod maud;
mod smart_default;
use concat_string::concat_string;
use proc_macro::TokenStream;
use proc_macro_error::proc_macro_error;
use quote::{quote, quote_spanned, ToTokens};
use syn::{parse_macro_input, parse_str, DeriveInput, ItemFn};
#[proc_macro]
#[proc_macro_error]
pub fn html(input: TokenStream) -> TokenStream {
maud::expand(input.into()).into()
}
/// Macro attribute to generate builder methods from `set_` methods.
///
/// This macro takes a method with the `set_` prefix and generates a corresponding method with the
@ -57,7 +50,7 @@ pub fn fn_builder(_: TokenStream, item: TokenStream) -> TokenStream {
fn_with_name.clone()
} else {
let g = &fn_set.sig.generics;
concat_string!(fn_with_name, quote! { #g }.to_string())
format!("{fn_with_name}{}", quote! { #g }.to_string())
};
let where_clause = fn_set
@ -66,7 +59,7 @@ pub fn fn_builder(_: TokenStream, item: TokenStream) -> TokenStream {
.where_clause
.as_ref()
.map_or(String::new(), |where_clause| {
concat_string!(quote! { #where_clause }.to_string(), " ")
format!("{} ", quote! { #where_clause }.to_string())
});
let args: Vec<String> = fn_set
@ -89,21 +82,21 @@ pub fn fn_builder(_: TokenStream, item: TokenStream) -> TokenStream {
.collect();
#[rustfmt::skip]
let fn_with = parse_str::<ItemFn>(concat_string!("
pub fn ", fn_with_generics, "(mut self, ", args.join(", "), ") -> Self ", where_clause, "{
self.", fn_set_name, "(", params.join(", "), ");
let fn_with = parse_str::<ItemFn>(format!(r##"
pub fn {fn_with_generics}(mut self, {}) -> Self {where_clause} {{
self.{fn_set_name}({});
self
}
").as_str()).unwrap();
}}
"##, args.join(", "), params.join(", ")
).as_str()).unwrap();
#[rustfmt::skip]
let fn_set_doc = concat_string!(
"<p id=\"method.", fn_with_name, "\" style=\"margin-bottom: 12px;\">Use ",
"<code class=\"code-header\">pub fn <span class=\"fn\" href=\"#method.", fn_with_name, "\">",
fn_with_name,
"</span>(self, …) -> Self</code> for the <a href=\"#method.new\">builder pattern</a>.",
"</p>"
);
let fn_set_doc = format!(r##"
<p id="method.{fn_with_name}" style="margin-bottom: 12px;">Use
<code class="code-header">pub fn <span class="fn" href="#method.{fn_with_name}">{fn_with_name}</span>(self, ) -> Self</code>
for the <a href="#method.new">builder pattern</a>.
</p>
"##);
let expanded = quote! {
#[doc(hidden)]
@ -115,6 +108,52 @@ pub fn fn_builder(_: TokenStream, item: TokenStream) -> TokenStream {
expanded.into()
}
#[proc_macro]
#[proc_macro_error]
pub fn html(input: TokenStream) -> TokenStream {
maud::expand(input.into()).into()
}
#[proc_macro_derive(AutoDefault, attributes(default))]
pub fn derive_auto_default(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
match smart_default::body_impl::impl_my_derive(&input) {
Ok(output) => output.into(),
Err(error) => error.to_compile_error().into(),
}
}
#[proc_macro_derive(ComponentClasses)]
pub fn derive_component_classes(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let name = &input.ident;
#[rustfmt::skip]
let fn_set_doc = format!(r##"
<p id="method.with_classes">Use
<code class="code-header"><span class="fn" href="#method.with_classes">with_classes</span>(self, ) -> Self</code>
to apply the <a href="#method.new">builder pattern</a>.
</p>
"##);
let expanded = quote! {
impl ComponentClasses for #name {
#[inline]
#[doc = #fn_set_doc]
fn set_classes(&mut self, op: ClassesOp, classes: impl Into<String>) -> &mut Self {
self.classes.set_value(op, classes);
self
}
fn classes(&self) -> &OptionClasses {
&self.classes
}
}
};
TokenStream::from(expanded)
}
/// Marks async main function as the `PageTop` entry-point.
///
/// # Examples
@ -154,43 +193,3 @@ pub fn test(_: TokenStream, item: TokenStream) -> TokenStream {
output.extend(item);
output
}
#[proc_macro_derive(AutoDefault, attributes(default))]
pub fn derive_auto_default(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
match smart_default::body_impl::impl_my_derive(&input) {
Ok(output) => output.into(),
Err(error) => error.to_compile_error().into(),
}
}
#[proc_macro_derive(ComponentClasses)]
pub fn derive_component_classes(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let name = &input.ident;
let fn_set_doc = concat_string!(
"<p id=\"method.with_classes\">",
"Use <code class=\"code-header\">",
" <span class=\"fn\" href=\"#method.with_classes\">with_classes</span>(self, …) -> Self ",
"</code> to apply the <a href=\"#method.new\">builder pattern</a>.",
"</p>"
);
let expanded = quote! {
impl ComponentClasses for #name {
#[inline]
#[doc = #fn_set_doc]
fn set_classes(&mut self, op: ClassesOp, classes: impl Into<String>) -> &mut Self {
self.classes.set_value(op, classes);
self
}
fn classes(&self) -> &OptionClasses {
&self.classes
}
}
};
TokenStream::from(expanded)
}