Skip to content

Commit 286fde4

Browse files
committed
update command validation error
1 parent 2018a42 commit 286fde4

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

src/cli.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,10 @@ pub struct LocalArgs {
106106
fn validate_output_target(target: &str) -> Result<PathBuf, String> {
107107
let path = PathBuf::from(target);
108108
if !path.is_dir() {
109-
let cmd_err =
110-
commands::CommandError::OutputTargetInvalid(utils::absolute_path_as_string(&path))
111-
.to_string();
109+
let cmd_err = commands::CommandValidationError::OutputTargetInvalid(
110+
utils::absolute_path_as_string(&path),
111+
)
112+
.to_string();
112113
Err(cmd_err)
113114
} else {
114115
Ok(path)
@@ -123,7 +124,8 @@ fn check_file_name(filename: &str) -> Result<String, String> {
123124
Ok(_) => Ok(filename.to_string()),
124125
Err(validate_err) => {
125126
let cmd_err =
126-
commands::CommandError::InvalidFilename(validate_err.to_string()).to_string();
127+
commands::CommandValidationError::InvalidFilename(validate_err.to_string())
128+
.to_string();
127129
Err(cmd_err)
128130
}
129131
}
@@ -134,7 +136,8 @@ fn validate_hash_target(target: &str) -> Result<PathBuf, String> {
134136
let path = PathBuf::from(target);
135137
if !path.exists() {
136138
let cmd_err =
137-
commands::CommandError::PathNotExist(utils::absolute_path_as_string(&path)).to_string();
139+
commands::CommandValidationError::PathNotExist(utils::absolute_path_as_string(&path))
140+
.to_string();
138141
Err(cmd_err)
139142
} else {
140143
Ok(path)

src/commands.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,40 +27,41 @@ pub struct HashCompareResult {
2727

2828
// Represents possible cli command errors
2929
#[derive(Debug, PartialEq, PartialOrd)]
30-
pub enum CommandError {
30+
pub enum CommandValidationError {
3131
PathNotExist(String),
3232
InvalidUrl,
3333
InvalidHashSum,
3434
OutputTargetInvalid(String),
3535
InvalidFilename(String),
3636
}
3737

38-
impl Error for CommandError {}
38+
impl Error for CommandValidationError {}
3939

40-
impl fmt::Display for CommandError {
40+
impl fmt::Display for CommandValidationError {
4141
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42+
let prefix = "Validation failed →";
4243
match self {
43-
CommandError::PathNotExist(path) => {
44+
CommandValidationError::PathNotExist(path) => {
4445
let msg = format!("The specified path '{}' does not exist", path);
4546
write!(f, "{}", msg)
4647
}
47-
CommandError::InvalidUrl => {
48-
write!(f, "Command error => The specified URL is invalid. Please ensure the URL is correctly formatted, including the scheme (e.g. 'http://', 'https://'). For example: https://example.com")
48+
CommandValidationError::InvalidUrl => {
49+
write!(f, "{prefix} The specified URL is invalid. Please ensure the URL is correctly formatted, including the scheme (e.g. 'http://', 'https://'). For example: https://example.com")
4950
}
50-
CommandError::InvalidHashSum => {
51+
CommandValidationError::InvalidHashSum => {
5152
write!(
5253
f,
53-
"Command error => The specified hash sum is not a valid hexadecimal digit"
54+
"{prefix} The specified hash sum is not a valid hexadecimal digit"
5455
)
5556
}
56-
CommandError::OutputTargetInvalid(target) => {
57+
CommandValidationError::OutputTargetInvalid(target) => {
5758
let msg = format!(
58-
"Invalid target - '{}' does not exist or is not a directory",
59+
"Invalid output target - '{}' does not exist or is not a directory",
5960
target
6061
);
6162
write!(f, "{}", msg)
6263
}
63-
CommandError::InvalidFilename(filename_err) => {
64+
CommandValidationError::InvalidFilename(filename_err) => {
6465
let msg = format!("Invalid filename - {}", filename_err);
6566
write!(f, "{}", msg)
6667
}
@@ -86,14 +87,14 @@ pub fn handle_download_cmd(args: DownloadArgs, os_type: os_specifics::OS) -> Res
8687
let download_url = &args.url;
8788

8889
if !utils::is_valid_url(download_url) {
89-
let command_err = CommandError::InvalidUrl;
90+
let command_err = CommandValidationError::InvalidUrl;
9091
return Err(command_err.into());
9192
}
9293

9394
// Check if the provided hash is a valid hex digit
9495
if let Some(origin_hash_sum) = args.hash_sum.as_ref() {
9596
if !verify::is_hash_valid(origin_hash_sum) {
96-
return Err(CommandError::InvalidHashSum.into());
97+
return Err(CommandValidationError::InvalidHashSum.into());
9798
}
9899
}
99100

@@ -136,7 +137,7 @@ pub fn handle_local_cmd(args: LocalArgs) -> Result<()> {
136137
let cmd_result = if let Some(origin_hash_sum) = args.hash_sum {
137138
// Check if the provided hash is a valid hex digit
138139
if !verify::is_hash_valid(&origin_hash_sum) {
139-
return Err(CommandError::InvalidHashSum.into());
140+
return Err(CommandValidationError::InvalidHashSum.into());
140141
}
141142

142143
let is_hash_equal = verify::is_hash_equal(&origin_hash_sum, &calculated_hash_sum);

0 commit comments

Comments
 (0)