Skip to content

Commit

Permalink
feat(libmake): ♻️ added new utility_macros and simplifying interface.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienrousseau committed Mar 25, 2024
1 parent c894cbf commit 1d82384
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 61 deletions.
85 changes: 24 additions & 61 deletions src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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(())
}
4 changes: 4 additions & 0 deletions src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
32 changes: 32 additions & 0 deletions src/macros/utility_macros.rs
Original file line number Diff line number Diff line change
@@ -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
}
};
}

0 comments on commit 1d82384

Please sign in to comment.