diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-06-29 03:34:41 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-06-29 03:34:41 +0800 |
| commit | ba15b7c06468cb6c52c8d2a53419fd83f9ebcb8b (patch) | |
| tree | cdaa1c71585d10dd73945cefe95f2c25f27924ed /just_template/src/template.rs | |
| parent | ff70307869a547b13850d1eec3f72e8ca3bca265 (diff) | |
refactor: promote project to workspace with macros sub-crate
Diffstat (limited to 'just_template/src/template.rs')
| -rw-r--r-- | just_template/src/template.rs | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/just_template/src/template.rs b/just_template/src/template.rs new file mode 100644 index 0000000..e368917 --- /dev/null +++ b/just_template/src/template.rs @@ -0,0 +1,54 @@ +use std::collections::HashMap; + +#[derive(Default, Clone)] +pub struct Template { + pub(crate) template_str: String, + pub(crate) params: HashMap<String, String>, + pub(crate) impl_params: HashMap<String, Vec<HashMap<String, String>>>, +} + +impl Template { + /// Add a parameter + pub fn insert_param(&mut self, name: String, value: String) { + self.params.insert(name, value); + } + + /// Add an implementation block and return a HashMap to set its parameters + pub fn add_impl(&mut self, impl_name: String) -> &mut Vec<HashMap<String, String>> { + self.impl_params.entry(impl_name).or_default() + } +} + +impl From<String> for Template { + fn from(s: String) -> Self { + Template { + template_str: s, + ..Default::default() + } + } +} + +impl<'a> From<&'a str> for Template { + fn from(s: &'a str) -> Self { + Template { + template_str: s.to_string(), + ..Default::default() + } + } +} + +impl<'a> From<std::borrow::Cow<'a, str>> for Template { + fn from(s: std::borrow::Cow<'a, str>) -> Self { + Template { + template_str: s.into_owned(), + ..Default::default() + } + } +} + +impl std::fmt::Display for Template { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let cloned = self.clone(); + write!(f, "{}", cloned.expand().unwrap_or_default()) + } +} |
