From fed3e702d9939ad65ec1a2a37c95932dcbc92fa1 Mon Sep 17 00:00:00 2001 From: Dobromir Marinov Date: Wed, 20 Sep 2023 16:19:16 +0100 Subject: [PATCH] Refactored folder and directory creation. --- src/error.rs | 15 ++++++++++ src/main.rs | 4 +-- src/utils.rs | 77 ++++++++++++++++++++++------------------------------ 3 files changed, 49 insertions(+), 47 deletions(-) diff --git a/src/error.rs b/src/error.rs index e316882..197aa16 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,3 +1,4 @@ +use crate::error::CliError::{CreateDir, CreateFile}; use std::error::Error; use std::fmt; use std::fmt::Display; @@ -9,6 +10,20 @@ pub(crate) enum CliError { ProjectName, } +impl CliError { + pub(crate) fn create_dir_err(dir: String, description: String) -> Self { + CreateDir { dir, description } + } + + pub(crate) fn create_file_err(file: String, description: String) -> Self { + CreateFile { file, description } + } + + pub(crate) fn project_name_err() -> Self { + CliError::ProjectName + } +} + impl Error for CliError {} impl Display for CliError { diff --git a/src/main.rs b/src/main.rs index f2c44ae..8f55479 100644 --- a/src/main.rs +++ b/src/main.rs @@ -35,12 +35,12 @@ fn main() { { // Creates the directories and files recursively if path.is_dir() { - if let Err(err) = create_dir(path, &args) { + if let Err(err) = create_dir(&path, &args) { println!("{}", err); std::process::exit(1) } } else if path.is_file() { - if let Err(err) = create_file(path, &args) { + if let Err(err) = create_file(&path, &args) { println!("{}", err); std::process::exit(1) } diff --git a/src/utils.rs b/src/utils.rs index d399aa6..3961227 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,8 +1,10 @@ use crate::error::CliError; use crate::{Args, PROJECT_TEMPLATE_FOLDER}; use regex::Regex; +use serde::Serialize; +use std::error::Error; use std::fs; -use std::path::{Path, PathBuf}; +use std::path::Path; use tera::Tera; use walkdir::DirEntry; @@ -15,7 +17,7 @@ pub(crate) fn validate_name(name: &str) -> Result { { Ok(name) } else { - Err(CliError::ProjectName) + Err(CliError::project_name_err()) } } @@ -27,55 +29,40 @@ pub(crate) fn is_hidden(entry: &DirEntry) -> bool { .unwrap_or(false) } -pub(crate) fn create_dir(input_dir: PathBuf, args: &Args) -> Result<(), CliError> { - let output_dir = - Path::new(&args.name).join(input_dir.strip_prefix(PROJECT_TEMPLATE_FOLDER).map_err( - |err| CliError::CreateDir { - dir: input_dir.display().to_string(), - description: err.to_string(), - }, - )?); - fs::create_dir(output_dir.display().to_string()).map_err(|err| CliError::CreateDir { - dir: output_dir.display().to_string(), - description: err.kind().to_string(), +pub(crate) fn create_dir(input_dir: &Path, args: &Args) -> Result<(), CliError> { + let output_dir = get_output_dir(input_dir, args).map_err(|err| { + CliError::create_dir_err(input_dir.display().to_string(), err.to_string()) })?; + fs::create_dir(&output_dir) + .map_err(|err| CliError::create_dir_err(output_dir, err.kind().to_string()))?; Ok(()) } -pub(crate) fn create_file(input_file: PathBuf, args: &Args) -> Result<(), CliError> { - let output_file = - Path::new(&args.name).join(input_file.strip_prefix(PROJECT_TEMPLATE_FOLDER).map_err( - |err| CliError::CreateFile { - file: input_file.display().to_string(), - description: err.to_string(), - }, - )?); - let input_text = fs::read_to_string(input_file).map_err(|err| CliError::CreateFile { - file: output_file.display().to_string(), - description: err.to_string(), +pub(crate) fn create_file(input_file: &Path, args: &Args) -> Result<(), CliError> { + let output_file = get_output_dir(input_file, args).map_err(|err| { + CliError::create_file_err(input_file.display().to_string(), err.to_string()) })?; + let input_text = fs::read_to_string(input_file) + .map_err(|err| CliError::create_file_err(output_file.clone(), err.kind().to_string()))?; + let output_text = replace_text(&input_text, args) + .map_err(|err| CliError::create_file_err(output_file.clone(), err.to_string()))?; + fs::write(&output_file, output_text) + .map_err(|err| CliError::create_file_err(output_file, err.kind().to_string()))?; - let mut tera = Tera::default(); - tera.add_raw_template("file", &input_text) - .map_err(|err| CliError::CreateFile { - file: output_file.display().to_string(), - description: err.to_string(), - })?; - let context = tera::Context::from_serialize(args).map_err(|err| CliError::CreateFile { - file: output_file.display().to_string(), - description: err.to_string(), - })?; - let output_text = tera - .render("file", &context) - .map_err(|err| CliError::CreateFile { - file: output_file.display().to_string(), - description: err.to_string(), - })?; + Ok(()) +} - fs::write(&output_file, output_text).map_err(|err| CliError::CreateFile { - file: output_file.display().to_string(), - description: err.to_string(), - })?; +pub(crate) fn get_output_dir(input_dir: &Path, args: &Args) -> Result> { + let output_dir = Path::new(&args.name).join(input_dir.strip_prefix(PROJECT_TEMPLATE_FOLDER)?); + replace_text(&output_dir.display().to_string(), args) +} - Ok(()) +pub(crate) fn replace_text( + template_text: &str, + values: impl Serialize, +) -> Result> { + let mut tera = Tera::default(); + tera.add_raw_template("text", template_text)?; + let context = tera::Context::from_serialize(values)?; + Ok(tera.render("text", &context)?) }