summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-02-28 14:06:54 +0800
committer魏曹先生 <1992414357@qq.com>2026-02-28 14:06:54 +0800
commit936033f39e391b44562bb8702e9c30bcf6905e85 (patch)
tree97a9bcaea404372b1b309661115583e115a9c0aa
parent55b31ee792963b4c23eae95f2216891b00cd6a69 (diff)
Refactor crate structure and move macros to lib.rs
-rw-r--r--src/expand.rs2
-rw-r--r--src/lib.rs83
-rw-r--r--src/template.rs33
-rw-r--r--src/test.rs2
4 files changed, 85 insertions, 35 deletions
diff --git a/src/expand.rs b/src/expand.rs
index 2bcbd06..049b04d 100644
--- a/src/expand.rs
+++ b/src/expand.rs
@@ -2,7 +2,7 @@ use std::{collections::HashMap, mem::replace};
use just_fmt::snake_case;
-use crate::Template;
+use crate::template::Template;
const IMPL_AREA_BEGIN: &str = "@@@ >>> ";
const IMPL_AREA_END: &str = "@@@ <<<";
diff --git a/src/lib.rs b/src/lib.rs
new file mode 100644
index 0000000..704b310
--- /dev/null
+++ b/src/lib.rs
@@ -0,0 +1,83 @@
+//! Template struct for storing template strings and their parameters.
+//!
+//! The template supports two types of parameters:
+//! - Simple parameters: key-value pairs used to replace simple placeholders (`<<<key>>>` format) in the template.
+//! - Implementation parameters: for implementation blocks (`>>>>>>>>> block_name` and `@@@ >>> block_name` format),
+//! can contain multiple parameter sets, each corresponding to an implementation instance.
+//!
+//! # Examples
+//! ```
+//! use just_template::Template;
+//!
+//! let mut tmpl = Template::from("Hello, <<<name>>>!".to_string());
+//! tmpl.insert_param("name".to_string(), "World".to_string());
+//! assert_eq!(tmpl.to_string(), "Hello, World!");
+//! ```
+//!
+//! Using the `tmpl_param!` macro makes it easier to add simple parameters:
+//! ```
+//! use just_template::{Template, tmpl_param};
+//!
+//! let mut tmpl = Template::from("<<<a>>> + <<<b>>> = <<<c>>>".to_string());
+//! tmpl_param!(tmpl, a = 1, b = 2, c = 3);
+//! assert_eq!(tmpl.to_string(), "1 + 2 = 3");
+//! ```
+//!
+//! Using the `tmpl!` macro adds implementation block parameters:
+//! ```
+//! use just_template::{Template, tmpl};
+//!
+//! let mut tmpl = Template::from("
+//! >>>>>>>>>> arms
+//! @@@ >>> arms
+//! <<<crate_name>>> => Some(<<<crate_name>>>::exec(data, params).await),
+//! @@@ <<<
+//! ".trim().to_string());
+//! tmpl!(tmpl += {
+//! arms {
+//! (crate_name = "my"),
+//! (crate_name = "you")
+//! }
+//! });
+//! // Output the expanded template
+//! let expanded = tmpl.to_string();
+//! assert_eq!(expanded, "
+//! my => Some(my::exec(data, params).await),
+//! you => Some(you::exec(data, params).await),
+//! ".trim().to_string());
+//! ```
+mod template;
+pub use template::*; // Re-export template to just_template
+
+pub mod expand;
+pub mod test;
+
+#[macro_export]
+macro_rules! tmpl_param {
+ ($template:ident, $($key:ident = $value:expr),* $(,)?) => {{
+ $(
+ $template.insert_param(stringify!($key).to_string(), $value.to_string());
+ )*
+ }};
+}
+
+#[macro_export]
+macro_rules! tmpl {
+ ($template:ident += {
+ $($name:ident {
+ $(($($key:ident = $value:expr),* $(,)?)),*
+ $(,)?
+ }),*
+ }) => {{
+ $(
+ let $name = $template.add_impl(stringify!($name).to_string());
+ $(
+ $name.push({
+ let mut params = std::collections::HashMap::new();
+ $(params.insert(stringify!($key).to_string(), $value.to_string());)*
+ params
+ });
+ )*
+ )*
+ }};
+}
diff --git a/src/template.rs b/src/template.rs
index 2df8e46..622f191 100644
--- a/src/template.rs
+++ b/src/template.rs
@@ -1,8 +1,5 @@
use std::collections::HashMap;
-pub mod expand;
-pub mod test;
-
#[derive(Default, Clone)]
pub struct Template {
pub(crate) template_str: String,
@@ -39,33 +36,3 @@ impl std::fmt::Display for Template {
write!(f, "{}", cloned.expand().unwrap_or_default())
}
}
-
-#[macro_export]
-macro_rules! tmpl_param {
- ($template:ident, $($key:ident = $value:expr),* $(,)?) => {{
- $(
- $template.insert_param(stringify!($key).to_string(), $value.to_string());
- )*
- }};
-}
-
-#[macro_export]
-macro_rules! tmpl {
- ($template:ident += {
- $($name:ident {
- $(($($key:ident = $value:expr),* $(,)?)),*
- $(,)?
- }),*
- }) => {{
- $(
- let $name = $template.add_impl(stringify!($name).to_string());
- $(
- $name.push({
- let mut params = std::collections::HashMap::new();
- $(params.insert(stringify!($key).to_string(), $value.to_string());)*
- params
- });
- )*
- )*
- }};
-}
diff --git a/src/test.rs b/src/test.rs
index f824b1b..a5ef801 100644
--- a/src/test.rs
+++ b/src/test.rs
@@ -1,6 +1,6 @@
#[cfg(test)]
mod tests {
- use crate::{Template, tmpl, tmpl_param};
+ use crate::{template::Template, tmpl, tmpl_param};
#[test]
fn expand() {