Skip to content

Commit f7c47c3

Browse files
committed
fix: add validate img according to precedent fix
1 parent 53222d1 commit f7c47c3

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

src/cli.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// to those terms.
55

66
use clap::{Args, Parser, Subcommand};
7-
use std::path::PathBuf;
7+
use std::{ffi::OsStr, path::PathBuf};
88

99
#[derive(Parser, Debug)]
1010
#[command(
@@ -79,7 +79,7 @@ pub struct DrawArgs {
7979
pub file: PathBuf,
8080

8181
/// Output file
82-
#[arg(short, value_parser = must_not_exist)]
82+
#[arg(short, value_parser = validate_image_output)]
8383
pub output: Option<PathBuf>,
8484
}
8585

@@ -117,6 +117,26 @@ fn must_not_exist(s: &str) -> Result<PathBuf, String> {
117117
}
118118
}
119119

120+
fn validate_image_output(s: &str) -> Result<PathBuf, String> {
121+
let mp = PathBuf::from(s);
122+
123+
if mp.exists() {
124+
return Err(format!("{} should not already exist.", mp.display()));
125+
}
126+
if let Some(ext) = mp.extension() {
127+
if ext != OsStr::new("png") {
128+
return Err(format!(
129+
"{} should have png extension (.png).",
130+
mp.display()
131+
));
132+
}
133+
} else {
134+
return Err("Output file must have an extension".to_string());
135+
}
136+
137+
Ok(mp)
138+
}
139+
120140
fn validate_block_width(val: &str) -> Result<usize, String> {
121141
match val.parse::<usize>() {
122142
Ok(v) => {

0 commit comments

Comments
 (0)