Skip to content

Commit

Permalink
refactor(libmake): 🎨 updated macros and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienrousseau committed Nov 6, 2023
1 parent 5b69898 commit 52b89c6
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 45 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ path = "benches/criterion.rs"
debug = true

[dependencies]
anyhow = "1.0.75"
assert_cmd = "2.0.12"
clap = "4.4.7"
csv = "1.3.0"
Expand Down
138 changes: 96 additions & 42 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,71 +3,127 @@

//! Macros for the `libmake` crate.
//!
//! This module bundles all macros used across the `libmake` crate.
//! These include macros for asserting the creation of directories,
//! generating templates from JSON, YAML, and CSV files.
//!
//! This module provides macros for asserting the creation of directories,
//! generating templates from JSON, YAML, and CSV files, and custom logging functionality.

// Macro for creating a new directory at the specified path.
#[macro_export]
/// A macro for creating a new directory.
/// Asserts that a directory is created at the given path.
///
/// # Arguments
///
/// * `$path` - A string literal specifying the path where the directory will be created.
///
/// This will create a new directory at the given path. If the directory
/// already exists, it will return an error.
/// # Panics
///
/// Panics if the directory cannot be created.
macro_rules! assert_create_directory {
($path:expr) => {
assert!(create_directory(Path::new($path)).is_ok());
use std::path::Path;
use std::fs::{self, create_dir_all};
assert!(create_dir_all(Path::new($path)).is_ok(), "Failed to create directory at: {}", $path);
};
}

// Macro for generating file templates using a command-line interface.
#[macro_export]
/// A macro for generating a new set of file templates using the command
/// line interface.
/// Asserts that file templates are generated from the given parameters.
///
/// # Arguments
///
/// * `$params` - Parameters for file generation.
///
/// # Panics
///
/// Panics if the files cannot be generated.
macro_rules! assert_generate_files {
($params:expr) => {
assert!(generate_files($params).is_ok());
assert!(generate_files($params).is_ok(), "Failed to generate files with parameters: {:?}", $params);
};
}

// Macro for generating file templates from a CSV file.
#[macro_export]
/// A macro for generating a new set of file templates using a CSV file.
/// Asserts that file templates are generated from a CSV file.
///
/// # Arguments
///
/// * `$csv_path` - The path to the CSV file.
///
/// # Panics
///
/// Panics if the files cannot be generated from the CSV.
macro_rules! assert_generate_from_csv {
($csv_path:expr) => {
assert!(generate_from_csv($csv_path).is_ok());
assert!(generate_from_csv($csv_path).is_ok(), "Failed to generate files from CSV at: {}", $csv_path);
};
}

// Macro for generating file templates from a JSON file.
#[macro_export]
/// A macro for generating a new set of file templates using a JSON file.
/// Asserts that file templates are generated from a JSON file.
///
/// # Arguments
///
/// * `$json_path` - The path to the JSON file.
///
/// # Panics
///
/// Panics if the files cannot be generated from the JSON.
macro_rules! assert_generate_from_json {
($path:expr) => {
assert!(generate_from_json($path).is_ok());
($json_path:expr) => {
assert!(generate_from_json($json_path).is_ok(), "Failed to generate files from JSON at: {}", $json_path);
};
}

// Macro for generating file templates from a YAML file.
#[macro_export]
/// A macro for generating a new set of file templates using a YAML file.
/// Asserts that file templates are generated from a YAML file.
///
/// # Arguments
///
/// * `$yaml_path` - The path to the YAML file.
///
/// # Panics
///
/// Panics if the files cannot be generated from the YAML.
macro_rules! assert_generate_from_yaml {
($path:expr) => {
assert!(generate_from_yaml($path).is_ok());
($yaml_path:expr) => {
assert!(generate_from_yaml($yaml_path).is_ok(), "Failed to generate files from YAML at: {}", $yaml_path);
};
}

// Macro for generating file templates from a configuration file.
#[macro_export]
/// A macro for generating a new set of file templates using a
/// configuration file. This will require a path to the configuration
/// file and the file type. The file type can be either `json`, `yaml`,
/// `yml` or `csv`.
/// Asserts that file templates are generated from a configuration file.
///
/// # Arguments
///
/// * `$path` - The path to the configuration file.
/// * `$file_type` - The type of the configuration file: `json`, `yaml`, `yml`, or `csv`.
///
/// # Panics
///
/// Panics if the files cannot be generated from the configuration file.
macro_rules! assert_generate_from_config {
($path:expr, $file_type:expr) => {
assert!(generate_from_config($path, $file_type).is_ok());
assert!(generate_from_config($path, $file_type).is_ok(), "Failed to generate files from {} configuration at: {}", $file_type, $path);
};
}
/// Custom logging macro for various log levels and formats.
// Macro for logging information with various log levels and formats.
#[macro_export]
/// Logs information with the specified level, component, and format.
///
/// # Parameters
///
/// * `$level`: The log level of the message.
/// * `$component`: The component where the log is coming from.
/// * `$description`: A description of the log message.
/// * `$format`: The format of the log message.
/// * `$level` - The log level for the message.
/// * `$component` - The component where the log message originates.
/// * `$description` - A description for the log message.
/// * `$format` - The format for the log message.
///
#[macro_export]
/// # Returns
///
/// This macro returns the created `Log` instance.
macro_rules! macro_log_info {
($level:expr, $component:expr, $description:expr, $format:expr) => {{
use dtt::DateTime;
Expand All @@ -94,24 +150,22 @@ macro_rules! macro_log_info {
log // Return the Log instance
}};
}
/// Macros related to executing shell commands.
///
/// Executes a shell command, logs the start and completion of the operation, and handles any errors that occur.
// Macro for executing a shell command and logging the operation.
#[macro_export]
/// Executes a shell command and logs the start, completion, and any errors.
///
/// # Parameters
///
/// * `$command`: The shell command to execute.
/// * `$package`: The name of the package the command is being run on.
/// * `$operation`: A description of the operation being performed.
/// * `$start_message`: The log message to be displayed at the start of the operation.
/// * `$complete_message`: The log message to be displayed upon successful completion of the operation.
/// * `$error_message`: The log message to be displayed in case of an error.
/// * `$command` - The shell command to execute.
/// * `$package` - The name of the package being operated on.
/// * `$operation` - A description of the operation.
/// * `$start_message` - The message to log at the start of the operation.
/// * `$complete_message` - The message to log upon successful completion.
/// * `$error_message` - The message to log in case of an error.
///
/// # Returns
///
/// Returns a `Result<(), anyhow::Error>` indicating the success or failure of the operation.
///
#[macro_export]
/// Returns a `Result<(), anyhow::Error>` to indicate the success or failure of the command execution.
macro_rules! macro_execute_and_log {
($command:expr, $package:expr, $operation:expr, $start_message:expr, $complete_message:expr, $error_message:expr) => {{
use anyhow::{Context, Result as AnyResult};
Expand Down
2 changes: 1 addition & 1 deletion tests/test_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ fn test_assert_generate_files() {
params.output =
Some(temp_dir.as_path().to_str().unwrap().to_owned());

assert_generate_files!(params);
assert_generate_files!(params.clone());
assert!(temp_dir.exists());
std::fs::remove_dir_all(temp_dir).unwrap();
}
Expand Down
4 changes: 2 additions & 2 deletions tests/test_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod tests {

extern crate libmake;
use libmake::generator::{
create_directory, generate_files, generate_from_csv,
generate_files, generate_from_csv,
generate_from_json, generate_from_yaml,
};
use libmake::generator::{
Expand All @@ -27,7 +27,7 @@ mod tests {
fn test_generate_files() {
let mut params = FileGenerationParams::new();
params.output = Some("my_library".into());
assert_generate_files!(params);
assert_generate_files!(params.clone());
}
// Unit test for the `generate_from_csv()` function.
#[test]
Expand Down

0 comments on commit 52b89c6

Please sign in to comment.