Skip to content

Commit 5e938ef

Browse files
committed
Quit silently when everything is fine
1 parent e919de5 commit 5e938ef

File tree

1 file changed

+39
-16
lines changed

1 file changed

+39
-16
lines changed

src/main.rs

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,14 @@ If --all is not specified, the file will be saved as <output>.ico
6060
force: bool,
6161
}
6262

63-
fn core(args: ToIconArgs) {
63+
/// # Returns
64+
/// - `bool`: `true` if everything went well, `false` otherwise.
65+
fn core(args: ToIconArgs) -> bool {
6466
let image_file = path::Path::new(&args.image);
67+
println!("Processing: {}", image_file.display());
6568
if !image_file.exists() {
6669
eprintln!("Error: The image file '{}' does not exist.", args.image);
67-
return;
70+
return false;
6871
}
6972

7073
let image_name = image_file
@@ -77,7 +80,7 @@ fn core(args: ToIconArgs) {
7780
Ok(img) => img,
7881
Err(e) => {
7982
eprintln!("Error: Failed to open image file '{}': {}", args.image, e);
80-
return;
83+
return false;
8184
}
8285
};
8386

@@ -95,7 +98,7 @@ fn core(args: ToIconArgs) {
9598
eprintln!("Error: Failed to decode image file '{}': {}", args.image, e);
9699
}
97100
}
98-
return;
101+
return false;
99102
}
100103
};
101104

@@ -105,8 +108,7 @@ fn core(args: ToIconArgs) {
105108
if !args.all {
106109
let save_path = format!("{output}.ico");
107110
let save_path = path::Path::new(&save_path);
108-
save_icon(&img, save_path, args.size.into(), args.force);
109-
return;
111+
return save_icon(&img, save_path, args.size.into(), args.force);
110112
}
111113

112114
let output_dir = format!("{output}_ico");
@@ -118,28 +120,34 @@ fn core(args: ToIconArgs) {
118120
output_dir.display(),
119121
e
120122
);
121-
return;
123+
return false;
122124
}
123125

126+
let mut ok = true;
127+
124128
let sizes = [16, 24, 32, 48, 64, 128, 256];
125129
for size in sizes {
126130
let save_path = output_dir.join(format!("{output}_{size}.ico"));
127-
save_icon(&img, &save_path, size, args.force);
131+
ok = save_icon(&img, &save_path, size, args.force) && ok;
128132

129133
if size == args.size.into() {
130134
let save_path = output_dir.join(format!("{output}.ico"));
131-
save_icon(&img, &save_path, size, args.force);
135+
ok = save_icon(&img, &save_path, size, args.force) && ok;
132136
}
133137
}
138+
139+
return ok;
134140
}
135141

136-
fn save_icon(img: &image::DynamicImage, path: &path::Path, size: u32, force: bool) {
142+
/// # Returns
143+
/// - `bool`: `true` if the icon was saved successfully, `false` otherwise.
144+
fn save_icon(img: &image::DynamicImage, path: &path::Path, size: u32, force: bool) -> bool {
137145
if path.exists() && !force {
138146
eprintln!(
139147
"Warning: File '{}' already exists. Use --force to overwrite.",
140148
path.display()
141149
);
142-
return;
150+
return false;
143151
}
144152

145153
let img = if img.dimensions() == (size, size) {
@@ -148,16 +156,31 @@ fn save_icon(img: &image::DynamicImage, path: &path::Path, size: u32, force: boo
148156
&img.resize_exact(size, size, image::imageops::FilterType::Lanczos3)
149157
};
150158

151-
img.save(path).unwrap_or_else(|e| {
152-
eprintln!("Error: Failed to save '{}': {}", size, e);
153-
});
159+
println!(
160+
"Saving icon to: '{}' with size {}x{}",
161+
path.display(),
162+
size,
163+
size
164+
);
165+
166+
if let Err(e) = img.save(path) {
167+
eprintln!("Error: Failed to save '{}': {}", path.display(), e);
168+
false
169+
} else {
170+
true
171+
}
154172
}
155173

156174
fn main() {
157-
if let Ok(args) = ToIconArgs::try_parse() {
158-
core(args);
175+
let ok = if let Ok(args) = ToIconArgs::try_parse() {
176+
core(args)
159177
} else {
160178
_ = ToIconArgs::command().print_help();
179+
false
180+
};
181+
182+
if ok {
183+
return;
161184
}
162185

163186
println!("\nPress Enter or wait 3 seconds to exit...");

0 commit comments

Comments
 (0)