From 5de9976d1cc5467db64b744bc6b903b01bc7883f Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Wed, 28 Oct 2026 19:42:42 +0000 Subject: [PATCH] Extract common logic in examples --- examples/canny.rs | 41 +++++++---------------------------------- examples/common/mod.rs | 33 +++++++++++++++++++++++++++++++++ examples/greyscale.rs | 35 +++++------------------------------ 3 files changed, 45 insertions(+), 64 deletions(-) create mode 100644 examples/common/mod.rs diff --git a/examples/canny.rs b/examples/canny.rs index 1413980..a20e9fd 100644 --- a/examples/canny.rs +++ b/examples/canny.rs @@ -1,38 +1,11 @@ +mod common; + use ccvl::filter; -use ccvl::image::*; +use ccvl::image; fn main() -> std::process::ExitCode { - let args = std::env::args().collect::>(); - if args.len() != 3 { - println!("Usage: {} IN OUT", args[0].as_str()); - return std::process::ExitCode::FAILURE; - } - - let input_path = args[1].as_str(); - let output_path = args[2].as_str(); - - match Image::load::(input_path) { - Err(Error::Invalid) => { - println!("{} is not a valid image", input_path); - std::process::ExitCode::FAILURE - } - Err(Error::Io(err)) => { - println!("I/O error loading image {}: {}", input_path, err); - std::process::ExitCode::FAILURE - } - Ok(img) => { - let result = filter::canny(img.map(|p| Luminance::from(p))) - .map(|p| p.into()); - match result.save::(output_path) { - Err(err) => { - println!( - "I/O error saving image {}: {}", - output_path, err - ); - std::process::ExitCode::FAILURE - } - Ok(()) => std::process::ExitCode::SUCCESS, - } - } - } + common::process_image(|img| { + let greyscale = img.map(|p| image::Luminance::from(p)); + filter::canny(greyscale).map(|p| p.into()) + }) } diff --git a/examples/common/mod.rs b/examples/common/mod.rs new file mode 100644 index 0000000..0e2b432 --- /dev/null +++ b/examples/common/mod.rs @@ -0,0 +1,33 @@ +use ccvl::image::*; + +pub fn process_image(f: F) -> std::process::ExitCode +where + F: FnOnce(Image) -> Image, +{ + let args = std::env::args().collect::>(); + if args.len() != 3 { + println!("Usage: {} IN OUT", args[0].as_str()); + return std::process::ExitCode::FAILURE; + } + + let input_path = args[1].as_str(); + let output_path = args[2].as_str(); + + match Image::load::(input_path) { + Err(Error::Invalid) => { + println!("{} is not a valid image", input_path); + std::process::ExitCode::FAILURE + } + Err(Error::Io(err)) => { + println!("I/O error loading image {}: {}", input_path, err); + std::process::ExitCode::FAILURE + } + Ok(img) => match f(img).save::(output_path) { + Err(err) => { + println!("I/O error saving image {}: {}", output_path, err); + std::process::ExitCode::FAILURE + } + Ok(()) => std::process::ExitCode::SUCCESS, + }, + } +} diff --git a/examples/greyscale.rs b/examples/greyscale.rs index 8bc1eb9..0c8ff74 100644 --- a/examples/greyscale.rs +++ b/examples/greyscale.rs @@ -1,34 +1,9 @@ -use ccvl::image::*; +mod common; -fn filter(img: Image) -> Image { - img.map(|p| Luminance::from(p).into()) -} +use ccvl::image; fn main() -> std::process::ExitCode { - let args = std::env::args().collect::>(); - if args.len() != 3 { - println!("Usage: {} IN OUT", args[0].as_str()); - return std::process::ExitCode::FAILURE; - } - - let input_path = args[1].as_str(); - let output_path = args[2].as_str(); - - match Image::load::(input_path) { - Err(Error::Invalid) => { - println!("{} is not a valid image", input_path); - std::process::ExitCode::FAILURE - } - Err(Error::Io(err)) => { - println!("I/O error loading image {}: {}", input_path, err); - std::process::ExitCode::FAILURE - } - Ok(img) => match filter(img).save::(output_path) { - Err(err) => { - println!("I/O error saving image {}: {}", output_path, err); - std::process::ExitCode::FAILURE - } - Ok(()) => std::process::ExitCode::SUCCESS, - }, - } + common::process_image(|img| { + img.map(|p| image::Luminance::from(p).into()) + }) }