fix: re-export slice! macro (#2008)

This commit is contained in:
blorbb 2023-11-11 22:47:15 +11:00 committed by GitHub
parent 61c7ff4256
commit 8573f22d96
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 30 deletions

View file

@ -180,7 +180,7 @@ pub mod error {
pub use leptos_macro::template;
#[cfg(not(all(target_arch = "wasm32", feature = "template_macro")))]
pub use leptos_macro::view as template;
pub use leptos_macro::{component, island, server, slot, view, Params};
pub use leptos_macro::{component, island, server, slice, slot, view, Params};
pub use leptos_reactive::*;
pub use leptos_server::{
self, create_action, create_multi_action, create_server_action,

View file

@ -971,9 +971,9 @@ pub(crate) fn attribute_value(attr: &KeyedAttribute) -> &syn::Expr {
}
}
/// Generates a `lens` into struct with a default getter and setter
/// Generates a `slice` into a struct with a default getter and setter.
///
/// Can be used to access deeply nested fields within a global state object
/// Can be used to access deeply nested fields within a global state object.
///
/// ```rust
/// # use leptos::{create_runtime, create_rw_signal};

View file

@ -1,59 +1,55 @@
extern crate proc_macro;
use proc_macro::TokenStream;
use proc_macro2::Ident;
use quote::quote;
use quote::{quote, ToTokens};
use syn::{
parse::{Parse, ParseStream},
parse_macro_input,
punctuated::Punctuated,
token::Dot,
Token, Type,
Token,
};
struct SliceMacroInput {
pub root: Ident,
pub path: Punctuated<Type, Dot>,
root: syn::Ident,
path: Punctuated<syn::Ident, Token![.]>,
}
impl Parse for SliceMacroInput {
fn parse(input: ParseStream) -> Result<Self, syn::Error> {
let root = syn::Ident::parse(input)?;
let _dot = <Token![.]>::parse(input)?;
let path = input.parse_terminated(Type::parse, Token![.])?;
fn parse(input: ParseStream) -> syn::Result<Self> {
let root: syn::Ident = input.parse()?;
input.parse::<Token![.]>()?;
// do not accept trailing punctuation
let path: Punctuated<syn::Ident, Token![.]> =
Punctuated::parse_separated_nonempty(input)?;
if path.is_empty() {
return Err(syn::Error::new(input.span(), "Expected identifier"));
return Err(input.error("expected identifier"));
}
if path.trailing_punct() {
return Err(syn::Error::new(
input.span(),
"Unexpected trailing `.`",
));
if !input.is_empty() {
return Err(input.error("unexpected token"));
}
Ok(SliceMacroInput { root, path })
Ok(Self { root, path })
}
}
impl From<SliceMacroInput> for TokenStream {
fn from(val: SliceMacroInput) -> Self {
let root = val.root;
let path = val.path;
impl ToTokens for SliceMacroInput {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
let root = &self.root;
let path = &self.path;
quote! {
tokens.extend(quote! {
::leptos::create_slice(
#root,
|st: &_| st.#path.clone(),
|st: &mut _, n| st.#path = n
)
}
.into()
})
}
}
pub fn slice_impl(tokens: TokenStream) -> TokenStream {
let input = parse_macro_input!(tokens as SliceMacroInput);
input.into()
input.into_token_stream().into()
}

View file

@ -14,7 +14,7 @@ error: expected `.`
|
= note: this error originates in the macro `slice` (in Nightly builds, run with -Z macro-backtrace for more info)
error: Expected identifier
error: unexpected end of input, expected identifier
--> tests/slice/red.rs:25:18
|
25 | let (_, _) = slice!(outer_signal.);
@ -22,7 +22,7 @@ error: Expected identifier
|
= note: this error originates in the macro `slice` (in Nightly builds, run with -Z macro-backtrace for more info)
error: Unexpected trailing `.`
error: unexpected end of input, expected identifier
--> tests/slice/red.rs:27:18
|
27 | let (_, _) = slice!(outer_signal.inner.);