stable for leptos_macro

This commit is contained in:
Greg Johnston 2023-01-02 13:04:56 -05:00
parent e2517c99b8
commit edbd3612b3
2 changed files with 31 additions and 16 deletions

View file

@ -1,3 +1,4 @@
use itertools::Itertools;
use proc_macro2::{Ident, TokenStream};
use quote::{format_ident, ToTokens, TokenStreamExt};
use std::collections::HashSet;
@ -50,11 +51,12 @@ impl Parse for Model {
// We need to remove the `#[doc = ""]` and `#[builder(_)]`
// attrs from the function signature
item.attrs
.drain_filter(|attr| attr.path == parse_quote!(doc) || attr.path == parse_quote!(prop));
drain_filter(&mut item.attrs, |attr| {
attr.path == parse_quote!(doc) || attr.path == parse_quote!(prop)
});
item.sig.inputs.iter_mut().for_each(|arg| {
if let FnArg::Typed(ty) = arg {
ty.attrs.drain_filter(|attr| {
drain_filter(&mut ty.attrs, |attr| {
attr.path == parse_quote!(doc) || attr.path == parse_quote!(prop)
});
}
@ -82,6 +84,19 @@ impl Parse for Model {
}
}
// implemented manually because Vec::drain_filter is nightly only
// follows std recommended parallel
fn drain_filter<T>(vec: &mut Vec<T>, mut some_predicate: impl FnMut(&mut T) -> bool) {
let mut i = 0;
while i < vec.len() {
if some_predicate(&mut vec[i]) {
_ = vec.remove(i);
} else {
i += 1;
}
}
}
impl ToTokens for Model {
fn to_tokens(&self, tokens: &mut TokenStream) {
let Self {
@ -333,6 +348,7 @@ impl Docs {
}
fn typed_builder(&self) -> String {
#[allow(unstable_name_collisions)]
let doc_str = self
.0
.iter()

View file

@ -1,4 +1,3 @@
#![feature(drain_filter, iter_intersperse)]
#![cfg_attr(not(feature = "stable"), feature(proc_macro_span))]
#[macro_use]
@ -35,7 +34,7 @@ mod server;
/// The `view` macro uses RSX (like JSX, but Rust!) It follows most of the
/// same rules as HTML, with the following differences:
///
///
/// 1. Text content should be provided as a Rust string, i.e., double-quoted:
/// ```rust
/// # use leptos::*;
@ -177,7 +176,7 @@ mod server;
/// # });
/// ```
///
/// 8. You can use the `_ref` attribute to store a reference to its DOM element in a
/// 8. You can use the `_ref` attribute to store a reference to its DOM element in a
/// [NodeRef](leptos_reactive::NodeRef) to use later.
/// ```rust
/// # use leptos::*;
@ -246,23 +245,23 @@ pub fn view(tokens: TokenStream) -> TokenStream {
}
/// Annotates a function so that it can be used with your template as a Leptos `<Component/>`.
///
///
/// The `#[component]` macro allows you to annotate plain Rust functions as components
/// and use them within your Leptos [view](mod@view) as if they were custom HTML elements. The
/// and use them within your Leptos [view](mod@view) as if they were custom HTML elements. The
/// component function takes a [Scope](leptos_reactive::Scope) and any number of other arguments.
/// When you use the component somewhere else, the names of its arguments are the names
/// of the properties you use in the [view](mod@view) macro.
///
///
/// Every component function should have the return type `-> impl [IntoView](leptos_dom::IntoView)`.
///
/// You can add Rust doc comments to component function arguments and the macro will use them to
///
/// You can add Rust doc comments to component function arguments and the macro will use them to
/// generate documentation for the component.
///
///
/// Heres how you would define and use a simple Leptos component which can accept custom properties for a name and age:
/// ```rust
/// # use leptos::*;
/// use std::time::Duration;
///
///
/// #[component]
/// fn HelloComponent(
/// cx: Scope,
@ -284,7 +283,7 @@ pub fn view(tokens: TokenStream) -> TokenStream {
/// <p>"Your name is " {name} " and you are " {age} " years old."</p>
/// }
/// }
///
///
/// #[component]
/// fn App(cx: Scope) -> impl IntoView {
/// view! { cx,
@ -294,7 +293,7 @@ pub fn view(tokens: TokenStream) -> TokenStream {
/// }
/// }
/// ```
///
///
/// The `#[component]` macro creates a struct with a name like `HelloComponentProps`. If you define
/// your component in one module and import it into another, make sure you import this `___Props`
/// struct as well.
@ -352,7 +351,7 @@ pub fn view(tokens: TokenStream) -> TokenStream {
/// // ✅ Do this instead
/// # use leptos::*;
/// #[component]
/// fn MyComponent<T>(cx: Scope, render_prop: T) -> impl IntoView
/// fn MyComponent<T>(cx: Scope, render_prop: T) -> impl IntoView
/// where T: Fn() -> HtmlElement<Div> {
/// todo!()
/// }