Skip to content

Commit 3b66193

Browse files
Improve CLI error reporting
1 parent 0c61f73 commit 3b66193

2 files changed

Lines changed: 50 additions & 33 deletions

File tree

src/bin/ffbuildtool.rs

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::{
66

77
use clap::{Args, Parser, Subcommand};
88

9-
use ffbuildtool::{Error, ItemProgress, Version};
9+
use ffbuildtool::{ItemProgress, Version};
1010
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
1111
use uuid::Uuid;
1212

@@ -199,10 +199,16 @@ impl ProgressManager {
199199
}
200200
}
201201

202+
async fn parse_manifest(path: &str) -> Result<Version, String> {
203+
Version::from_manifest(path)
204+
.await
205+
.map_err(|e| format!("Couldn't parse manifest: {}", e))
206+
}
207+
202208
static PROGRESS: OnceLock<ProgressManager> = OnceLock::new();
203209

204210
#[tokio::main]
205-
async fn main() -> Result<(), Error> {
211+
async fn main() -> Result<(), String> {
206212
PROGRESS
207213
.set(ProgressManager::new())
208214
.unwrap_or_else(|_| panic!());
@@ -217,13 +223,13 @@ async fn main() -> Result<(), Error> {
217223
}
218224
}
219225

220-
async fn generate_manifest(args: GenManifestArgs) -> Result<(), Error> {
226+
async fn generate_manifest(args: GenManifestArgs) -> Result<(), String> {
221227
println!(
222228
"Generating manifest for build at {} with asset URL {}",
223229
args.build_path, args.asset_url
224230
);
225231
let parent_uuid: Option<Uuid> = if let Some(p) = args.parent {
226-
Some(Uuid::parse_str(p.as_str())?)
232+
Some(Uuid::parse_str(p.as_str()).map_err(|_| "Invalid parent UUID".to_string())?)
227233
} else {
228234
None
229235
};
@@ -235,21 +241,24 @@ async fn generate_manifest(args: GenManifestArgs) -> Result<(), Error> {
235241
args.description.as_deref(),
236242
parent_uuid,
237243
)
238-
.await?;
244+
.await
245+
.map_err(|e| format!("Couldn't generate bundle info: {}", e))?;
239246

240247
if args.hidden {
241248
version.set_hidden(true);
242249
}
243250

244251
println!("Build UUID: {}", version.get_uuid());
245252

246-
version.export_manifest(&args.output_path)?;
253+
version
254+
.export_manifest(&args.output_path)
255+
.map_err(|e| format!("Couldn't export manifest: {}", e))?;
247256
println!("Manifest exported to {}", args.output_path);
248257
Ok(())
249258
}
250259

251-
async fn download_build(args: DownloadBuildArgs) -> Result<(), Error> {
252-
let version = Version::from_manifest(&args.manifest_path).await?;
260+
async fn download_build(args: DownloadBuildArgs) -> Result<(), String> {
261+
let version = parse_manifest(&args.manifest_path).await?;
253262
println!(
254263
"Downloading build {} to {}",
255264
version.get_uuid(),
@@ -262,13 +271,14 @@ async fn download_build(args: DownloadBuildArgs) -> Result<(), Error> {
262271

263272
version
264273
.download_compressed(&args.output_path, Some(Arc::new(cb)))
265-
.await?;
274+
.await
275+
.map_err(|e| format!("Couldn't download build: {}", e))?;
266276
println!("Download complete");
267277
Ok(())
268278
}
269279

270-
async fn repair_build(args: RepairBuildArgs) -> Result<(), Error> {
271-
let version = Version::from_manifest(&args.manifest_path).await?;
280+
async fn repair_build(args: RepairBuildArgs) -> Result<(), String> {
281+
let version = parse_manifest(&args.manifest_path).await?;
272282
println!(
273283
"Repairing build {} at {}",
274284
version.get_uuid(),
@@ -279,7 +289,10 @@ async fn repair_build(args: RepairBuildArgs) -> Result<(), Error> {
279289
PROGRESS.get().unwrap().update_item(name, progress);
280290
};
281291

282-
let corrupted = version.repair(&args.build_path, Some(Arc::new(cb))).await?;
292+
let corrupted = version
293+
.repair(&args.build_path, Some(Arc::new(cb)))
294+
.await
295+
.map_err(|e| format!("Couldn't repair build: {}", e))?;
283296
if corrupted.is_empty() {
284297
println!("No corrupted files found");
285298
} else {
@@ -291,8 +304,8 @@ async fn repair_build(args: RepairBuildArgs) -> Result<(), Error> {
291304
Ok(())
292305
}
293306

294-
async fn validate_build(args: ValidateBuildArgs) -> Result<(), Error> {
295-
let version = Version::from_manifest(&args.manifest_path).await?;
307+
async fn validate_build(args: ValidateBuildArgs) -> Result<(), String> {
308+
let version = parse_manifest(&args.manifest_path).await?;
296309
println!(
297310
"Validating build {} at {}",
298311
version.get_uuid(),
@@ -306,11 +319,13 @@ async fn validate_build(args: ValidateBuildArgs) -> Result<(), Error> {
306319
let corrupted = if args.uncompressed {
307320
version
308321
.validate_uncompressed(&args.build_path, None)
309-
.await?
322+
.await
323+
.map_err(|e| format!("Couldn't validate uncompressed files: {}", e))?
310324
} else {
311325
version
312326
.validate_compressed(&args.build_path, Some(Arc::new(cb)))
313-
.await?
327+
.await
328+
.map_err(|e| format!("Couldn't validate compressed files: {}", e))?
314329
};
315330

316331
if corrupted.is_empty() {
@@ -325,7 +340,7 @@ async fn validate_build(args: ValidateBuildArgs) -> Result<(), Error> {
325340
}
326341

327342
#[cfg(feature = "lzma")]
328-
async fn extract_bundle(args: ExtractBundleArgs) -> Result<(), Error> {
343+
async fn extract_bundle(args: ExtractBundleArgs) -> Result<(), String> {
329344
use std::path::PathBuf;
330345

331346
let asset_bundle = ffbuildtool::bundle::AssetBundle::from_file(&args.bundle_path)?;

src/bundle.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -357,9 +357,6 @@ impl AssetBundle {
357357
);
358358
}
359359

360-
#[cfg(debug_assertions)]
361-
dbg!(&header);
362-
363360
// seek to first level
364361
let offset = reader.reader_bytes();
365362
skip_exact(&mut reader, header.header_size as usize - offset)?;
@@ -377,9 +374,6 @@ impl AssetBundle {
377374
}
378375
}
379376

380-
#[cfg(debug_assertions)]
381-
dbg!(&levels);
382-
383377
Ok(Self { levels })
384378
}
385379

@@ -405,18 +399,23 @@ impl AssetBundle {
405399
Ok(())
406400
}
407401

408-
pub fn from_file(path: &str) -> Result<Self, Error> {
409-
let file = File::open(path)?;
410-
let metadata = file.metadata()?;
402+
pub fn from_file(path: &str) -> Result<Self, String> {
403+
let file = File::open(path).map_err(|e| format!("Couldn't open file {}: {}", path, e))?;
404+
let metadata = file.metadata().unwrap();
411405
let mut reader = BufReader::new(file);
412406
Self::read(&mut reader, metadata.len() as u32)
407+
.map_err(|e| format!("Couldn't read bundle: {}", e))
413408
}
414409

415-
pub fn to_file(&self, path: &str) -> Result<(), Error> {
416-
let file = File::create(path)?;
410+
pub fn to_file(&self, path: &str) -> Result<(), String> {
411+
let file =
412+
File::create(path).map_err(|e| format!("Couldn't create file {}: {}", path, e))?;
417413
let mut writer = BufWriter::new(file);
418-
self.write(&mut writer)?;
419-
writer.flush()?;
414+
self.write(&mut writer)
415+
.map_err(|e| format!("Couldn't write bundle: {}", e))?;
416+
writer
417+
.flush()
418+
.map_err(|e| format!("Couldn't finish writing bundle: {}", e))?;
420419
Ok(())
421420
}
422421

@@ -438,20 +437,23 @@ impl AssetBundle {
438437
Ok(result)
439438
}
440439

441-
pub fn extract_files(&self, output_dir: &str) -> Result<(), Error> {
440+
pub fn extract_files(&self, output_dir: &str) -> Result<(), String> {
442441
let make_subdirs = self.levels.len() > 1;
443442
for (i, level) in self.levels.iter().enumerate() {
444443
let level_dir = if make_subdirs {
445444
format!("{}/level{}", output_dir, i)
446445
} else {
447446
output_dir.to_string()
448447
};
449-
util::create_dir_if_needed(&level_dir)?;
448+
util::create_dir_if_needed(&level_dir)
449+
.map_err(|e| format!("Couldn't create dir {}: {}", level_dir, e))?;
450450

451451
let dir_path = Path::new(&level_dir);
452452
for file in &level.files {
453453
let file_path = dir_path.join(&file.name);
454-
std::fs::write(&file_path, &file.data)?;
454+
std::fs::write(&file_path, &file.data).map_err(|e| {
455+
format!("Couldn't write file {}/{}: {}", level_dir, file.name, e)
456+
})?;
455457
}
456458
}
457459
Ok(())

0 commit comments

Comments
 (0)