From 35d5dc6b2de49e4363421765236bc9d506d6fb23 Mon Sep 17 00:00:00 2001 From: opencode Date: Sun, 5 Jul 2026 14:50:40 +0000 Subject: [PATCH] fix(#12): human-readable libraw error messages --- src/main.rs | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 0610ca2..569793e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -55,6 +55,22 @@ fn sanitize_path( } } +/// Map libraw error codes to human-readable strings. +fn libraw_error_string(code: c_int) -> &'static str { + match code { + 0 => "Success", + -1 => "General failure (LIBRAW_ERROR_UNSUCCESSFUL)", + -2 => "Unsupported file format", + -3 => "File open failed (LIBRAW_ERROR_FILE_OPEN_FAIL)", + -4 => "File is not recognized as RAW", + -5 => "Out of memory", + -6 => "File read failed", + -7 => "Internal error", + -8 => "Unsupported operation", + _ => "Unknown error code", + } +} + /// Photo Editor - A Rust-based photo editor with RAW image processing capabilities using libraw #[derive(Parser, Debug)] #[command(author, version, about, long_about = None)] @@ -223,7 +239,7 @@ fn main() -> Result<(), Box> { let ret = unsafe { libraw_open_file(lr, input_cstr.as_ptr()) }; if ret != LIBRAW_SUCCESS { - eprintln!("Error: Failed to open file. Error code: {}", ret); + eprintln!("Error: Failed to open file: {}", libraw_error_string(ret)); unsafe { libraw_close(lr) }; return Err("Failed to open file".into()); } @@ -233,7 +249,7 @@ fn main() -> Result<(), Box> { // Process the RAW data let ret = unsafe { libraw_unpack(lr) }; if ret != LIBRAW_SUCCESS { - eprintln!("Error: Failed to unpack RAW data. Error code: {}", ret); + eprintln!("Error: Failed to unpack RAW data: {}", libraw_error_string(ret)); unsafe { libraw_close(lr) }; return Err("Failed to unpack RAW data".into()); } @@ -248,7 +264,7 @@ fn main() -> Result<(), Box> { // Process the image let ret = unsafe { libraw_dcraw_process(lr) }; if ret != LIBRAW_SUCCESS { - eprintln!("Error: Failed to process image. Error code: {}", ret); + eprintln!("Error: Failed to process image: {}", libraw_error_string(ret)); unsafe { libraw_close(lr) }; return Err("Failed to process image".into()); } @@ -300,7 +316,7 @@ fn main() -> Result<(), Box> { }; if ret != LIBRAW_SUCCESS { - eprintln!("Error: Failed to write output file. Error code: {}", ret); + eprintln!("Error: Failed to write output file: {}", libraw_error_string(ret)); unsafe { libraw_close(lr) }; return Err("Failed to write output file".into()); }