Conversation
|
Nice, this works use bon::bon;
use hypertext::prelude::*;
struct Component;
#[bon]
impl Component {
#[builder]
fn new(id: u64, optional: Option<String>, children: impl Renderable) -> impl Renderable {
maud! { div #(id) { (optional) (children) } }
}
}
fn main() {
let res = maud! {
Component id=1 { "hello" }
}
.render();
println!("{res:?}"); // Rendered("<div id=\"1\">hello</div>")
}I only wish there was a way to do it with bare functions, but unfortunately the generated API is I hope this gets merged |
|
Fantastic work! I suspect this would potentially solve #123, which is currently a blocker for using hypertext in our codebase. Will check and report back.
Update 2: this branch actually works fine with hotpatching! The issue is that |
|
Hi there! Thank you so much for this work, it looks great! I would prefer that we use bon by default instead, would that be fine? |
|
@vidhanio Yes, I can rewrite this to use |
|
@XX, yep! also, does this PR supersede your other one? |
|
also, please wait a sec as i will be adding in some changes to the rendering/parsing code based on #153. |
|
@vidhanio Yes, this PR supersede the previous one. |
This pull request changes the way user components are instantiated during the expansion of the
rsx!andmaud!macros. Previously, a struct-literal approach was used:Now a builder-based approach is used:
The builder methods can be:
#[derive(TypedBuilder)]from thetyped-buildercrate);#[component]macro arguments (for example,#[component(builder = hypertext::DefaultBuilder)]or#[component(builder = bon::Builder)]);It is also now possible to propagate attributes defined on the component function parameters to the fields of the generated struct. This allows specifying builder-specific field attributes, including default argument values.
Some usage examples are provided in two new tests: https://github.com/vidhanio/hypertext/pull/183/changes#diff-8c79c3d623f866c80fb5e4093f5e2b774c3d590025116a0352d4a240e1ed6817
Backward Compatibility
The changes preserve API backward compatibility as much as possible. However, in some aspects the new behavior is incompatible with the previous one:
..at the end if the component implementsDefault. This syntax has been removed from the component parser.Default, it is now necessary to explicitly specify the use ofDefaultBuilderinstead of the defaultTypedBuilder(i.e.,#[component(builder = hypertext::DefaultBuilder)]) if omitting some component properties at the call site should be allowed.builder,build, and field setters, which may cause conflicts with similarly named methods already present in older components.#[component]macro. By default, this list includes thebuilderattribute to allow passing configuration options toTypedBuilder.Closes issue #180
Closes issue #128