From 1d82384e827aebf3ed6dd1619bc1efd347499ed8 Mon Sep 17 00:00:00 2001 From: Sebastien Rousseau Date: Mon, 25 Mar 2024 17:50:04 +0000 Subject: [PATCH] feat(libmake): :recycle: added new utility_macros and simplifying interface.rs --- src/interface.rs | 85 ++++++++++-------------------------- src/macros/mod.rs | 4 ++ src/macros/utility_macros.rs | 32 ++++++++++++++ 3 files changed, 60 insertions(+), 61 deletions(-) create mode 100644 src/macros/utility_macros.rs diff --git a/src/interface.rs b/src/interface.rs index 6bc43c9..fae9ae8 100644 --- a/src/interface.rs +++ b/src/interface.rs @@ -10,6 +10,7 @@ use std::{ }; use super::generator::FileGenerationParams; +use crate::macro_replace_placeholder; /// Replaces placeholders in a template file with values from the provided parameters /// and writes the result to an output file. @@ -37,70 +38,32 @@ pub fn replace_placeholders( let tpl_reader = BufReader::new(tpl); let mut output = File::create(output_file)?; let tpl_lines = tpl_reader.lines(); + for line in tpl_lines { let line = line?; - let replaced_line = line - .replace( - "{author}", - params.author.as_ref().unwrap_or(&String::new()), - ) - .replace( - "{build}", - params.build.as_ref().unwrap_or(&String::new()), - ) - .replace( - "{categories}", - params.categories.as_ref().unwrap_or(&String::new()), - ) - .replace( - "{description}", - params.description.as_ref().unwrap_or(&String::new()), - ) - .replace( - "{documentation}", - params.documentation.as_ref().unwrap_or(&String::new()), - ) - .replace( - "{edition}", - params.edition.as_ref().unwrap_or(&String::new()), - ) - .replace( - "{email}", - params.email.as_ref().unwrap_or(&String::new()), - ) - .replace( - "{homepage}", - params.homepage.as_ref().unwrap_or(&String::new()), - ) - .replace( - "{keywords}", - params.keywords.as_ref().unwrap_or(&String::new()), - ) - .replace( - "{license}", - params.license.as_ref().unwrap_or(&String::new()), - ) - .replace( - "{name}", - params.name.as_ref().unwrap_or(&String::new()), - ) - .replace( - "{repository}", - params.repository.as_ref().unwrap_or(&String::new()), - ) - .replace( - "{rustversion}", - params.rustversion.as_ref().unwrap_or(&String::new()), - ) - .replace( - "{version}", - params.version.as_ref().unwrap_or(&String::new()), - ) - .replace( - "{website}", - params.website.as_ref().unwrap_or(&String::new()), - ); + let replaced_line = macro_replace_placeholder!( + line, + params, + author, + build, + categories, + description, + documentation, + edition, + email, + homepage, + keywords, + license, + name, + output, + readme, + repository, + rustversion, + version, + website + ); writeln!(output, "{replaced_line}")?; } + Ok(()) } diff --git a/src/macros/mod.rs b/src/macros/mod.rs index 2334a82..ddd93b2 100644 --- a/src/macros/mod.rs +++ b/src/macros/mod.rs @@ -8,3 +8,7 @@ pub mod generator_macros; /// The `log_macros` module contains macros related to logging messages at various log levels and formats. pub mod log_macros; + +/// The `utility_macros` module contains utility macros for common tasks such as +/// replacing placeholders in a line with values from parameters. +pub mod utility_macros; diff --git a/src/macros/utility_macros.rs b/src/macros/utility_macros.rs new file mode 100644 index 0000000..fbffd67 --- /dev/null +++ b/src/macros/utility_macros.rs @@ -0,0 +1,32 @@ +// Copyright notice and licensing information. +// These lines indicate the copyright of the software and its licensing terms. +// SPDX-License-Identifier: Apache-2.0 OR MIT indicates dual licensing under Apache 2.0 or MIT licenses. +// Copyright © 2024 LibMake. All rights reserved. + +/// 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. +/// +#[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 + } + }; +}