Feat: custom format_args for inlining variables into html templates

This commit is contained in:
Jonathan Kelley committed 2021-02-26 20:42:55 -05:00
1 parent a8b1225c48
commit e4b1f6ea0d
13 files changed
+886 -333

No files matched your search

+1
View File
@@ -34,5 +34,6 @@ longest-increasing-subsequence = "0.1.0"
serde = { version = "1.0.123", features = ["derive"] }
log = "0.4.14"
pretty_env_logger = "0.4.0"
fstrings = "0.2.3"
# ouroboros = "0.8.0"
# hashbrown = { version = "0.9.1", features = ["bumpalo"] }
+67
View File
@@ -0,0 +1,67 @@
// #[macro_use]
extern crate fstrings;
use bumpalo::collections::String;
// use dioxus_core::ifmt;
// use fstrings::format_args_f;
use bumpalo::core_alloc::fmt::Write;
fn main() {
let bump = bumpalo::Bump::new();
let b = ≎
let world = "123";
// dioxus_core::ifmt!(in b; "Hello {world}";);
}
// let mut s = bumpalo::collections::String::new_in(b);
// fstrings::write_f!(s, "Hello {world}");
// dbg!(s);
// let p = {
// println!("hello, {}", &world);
// ()
// };
// let g = format_args!("hello {world}", world = world);
// let g = dioxus_core::ifmt!(in b, "Hello {world}");
// let g = ifmt!(in b, "hhello {world}");
// let g = ::core::fmt::Arguments::new_v1(
// &["hello "],
// &match (&world,) {
// (arg0,) => [::core::fmt::ArgumentV1::new(
// arg0,
// ::core::fmt::Display::fmt,
// )],
// },
// );
// fn main() {
// let bump = bumpalo::Bump::new();
// let b = ≎
// let world = "123";
// let world = 123;
// let g = {
// use bumpalo::core_alloc::fmt::Write;
// use ::dioxus_core::prelude::bumpalo;
// use ::dioxus_core::prelude::format_args_f;
// let bump = b;
// let mut s = bumpalo::collections::String::new_in(bump);
// let _ = (&mut s).write_fmt(::core::fmt::Arguments::new_v1(
// &[""],
// &match (&::core::fmt::Arguments::new_v1(
// &["hhello "],
// &match (&world,) {
// (arg0,) => [::core::fmt::ArgumentV1::new(
// arg0,
// ::core::fmt::Display::fmt,
// )],
// },
// ),)
// {
// (arg0,) => [::core::fmt::ArgumentV1::new(
// arg0,
// ::core::fmt::Display::fmt,
// )],
// },
// ));
// s
// };
// }
+11 -12
View File
@@ -2,23 +2,22 @@
use dioxus_core::prelude::*;
fn main() {}
fn main() {
Some(10)
.map(|f| f * 5)
.map(|f| f / 3)
.map(|f| f * 5)
.map(|f| f / 3);
}
/*
Our flagship demo :)
*/
static Example: FC<()> = |ctx, props| {
let (val1, set_val1) = use_state(&ctx, || "b1");
let (name, set_name) = use_state(&ctx, || "...?");
ctx.view(html! {
<div>
<button onclick={move |_| set_val1("b1")}> "Set value to b1" </button>
<button onclick={move |_| set_val1("b2")}> "Set value to b2" </button>
<button onclick={move |_| set_val1("b3")}> "Set value to b3" </button>
<div>
<p> "Value is: {val1}" </p>
</div>
<h1> "Hello, {name}" </h1>
<button onclick={move |_| set_name("jack")}> "jack!" </button>
<button onclick={move |_| set_name("jill")}> "jill!" </button>
</div>
})
};
+118 -1
View File
@@ -142,7 +142,8 @@ pub mod prelude {
// Re-export the FC macro
pub use crate as dioxus;
pub use crate::nodebuilder as builder;
pub use dioxus_core_macro::fc;
// pub use dioxus_core_macro::fc;
pub use dioxus_core_macro::format_args_f;
pub use dioxus_html_2::html;
// pub use crate::diff::DiffMachine;
@@ -150,3 +151,119 @@ pub mod prelude {
pub use crate::hooks::*;
}
// #[macro_use]
// extern crate dioxus_core_macro;
// #[macro_use]
// extern crate fstrings;
// pub use dioxus_core_macro::format_args_f;
// macro_rules! mk_macros {( @with_dollar![$dol:tt]=>
// $(
// #[doc = $doc_string:literal]
// $printlnf:ident
// => $println:ident!($($stream:ident,)? ...)
// ,
// )*
// ) => (
// $(
// #[doc = $doc_string]
// #[macro_export]
// macro_rules! $printlnf {(
// $($dol $stream : expr,)? $dol($dol args:tt)*
// ) => (
// $println!($($dol $stream,)? "{}", format_args_f!($dol($dol args)*))
// )}
// )*
// )}
// mk_macros! { @with_dollar![$]=>
// #[doc = "Like [`print!`](https://doc.rust-lang.org/std/macro.print.html), but with basic f-string interpolation."]
// print_f
// => print!(...)
// ,
// #[doc = "Like [`println!`](https://doc.rust-lang.org/std/macro.println.html), but with basic f-string interpolation."]
// println_f
// => println!(...)
// ,
// #[doc = "Like [`eprint!`](https://doc.rust-lang.org/std/macro.eprint.html), but with basic f-string interpolation."]
// eprint_f
// => eprint!(...)
// ,
// #[doc = "Like [`eprintln!`](https://doc.rust-lang.org/std/macro.eprintln.html), but with basic f-string interpolation."]
// eprintln_f
// => eprintln!(...)
// ,
// #[doc = "Like [`format!`](https://doc.rust-lang.org/std/macro.format.html), but with basic f-string interpolation."]
// format_f
// => format!(...)
// ,
// #[doc = "Shorthand for [`format_f`]."]
// f
// => format!(...)
// ,
// #[doc = "Like [`panic!`](https://doc.rust-lang.org/std/macro.panic.html), but with basic f-string interpolation."]
// panic_f
// => panic!(...)
// ,
// #[doc = "Like [`unreachable!`](https://doc.rust-lang.org/std/macro.unreachable.html), but with basic f-string interpolation."]
// unreachable_f
// => unreachable!(...)
// ,
// #[doc = "Like [`unimplemented!`](https://doc.rust-lang.org/std/macro.unimplemented.html), but with basic f-string interpolation."]
// unimplemented_f
// => unimplemented!(...)
// ,
// #[doc = "Like [`write!`](https://doc.rust-lang.org/std/macro.write.html), but with basic f-string interpolation."]
// write_f
// => write!(stream, ...)
// ,
// #[doc = "Like [`writeln!`](https://doc.rust-lang.org/std/macro.writeln.html), but with basic f-string interpolation."]
// writeln_f
// => writeln!(stream, ...)
// ,
// }
/// Like the `format!` macro for creating `std::string::String`s but for
/// `bumpalo::collections::String`.
///
/// # Examples
///
/// ```
/// use bumpalo::Bump;
///
/// let b = Bump::new();
///
/// let who = "World";
/// let s = bumpalo::format!(in &b, "Hello, {}!", who);
/// assert_eq!(s, "Hello, World!")
/// ```
#[macro_export]
macro_rules! ifmt {
( in $bump:expr; $fmt:literal;) => {{
use bumpalo::core_alloc::fmt::Write;
use $crate::prelude::bumpalo;
let bump = $bump;
let mut s = bumpalo::collections::String::new_in(bump);
let args = $crate::prelude::format_args_f!($fmt);
s.write_fmt(args);
s
}};
}
// ( in $bump:expr; $fmt:expr; ) => {
// $println!("{}", format_args_f!($dol($dol args)*))
// write!(&mut s, println!("{}", args));
// let _ = $crate::write_f!(&mut s, $fmt);
// s
// use fstrings::*;
// $crate::ifmt!(in $bump, $fmt)
// };
#[test]
fn macro_test() {
let w = 123;
let world = &w;
// let g = format_args_f!("Hello {world}");
// dbg!(g);
}
+16 -9
View File
@@ -1,11 +1,14 @@
//! Helpers for building virtual DOM VNodes.
use std::ops::Deref;
use crate::{
innerlude::VComponent,
nodes::{Attribute, Listener, NodeKey, VNode},
prelude::VElement,
};
use bumpalo::format;
use bumpalo::Bump;
/// A virtual DOM element builder.
@@ -335,15 +338,11 @@ where
/// .finish();
/// ```
#[inline]
pub fn on(self, _event: &'a str, _callback: impl Fn(()) + 'a) -> Self
// pub fn on<F>(mut self, event: &'a str, callback: impl Fn(()) -> () + 'static) -> Self
// F: Fn(()) + 'static,
// F: Fn(()) + 'a,
{
// self.listeners.push(Listener {
// event,
// callback: self.bump.alloc(callback),
// });
pub fn on(mut self, event: &'static str, callback: impl Fn(()) + 'a) -> Self {
self.listeners.push(Listener {
event,
callback: self.bump.alloc(callback),
});
self
}
}
@@ -1038,6 +1037,14 @@ pub fn text<'a>(contents: &'a str) -> VNode<'a> {
VNode::text(contents)
}
pub fn text2<'a>(contents: bumpalo::collections::String<'a>) -> VNode<'a> {
let f: &'a str = contents.into_bump_str();
VNode::text(f)
}
// pub fn text<'a>(contents: &'a str) -> VNode<'a> {
// VNode::text(contents)
// }
/// Construct an attribute for an element.
///
/// # Example
+3
View File
@@ -222,6 +222,9 @@ impl ActiveFrame {
// #[cfg(test)]
mod tests {
use super::*;
use crate::prelude::bumpalo;
// use crate::prelude::bumpalo::collections::string::String;
use crate::prelude::format_args_f;
static ListenerTest: FC<()> = |ctx, props| {
ctx.view(html! {