Skip to content

Commit

Permalink
feat: add warning for protected dirs '.' and '..'
Browse files Browse the repository at this point in the history
  • Loading branch information
msmoiz committed Aug 24, 2024
1 parent f847d52 commit 0d2f0bb
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
authors = ["Mustafa <mustafa.moiz125@gmail.com>"]
edition = "2018"
name = "renom"
version = "1.2.0"
version = "1.3.0"
description = "A simple program to rename Unreal Engine projects."
keywords = ["gamedev", "ue4", "ue5", "unreal_engine", "rename"]
categories = [
Expand Down
11 changes: 11 additions & 0 deletions src/workflows/rename_project/interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,24 @@ pub fn get_params_from_user() -> Result<Params, String> {

fn get_project_root_from_user() -> Result<PathBuf, String> {
Text::new("Project root directory path:")
.with_validator(validate_project_root_is_not_special)
.with_validator(validate_project_root_is_dir)
.with_validator(validate_project_root_contains_project_descriptor)
.prompt()
.map(|project_root| PathBuf::from(project_root))
.map_err(|err| err.to_string())
}

fn validate_project_root_is_not_special(project_root: &str) -> Result<Validation, CustomUserError> {
match project_root {
"." => Ok(Validation::Invalid("Provided path '.' is protected".into())),
".." => Ok(Validation::Invalid(
"Provided path '..' is protected".into(),
)),
_ => Ok(Validation::Valid),
}
}

fn validate_project_root_is_dir(project_root: &str) -> Result<Validation, CustomUserError> {
match PathBuf::from(project_root).is_dir() {
true => Ok(Validation::Valid),
Expand Down
9 changes: 9 additions & 0 deletions src/workflows/rename_project/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub fn rename_project(params: Params) -> Result<(), String> {
}

fn validate_params(params: &Params) -> Result<(), String> {
validate_project_root_is_not_special(&params.project_root)?;
validate_project_root_is_dir(&params.project_root)?;
validate_project_root_contains_project_descriptor(&params.project_root)?;
let project_name = detect_project_name(&params.project_root)?;
Expand All @@ -67,6 +68,14 @@ fn validate_params(params: &Params) -> Result<(), String> {
Ok(())
}

fn validate_project_root_is_not_special(project_root: &Path) -> Result<(), String> {
match project_root {
path if path == Path::new(".") => Err("project root cannot be '.'".into()),
path if path == Path::new("..") => Err("project root cannot be '..'".into()),
_ => Ok(()),
}
}

fn validate_project_root_is_dir(project_root: &Path) -> Result<(), String> {
match project_root.is_dir() {
true => Ok(()),
Expand Down

0 comments on commit 0d2f0bb

Please sign in to comment.