Skip to content

Commit

Permalink
fix(libmake): 🐛 Fixed error handling for ASCII art generation
Browse files Browse the repository at this point in the history
Integrated logger initialization in the run function. Implemented command-line argument parsing. Added welcome message for user guidance. Improved documentation and comments for clarity.
  • Loading branch information
sebastienrousseau committed Jan 17, 2024
1 parent 89bacec commit 29cc979
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 25 deletions.
38 changes: 17 additions & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright © 2023 xtasks. All rights reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// 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.

//! # `LibMake`
//!
Expand All @@ -21,10 +23,10 @@
//!
//! ## Overview
//!
//! `LibMake` is a tool designed to quickly help creating high-quality
//! `LibMake` is a tool designed to quickly help create high-quality
//! Rust libraries by generating a set of pre-filled and pre-defined
//! templated files. This opinionated boilerplate scaffolding tool aims
//! to greatly reduces development time and minimizes repetitive tasks,
//! to greatly reduce development time and minimize repetitive tasks,
//! allowing you to focus on your business logic while enforcing
//! standards, best practices, consistency, and providing style guides
//! for your library.
Expand All @@ -42,7 +44,7 @@
//!
//! `LibMake` offers the following features and benefits:
//!
//! - Create your Rust library with ease using the command line
//! - Create your Rust library with ease using the command-line
//! interface or by providing a configuration file in CSV, JSON, or
//! YAML format.
//! - Rapidly generate new library projects with a pre-defined structure
Expand Down Expand Up @@ -77,11 +79,12 @@
#![crate_type = "lib"]

// Import necessary dependencies
use crate::args::process_arguments;
use crate::ascii::generate_ascii_art;
use crate::cli::build;
use crate::loggers::init_logger;
use dtt::DateTime;
use env_logger::Env;
use rlg::macro_log;
use rlg::{LogFormat, LogLevel};
use rlg::{macro_log, LogFormat, LogLevel};
use std::error::Error;
use std::fs::File;
use std::io::Write;
Expand Down Expand Up @@ -121,7 +124,6 @@ pub mod utils;
/// }
/// ```
///
///
/// # Errors
///
/// This function will return an error in the following situations:
Expand All @@ -135,16 +137,10 @@ pub fn run() -> Result<(), Box<dyn Error>> {
let iso = date.iso_8601;

// Initialize the logger using the `env_logger` crate
env_logger::Builder::from_env(
Env::default().default_filter_or("info"),
)
.format(|buf, record| {
writeln!(buf, "[{}] - {}", record.level(), record.args())
})
.init();
init_logger(None)?;

// Open the log file for appending
let mut log_file = File::create("ssg.log")?;
let mut log_file = File::create("./ssg.log")?;

// Generate ASCII art for the tool's CLI
let log = macro_log!(
Expand All @@ -159,8 +155,8 @@ pub fn run() -> Result<(), Box<dyn Error>> {
writeln!(log_file, "{}", log)?;

match generate_ascii_art("LibMake") {
Ok(ascii_art) => println!("{ascii_art}"),
Err(e) => eprintln!("Error generating ASCII art: {e}"),
Ok(ascii_art) => println!("{}", ascii_art),
Err(e) => eprintln!("Error generating ASCII art: {:?}", e),
}
let log = macro_log!(
"id",
Expand All @@ -174,8 +170,8 @@ pub fn run() -> Result<(), Box<dyn Error>> {
writeln!(log_file, "{}", log)?;

// Build the command-line interface and process the arguments
let matches = cli::build()?;
args::process_arguments(&matches)?;
let matches = build()?;
process_arguments(&matches)?;

// Check the number of arguments, provide a welcome message if no arguments were passed
if std::env::args().len() == 1 {
Expand Down
34 changes: 30 additions & 4 deletions src/loggers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,35 @@
//!
//! Provides access to logging functions and types.
//!
use env_logger::Env;
use rlg::LogLevel;
use std::io::Write;

/// Re-exported main [`rlg`] module from rlg for application logging.
pub use rlg::*;
/// Initializes the logging system.
///
/// This function sets up the logging system using the `env_logger` crate. It takes a `default_log_level` parameter, which determines the minimum log level to be displayed. The function returns a `Result` type, which will be `Ok` if the logging system is initialized successfully, or an error if there was a problem.
///
/// # Examples
///
/// ```
/// use rlg::LogLevel;
/// use libmake::loggers::init_logger;
///
/// // Initialize the logging system with a default log level of `info`
/// init_logger(Some(LogLevel::INFO)).unwrap();
/// ```
pub fn init_logger(
default_log_level: Option<LogLevel>,
) -> Result<(), Box<dyn std::error::Error>> {
let env = Env::default().default_filter_or(
default_log_level.unwrap_or(LogLevel::INFO).to_string(),
);

/// Re-exported [`rlg_macros`] module from rlg.
pub use rlg::macros as rlg_macros;
env_logger::Builder::from_env(env)
.format(|buf, record| {
writeln!(buf, "[{}] - {}", record.level(), record.args())
})
.init();

Ok(())
}

0 comments on commit 29cc979

Please sign in to comment.