aboutsummaryrefslogtreecommitdiff
path: root/mingling_macros/src/func
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-07-21 04:26:10 +0800
committer魏曹先生 <1992414357@qq.com>2026-07-21 04:26:10 +0800
commit2683d7c6c3c88355fd3188ac2b2c68338ae2d205 (patch)
treecc89cfe0d377e8550aa3577add86f4366b804c07 /mingling_macros/src/func
parenta3b75b7eeae5e7a0c53b6167bafb75a85fa6d9ce (diff)
feat(core, macros): add r_append! macro and RenderResult::append_other
Implement `RenderResult::append_other()` to merge buffered content and immediate output behavior from one result into another. Add the `r_append!` macro with both explicit buffer (`dst, src`) and implicit buffer (`src`) usage forms, re-exported from `mingling::prelude`.
Diffstat (limited to 'mingling_macros/src/func')
-rw-r--r--mingling_macros/src/func/r_print.rs57
1 files changed, 57 insertions, 0 deletions
diff --git a/mingling_macros/src/func/r_print.rs b/mingling_macros/src/func/r_print.rs
index 0f77b2b..86ea52f 100644
--- a/mingling_macros/src/func/r_print.rs
+++ b/mingling_macros/src/func/r_print.rs
@@ -68,3 +68,60 @@ pub(crate) fn r_eprintln(input: TokenStream) -> TokenStream {
pub(crate) fn r_eprint(input: TokenStream) -> TokenStream {
expand_print(input, "eprint")
}
+
+/// Parsed input for `r_append!`.
+///
+/// Two forms:
+/// - `(dst, src)` — explicit buffer and source
+/// - `(src)` — implicit `__render_result_buffer`
+struct AppendInput {
+ dst: Option<Ident>,
+ src: proc_macro2::TokenStream,
+}
+
+impl Parse for AppendInput {
+ fn parse(input: ParseStream) -> syn::Result<Self> {
+ if input.peek(Ident) && input.peek2(Token![,]) {
+ let dst: Ident = input.parse()?;
+ let _comma: Token![,] = input.parse()?;
+ let src: TokenStream2 = input.parse()?;
+ Ok(AppendInput {
+ dst: Some(dst),
+ src,
+ })
+ } else {
+ let src: TokenStream2 = input.parse()?;
+ Ok(AppendInput { dst: None, src })
+ }
+ }
+}
+
+/// `r_append!` macro: appends the contents of another `RenderResult` to this one.
+///
+/// Two forms:
+/// - `r_append!(dst, src)` — appends `src` into `dst`
+/// - `r_append!(src)` — appends `src` into `__render_result_buffer`
+pub(crate) fn r_append(input: TokenStream) -> TokenStream {
+ let parsed: AppendInput = match syn::parse(input) {
+ Ok(p) => p,
+ Err(e) => return e.to_compile_error().into(),
+ };
+
+ let dst_ident = parsed.dst.clone();
+ let src_tokens = parsed.src;
+
+ let expanded = match dst_ident {
+ Some(dst) => {
+ quote! {
+ #dst.append_other(#src_tokens);
+ }
+ }
+ None => {
+ quote! {
+ __render_result_buffer.append_other(#src_tokens);
+ }
+ }
+ };
+
+ expanded.into()
+}