Implement IntoView on #[component] generated struct for better builder API ergonomics
Is your feature request related to a problem? Please describe.
Per the current builder API document, currently, components created via #[component] macro is supposed to be used like this with the builder API:
use leptos::html::p;
let (value, set_value) = signal(0);
Show(
ShowProps::builder()
.when(move || value.get() > 5)
.fallback(|| p().child("I will appear if `value` is 5 or lower"))
.children(ToChildren::to_children(|| {
p().child("I will appear if `value` is above 5")
}))
.build(),
)This does not adhere to the Rust style guideline which suggests a snake case naming convention for functions.
Describe the solution you'd like
if we look closer, Show is actually a function that returns impl IntoView. Moreover, the components and the props generated are related one-to-one. So why couldn't we add a, for instance, a method called view, on ShowProps that consumes the struct and calls Show to return an impl IntoView? This way, we can do something like
// Additional generated code for Show
// impl Trait in struct method return position has long been stabilized, so it should not affect MSRV
impl ShowProps {
pub fn view(self) -> impl IntoView {
Show(self)
}
}
// End Additional generated code for Show
use leptos::html::p;
let (value, set_value) = signal(0);
ShowProps::builder()
.when(move || value.get() > 5)
.fallback(|| p().child("I will appear if `value` is 5 or lower"))
.children(ToChildren::to_children(|| {
p().child("I will appear if `value` is above 5")
}))
.build()
.view()Or we can go further, combine the build & view calls by implementing view on the generated builder, and use Show instead of ShowProps for the name of the generated struct (one could actually have a struct & a function with the same name in a same namespace), and result in something like
use leptos::html::p;
let (value, set_value) = signal(0);
Show::builder()
.when(move || value.get() > 5)
.fallback(|| p().child("I will appear if `value` is 5 or lower"))
.children(ToChildren::to_children(|| {
p().child("I will appear if `value` is above 5")
}))
.view()The code now adheres better to the Rust style guideline.
For the compatibility with the previous releases, we can define an alias
pub type ShowProps = Show;This should make the builder API easier and nicer to use.
Describe alternatives you've considered
Additional context Rust Style Guide
Source: leptos-rs/leptos