#[macro_export]
macro_rules! macro_replace_placeholder {
($line:expr, $params:expr, $($field:ident),+) => {
{
let mut line = $line;
$(
line = line.replace(
concat!("{", stringify!($field), "}"),
&$params.$field.as_deref().unwrap_or(""),
);
)+
line
}
};
}
#[macro_export]
macro_rules! macro_get_field {
($func_name:ident, $deserializer:expr) => {
pub fn $func_name(
file_path: Option<&str>,
field_name: &str,
) -> Result<String, Box<dyn std::error::Error>> {
file_path.map_or_else(
|| Ok(String::new()),
|file_path| {
let current_dir = env::current_dir()?;
let file_path =
Path::new(¤t_dir).join(file_path);
read_file(&file_path, |file| {
let value: serde_json::Value =
$deserializer(file)?;
let field_value = value
.get(field_name)
.ok_or_else(|| {
format!(
"Field '{}' not found",
field_name
)
})?
.as_str()
.map(|s| s.to_string())
.unwrap_or_else(|| {
value[field_name].to_string()
});
Ok(field_value)
})
},
)
}
};
}