Feat: custom format_args for inlining variables into html templates
This commit is contained in:
1 parent
a8b1225c48
commit
e4b1f6ea0d
13 files changed
+886
-333
No files matched your search
+118
-1
@@ -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);
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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! {
|
||||
|
||||
Reference in new issue
Block a user