1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
/// Replaces placeholders in a given line with corresponding values from the provided parameters.
///
/// # Arguments
///
/// * `line` - The line containing placeholders to be replaced.
/// * `params` - The parameters containing values to replace the placeholders.
/// * `$($field:ident),+` - Identifiers representing the fields in `params` to be replaced.
///
/// # Returns
///
/// The line with placeholders replaced by their corresponding values.
///
///
/// # Examples
///
/// ```
/// use serde_yml::macro_replace_placeholder;
//
/// #[derive(Default)]
/// struct Params {
/// field1: Option<&'static str>,
/// field2: Option<&'static str>,
/// }
//
/// let params = Params {
/// field1: Some("value1"),
/// field2: Some("value2"),
/// };
//
/// // Test replacing both fields
/// let line = macro_replace_placeholder!(
/// "Field 1: {field1}, Field 2: {field2}",
/// ¶ms,
/// field1,
/// field2
/// );
/// assert_eq!(line, "Field 1: value1, Field 2: value2");
/// ```
///
#[macro_export]
macro_rules! macro_replace_placeholder {
($line:expr, $params:expr, $($field:ident),+) => {
{
let mut line = $line.to_owned(); // Convert line to owned String
$(
line = line.replace(
concat!("{", stringify!($field), "}"),
&$params.$field.as_deref().unwrap_or(""),
);
)+
line
}
};
}