forked from leptos-rs/leptos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a
slice!()
macro (leptos-rs#1867)
- Loading branch information
Showing
6 changed files
with
186 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
extern crate proc_macro; | ||
|
||
use proc_macro::TokenStream; | ||
use proc_macro2::Ident; | ||
use quote::quote; | ||
use syn::{ | ||
parse::{Parse, ParseStream}, | ||
parse_macro_input, | ||
punctuated::Punctuated, | ||
token::Dot, | ||
Token, Type, | ||
}; | ||
|
||
struct SliceMacroInput { | ||
pub root: Ident, | ||
pub path: Punctuated<Type, Dot>, | ||
} | ||
|
||
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![.])?; | ||
|
||
if path.is_empty() { | ||
return Err(syn::Error::new(input.span(), "Expected identifier")); | ||
} | ||
|
||
if path.trailing_punct() { | ||
return Err(syn::Error::new( | ||
input.span(), | ||
"Unexpected trailing `.`", | ||
)); | ||
} | ||
|
||
Ok(SliceMacroInput { root, path }) | ||
} | ||
} | ||
|
||
impl From<SliceMacroInput> for TokenStream { | ||
fn from(val: SliceMacroInput) -> Self { | ||
let root = val.root; | ||
let path = val.path; | ||
|
||
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use leptos::{create_runtime, create_rw_signal}; | ||
use leptos_macro::slice; | ||
|
||
#[derive(Default)] | ||
pub struct OuterState { | ||
count: i32, | ||
inner: InnerState, | ||
} | ||
|
||
#[derive(Clone, PartialEq, Default)] | ||
pub struct InnerState { | ||
inner_count: i32, | ||
inner_name: String, | ||
} | ||
|
||
#[test] | ||
fn green() { | ||
let _ = create_runtime(); | ||
|
||
let outer_signal = create_rw_signal(OuterState::default()); | ||
|
||
let (_, _) = slice!(outer_signal.count); | ||
|
||
let (_, _) = slice!(outer_signal.inner.inner_count); | ||
let (_, _) = slice!(outer_signal.inner.inner_name); | ||
} | ||
|
||
#[test] | ||
fn red() { | ||
let t = trybuild::TestCases::new(); | ||
t.compile_fail("tests/slice/red.rs") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use leptos::{create_runtime, create_rw_signal}; | ||
use leptos_macro::slice; | ||
|
||
#[derive(Default, PartialEq)] | ||
pub struct OuterState { | ||
count: i32, | ||
inner: InnerState, | ||
} | ||
|
||
#[derive(Clone, PartialEq, Default)] | ||
pub struct InnerState { | ||
inner_count: i32, | ||
inner_name: String, | ||
} | ||
|
||
fn main() { | ||
let _ = create_runtime(); | ||
|
||
let outer_signal = create_rw_signal(OuterState::default()); | ||
|
||
let (_, _) = slice!(); | ||
|
||
let (_, _) = slice!(outer_signal); | ||
|
||
let (_, _) = slice!(outer_signal.); | ||
|
||
let (_, _) = slice!(outer_signal.inner.); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
error: unexpected end of input, expected identifier | ||
--> tests/slice/red.rs:21:18 | ||
| | ||
21 | let (_, _) = slice!(); | ||
| ^^^^^^^^ | ||
| | ||
= note: this error originates in the macro `slice` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: expected `.` | ||
--> tests/slice/red.rs:23:18 | ||
| | ||
23 | let (_, _) = slice!(outer_signal); | ||
| ^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: this error originates in the macro `slice` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: Expected identifier | ||
--> tests/slice/red.rs:25:18 | ||
| | ||
25 | let (_, _) = slice!(outer_signal.); | ||
| ^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: this error originates in the macro `slice` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: Unexpected trailing `.` | ||
--> tests/slice/red.rs:27:18 | ||
| | ||
27 | let (_, _) = slice!(outer_signal.inner.); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: this error originates in the macro `slice` (in Nightly builds, run with -Z macro-backtrace for more info) |