From ca7527681b609fedc368ea973022b004469035e6 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Mon, 29 Jun 2026 03:54:54 +0800 Subject: feat(just_template): add proc-macro `tmpl!` and restructure crate Move the old `tmpl!` and `tmpl_param!` macros into a dedicated `just_template_macros` proc-macro crate. The new `tmpl!` macro supports both simple parameter assignment and multi-arm implementation blocks with per-arm overrides. Remove the deprecated `deprecated` module and update tests accordingly. --- just_template/src/test_macros.rs | 186 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 186 insertions(+) create mode 100644 just_template/src/test_macros.rs (limited to 'just_template/src/test_macros.rs') diff --git a/just_template/src/test_macros.rs b/just_template/src/test_macros.rs new file mode 100644 index 0000000..5be58f5 --- /dev/null +++ b/just_template/src/test_macros.rs @@ -0,0 +1,186 @@ +use crate::Template; +use crate::tmpl; + +#[test] +fn basic_param() { + let mut tmpl = Template::from("Hello, <<>>!".to_string()); + + tmpl!(tmpl, name = "World"); + // tmpl.insert_param("name".to_string(), "World".to_string()); + + assert_eq!(tmpl.expand().unwrap(), "Hello, World!"); +} + +#[test] +fn multi_param() { + let mut tmpl = Template::from("<<>> + <<>> = <<>>".to_string()); + + tmpl! { + // tmpl, // Template named tmpl can be omitted + a = "1", + b = "2", + c = "3", + }; + + assert_eq!(tmpl.expand().unwrap(), "1 + 2 = 3"); +} + +#[test] +fn impl_blocks() { + let mut tmpl = Template::from( + r#" +>>>>>>>>>> arms +@@@ >>> arms + "<<>>" => Some(<<>>::exec(data, params).await), +@@@ <<< +"# + .trim() + .to_string(), + ); + + tmpl! { + arms { + crate_name = "my", + crate_name = "you", + } + } + + let expanded = tmpl.expand().unwrap(); + assert!(expanded.contains(r#""my" => Some(my::exec(data, params).await)"#)); + assert!(expanded.contains(r#""you" => Some(you::exec(data, params).await)"#)); +} + +#[test] +fn display_block_global_hidden_by_default() { + let tmpl = Template::from( + r#" +visible line +??? >>> debug + hidden line +??? <<< +visible end +"# + .trim() + .to_string(), + ); + + let expanded = tmpl.expand().unwrap(); + assert!(expanded.contains("visible line")); + assert!(expanded.contains("visible end")); + assert!(!expanded.contains("hidden line")); +} + +#[test] +fn display_block_global_shown_via_param() { + let mut tmpl = Template::from( + r#" +visible line +??? >>> debug + shown line +??? <<< +visible end +"# + .trim() + .to_string(), + ); + + tmpl! { + debug = true, + } + + let expanded = tmpl.expand().unwrap(); + assert!(expanded.contains("visible line")); + assert!(expanded.contains("visible end")); + assert!(expanded.contains("shown line")); +} + +#[test] +fn display_block_inside_impl_area_hidden_by_default() { + let mut tmpl = Template::from( + r#" +>>>>>>>>>> arms +@@@ >>> arms + <<>> => exec, +??? >>> extra + <<>> => metrics, +??? <<< +@@@ <<< +"# + .trim() + .to_string(), + ); + + tmpl! { + arms { + crate_name = "my" + } + } + + let expanded = tmpl.expand().unwrap(); + assert!(expanded.contains(r#"my => exec"#)); + assert!(!expanded.contains(r#"my => metrics"#)); +} + +#[test] +fn display_block_inside_impl_area_shown_by_global_param() { + let mut tmpl = Template::from( + r#" +>>>>>>>>>> arms +@@@ >>> arms + <<>> => exec, +??? >>> extra + <<>> => metrics, +??? <<< +@@@ <<< +"# + .trim() + .to_string(), + ); + + tmpl! { + extra = true, + arms { + crate_name = "my", + crate_name = "you" + } + } + + let expanded = tmpl.expand().unwrap(); + assert!(expanded.contains(r#"my => exec"#)); + assert!(expanded.contains(r#"my => metrics"#)); + assert!(expanded.contains(r#"you => exec"#)); + assert!(expanded.contains(r#"you => metrics"#)); +} + +#[test] +fn display_block_inside_impl_area_shown_by_arm_param() { + let mut tmpl = Template::from( + r#" +>>>>>>>>>> arms +@@@ >>> arms + <<>> => exec, +??? >>> extra + <<>> => metrics, +??? <<< +@@@ <<< +"# + .trim() + .to_string(), + ); + + tmpl! { + arms { + crate_name = "my", + { + crate_name = "you", + extra = true + } + } + } + + let expanded = tmpl.expand().unwrap(); + assert!(expanded.contains(r#"my => exec"#)); + assert!(!expanded.contains(r#"my => metrics"#)); + assert!(expanded.contains(r#"you => exec"#)); + assert!(expanded.contains(r#"you => metrics"#)); +} -- cgit