aarty/src/main.rs

85 lines
2.3 KiB
Rust
Raw Normal View History

2022-10-03 18:13:59 +00:00
use std::io::Write;
use clap::Parser;
use image::GenericImageView;
2022-10-03 18:50:48 +00:00
extern crate pretty_env_logger;
2022-10-03 18:13:59 +00:00
#[macro_use]
extern crate log;
2022-10-03 14:04:38 +00:00
mod args;
2022-10-03 18:13:59 +00:00
mod ascii_processor;
2022-10-03 14:04:38 +00:00
2022-10-03 18:13:59 +00:00
use crate::args::{
args::Arguments,
2022-10-03 18:50:48 +00:00
enums::OutputMethod,
2022-10-03 18:13:59 +00:00
};
use crate::ascii_processor::generate_ascii;
2022-10-03 14:04:38 +00:00
2022-10-03 11:09:12 +00:00
fn main() {
// Initialize the logger
pretty_env_logger::init();
info!("Successfully initialized logger");
info!("Parsing arguments");
// Parse the arguments
2022-10-03 14:04:38 +00:00
let arguments = Arguments::parse();
info!("Successfully parsed arguments");
trace!("Arguments: {:?}", arguments);
2022-10-03 18:13:59 +00:00
// Validate the arguments
info!("Validating arguments");
match arguments.validate() {
Ok(_) => (),
Err(e) => {
error!("Failed to validate arguments: {}", e);
eprintln!("Failed to validate arguments: {}", e);
std::process::exit(1);
}
}
// Open the image
info!("Opening image: {}", arguments.image);
2022-10-03 18:13:59 +00:00
let image = match image::open(arguments.image.clone()) {
Ok(image) => image,
Err(e) => {
error!("Failed to open image: {:?}", e);
eprintln!("Failed to open image: {:?}", e);
std::process::exit(1);
}
};
info!("Successfully opened image");
trace!("Image dimensions: {:?}", image.dimensions());
2022-10-03 18:13:59 +00:00
// Process the image
2022-10-03 18:50:48 +00:00
let output = generate_ascii(image, &arguments);
info!("Successfully processed image");
2022-10-03 18:13:59 +00:00
// Output the image
info!("Outputting image");
match arguments.output_method {
OutputMethod::File => {
match std::fs::write(
arguments.output.clone(),
output.iter()
.map(|s| format!("{}", s))
.collect::<String>(),
) {
2022-10-03 18:13:59 +00:00
Ok(_) => info!("Successfully outputted image: {}", arguments.output),
Err(e) => {
error!("Failed to output image: {:?}", e);
eprintln!("Failed to output image: {:?}", e);
std::process::exit(1);
}
}
}
OutputMethod::Stdout => {
for char in output {
print!("{}", char);
std::io::stdout().flush().unwrap();
}
info!("Successfully outputted image");
}
}
2022-10-03 11:09:12 +00:00
}