Files
dioxus/packages/rsx/src/forloop.rs
T
88af3e7eff Hotreloading of for/if/body, formatted strings, literals, component props, nested rsx, light CLI rewrite, cli TUI (#2258)
Hotreload the contents of for loops, if chains, component bodies, props, attributes, and any literals discovered in rsx!
Add a TUI renderer to the CLI.
Improve the CLI build system to be async and parallel.
Refactor RSX to allow partial expansion of expressions.
Merge autofmt implementations for consistency.
Merge the representation of elements and components under the hood.
Add a diagnostics system for rsx for improved error messages.
Drop interprocess and move to websockets for communication between the CLI and the server.
Assign IDs to nodes and attributes in a stable way to be used in non compiler contexts.
Add hotreloading to any body of component/for loop/if chain/etc.

---------

Co-authored-by: Evan Almloff <evanalmloff@gmail.com>
Co-authored-by: Liam Mitchell <liamkarlmitchell@gmail.com>
2024-07-17 19:11:18 -07:00

70 lines
1.7 KiB
Rust

use super::*;
use location::DynIdx;
use proc_macro2::TokenStream as TokenStream2;
use syn::{braced, Expr, Pat};
#[non_exhaustive]
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct ForLoop {
pub for_token: Token![for],
pub pat: Pat,
pub in_token: Token![in],
pub expr: Box<Expr>,
pub body: TemplateBody,
pub dyn_idx: DynIdx,
}
impl Parse for ForLoop {
fn parse(input: ParseStream) -> Result<Self> {
// todo: better partial parsing
// A bit stolen from `ExprForLoop` in the `syn` crate
let for_token = input.parse()?;
let pat = input.call(Pat::parse_single)?;
let in_token = input.parse()?;
let expr = input.call(Expr::parse_without_eager_brace)?;
let content;
let _brace = braced!(content in input);
let body = content.parse()?;
Ok(Self {
for_token,
pat,
in_token,
expr: Box::new(expr),
body,
dyn_idx: DynIdx::default(),
})
}
}
impl ToTokens for ForLoop {
fn to_tokens(&self, tokens: &mut TokenStream2) {
let ForLoop {
pat, expr, body, ..
} = self;
// the temporary is important so we create a lifetime binding
tokens.append_all(quote! {
{
let ___nodes = (#expr).into_iter().map(|#pat| { #body }).into_dyn_node();
___nodes
}
});
}
}
#[test]
fn parses_for_loop() {
let toks = quote! {
for item in 0..10 {
div { "cool-{item}" }
div { "cool-{item}" }
div { "cool-{item}" }
}
};
let for_loop: ForLoop = syn::parse2(toks).unwrap();
assert!(for_loop.body.roots.len() == 3);
}