diff --git a/src/image_conversion.rs b/src/image_conversion.rs index 55a97be..8daa357 100644 --- a/src/image_conversion.rs +++ b/src/image_conversion.rs @@ -1,12 +1,13 @@ -use std::env; -use std::io::{stderr, stdout, Write}; -use std::path::{Path, PathBuf}; -use std::process::{Child, Command, Stdio}; -use std::sync::atomic::AtomicBool; -use std::sync::atomic::Ordering::Relaxed; -use std::thread::{JoinHandle, spawn}; +use std::{ + env, + io::{stderr, Write}, + path::Path, + process::{Child, Command, Stdio}, + sync::atomic::{AtomicBool, Ordering::Relaxed}, + thread::spawn, +}; + use color_eyre::eyre::{bail, Context, ContextCompat}; -use color_eyre::owo_colors::colors; pub static CAPTURE_IMAGE_CONVERTER: AtomicBool = AtomicBool::new(false); @@ -22,13 +23,14 @@ pub enum Converter { Imagemagick(Option), } - impl Converter { pub fn new_from_arg(input: &str) -> color_eyre::Result { let c = match input { - "imagemagick" => {Self::Imagemagick(env::var("CONVERTER_PATH").ok())} - "ffmpeg" => {Self::FFMPEG(env::var("CONVERTER_PATH").ok())} - &_ => {bail!("Unrecongized converter tool: {input}")} + "imagemagick" => Self::Imagemagick(env::var("CONVERTER_PATH").ok()), + "ffmpeg" => Self::FFMPEG(env::var("CONVERTER_PATH").ok()), + &_ => { + bail!("Unrecongized converter tool: {input}") + }, }; Ok(c) } @@ -37,22 +39,33 @@ impl AsRef for Converter { fn as_ref(&self) -> &str { #[cfg(not(windows))] match self { - Converter::FFMPEG(path) => {path.as_deref().unwrap_or("ffmpeg")} - Converter::Imagemagick(path) => {path.as_deref().unwrap_or("magick")} + Converter::FFMPEG(path) => path.as_deref().unwrap_or("ffmpeg"), + Converter::Imagemagick(path) => path.as_deref().unwrap_or("magick"), } #[cfg(windows)] match self { - Converter::FFMPEG(path) => {path.as_deref().unwrap_or("ffmpeg.exe")} - Converter::Imagemagick(path) => {path.as_deref().unwrap_or("magick")} + Converter::FFMPEG(path) => path.as_deref().unwrap_or("ffmpeg.exe"), + Converter::Imagemagick(path) => path.as_deref().unwrap_or("magick"), } } } impl ImageConverter { #[allow(unused)] - pub fn new_ffmpeg() -> Self { ImageConverter { validated: false, converter: Converter::FFMPEG(None) } } + pub fn new_ffmpeg() -> Self { + ImageConverter { + validated: false, + converter: Converter::FFMPEG(None), + } + } + #[allow(unused)] - pub fn new_imagemagick() -> Self { ImageConverter { validated: false, converter: Converter::Imagemagick(None) } } + pub fn new_imagemagick() -> Self { + ImageConverter { + validated: false, + converter: Converter::Imagemagick(None), + } + } pub fn new_with_converter(p: Converter) -> Self { ImageConverter { @@ -60,7 +73,7 @@ impl ImageConverter { converter: p, } } - + pub fn validate(&mut self) -> color_eyre::Result<()> { program_is_callable(Path::new(self.converter.as_ref()))?; self.validated = true; @@ -68,15 +81,19 @@ impl ImageConverter { } pub fn convert_and_write(&self, buf: Vec, dest: &str) -> color_eyre::Result<()> { - let captured = || if CAPTURE_IMAGE_CONVERTER.load(Relaxed) { - Stdio::piped() - } else { - Stdio::null() + let captured = || { + if CAPTURE_IMAGE_CONVERTER.load(Relaxed) { + Stdio::piped() + } else { + Stdio::null() + } }; let mut com = match &self.converter { - Converter::FFMPEG(_) => {Self::spawn_ffmpeg(self.converter.as_ref(), dest, captured)} - Converter::Imagemagick(_) => {Self::spawn_imagemagick(self.converter.as_ref(), dest, captured)} + Converter::FFMPEG(_) => Self::spawn_ffmpeg(self.converter.as_ref(), dest, captured), + Converter::Imagemagick(_) => { + Self::spawn_imagemagick(self.converter.as_ref(), dest, captured) + }, }?; let mut stdin = com.stdin.take().context("Failed to take stdin")?; @@ -99,31 +116,43 @@ impl ImageConverter { } } - fn spawn_imagemagick(program: &str, dest: &str, captured: fn() -> Stdio) -> color_eyre::Result { + fn spawn_imagemagick( + program: &str, + dest: &str, + captured: fn() -> Stdio, + ) -> color_eyre::Result { let com = Command::new(program) .stdin(Stdio::piped()) .stderr(captured()) .stdout(captured()) .args(&[ - "convert", "-", // - Means piped input - "-quality", "100", // Preserve full input quality + "convert", "-", // - Means piped input + "-quality", "100", // Preserve full input quality "-strip", // Strip unused metadata and profiles - dest // Output to this file + dest, // Output to this file ]) .spawn()?; Ok(com) } - fn spawn_ffmpeg(program: &str, dest: &str, captured: fn() -> Stdio) -> color_eyre::Result { + + fn spawn_ffmpeg( + program: &str, + dest: &str, + captured: fn() -> Stdio, + ) -> color_eyre::Result { let com = Command::new(program) .stdin(Stdio::piped()) .stderr(captured()) .stdout(captured()) .args(&[ - "-hwaccel", "auto", // Optionally enables HWaccel when available - "-i", "-", // Takes stdin as source - "-f", "image2pipe", // Take pipe as source - "-y", // Overwrites any existing file and creates if necessary - dest // Output to this file + "-hwaccel", + "auto", // Optionally enables HWaccel when available + "-i", + "-", // Takes stdin as source + "-f", + "image2pipe", // Take pipe as source + "-y", // Overwrites any existing file and creates if necessary + dest, // Output to this file ]) .spawn()?; Ok(com) @@ -141,4 +170,3 @@ fn program_is_callable(name: &Path) -> color_eyre::Result<()> { .with_context(|| format!("{} exited with a non-zero error code", name.display())) .map(|_| ()) } - diff --git a/src/main.rs b/src/main.rs index cd7c148..77a8186 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,21 +1,23 @@ #![feature(if_let_guard)] -use std::env; -use std::sync::atomic::Ordering::Relaxed; +use std::{env, sync::atomic::Ordering::Relaxed}; use color_eyre::eyre::Result; -use crate::{cli::build_command_structure, subcommands::branch_subcommands}; -use crate::image_conversion::CAPTURE_IMAGE_CONVERTER; +use crate::{ + cli::build_command_structure, + image_conversion::CAPTURE_IMAGE_CONVERTER, + subcommands::branch_subcommands, +}; mod cli; mod error; mod fs_util; +mod image_conversion; mod logging; mod subcommands; mod update_diff; pub(crate) mod util; -mod image_conversion; pub const COMMIT_HASH: &str = env!("GIT_HASH"); pub const GIT_TAG: &str = env!("GIT_TAG"); diff --git a/src/subcommands/unpack_raw_blk.rs b/src/subcommands/unpack_raw_blk.rs index a7cbc75..28e4a4b 100644 --- a/src/subcommands/unpack_raw_blk.rs +++ b/src/subcommands/unpack_raw_blk.rs @@ -62,7 +62,7 @@ pub fn unpack_raw_blk(args: &ArgMatches) -> Result<()> { _ => input.to_owned(), }; - //output_folder.push(input.file_name().unwrap().to_string_lossy().to_string()); + // output_folder.push(input.file_name().unwrap().to_string_lossy().to_string()); match format.as_str() { "Json" => { output_folder.set_extension("json"); diff --git a/src/subcommands/unpack_vromf.rs b/src/subcommands/unpack_vromf.rs index ffd902b..94ad4c3 100644 --- a/src/subcommands/unpack_vromf.rs +++ b/src/subcommands/unpack_vromf.rs @@ -1,10 +1,18 @@ -use std::{env, fs, fs::{File, OpenOptions}, io::{BufWriter, Write}, ops::ControlFlow, path::PathBuf, str::FromStr, sync::Arc, thread, thread::JoinHandle}; -use std::ffi::OsStr; -use std::mem::{swap, take}; -use std::sync::atomic::Ordering::Relaxed; +use std::{ + ffi::OsStr, + fs, + fs::{File, OpenOptions}, + io::{BufWriter, Write}, + mem::take, + ops::ControlFlow, + path::PathBuf, + str::FromStr, + sync::Arc, + thread, + thread::JoinHandle, +}; -use clap::ArgMatches; -use clap::parser::ValueSource; +use clap::{parser::ValueSource, ArgMatches}; use color_eyre::{ eyre::{Context, ContextCompat, Result}, Help, @@ -16,8 +24,13 @@ use wt_blk::{ }; use zip::{write::FileOptions, CompressionMethod}; -use crate::{arced, context, error::CliError, util::CrlfWriter}; -use crate::image_conversion::{CAPTURE_IMAGE_CONVERTER, Converter, ImageConverter}; +use crate::{ + arced, + context, + error::CliError, + image_conversion::{Converter, ImageConverter}, + util::CrlfWriter, +}; pub fn unpack_vromf(args: &ArgMatches) -> Result<()> { info!("Mode: Unpacking vromf"); @@ -61,7 +74,11 @@ pub fn unpack_vromf(args: &ArgMatches) -> Result<()> { let mut ffmpeg = ImageConverter::new_with_converter(Converter::new_from_arg(avif2png)?); let mut avif2png = false; - if args.value_source("avif2png").context("infallible")?.ne(&ValueSource::DefaultValue) { + if args + .value_source("avif2png") + .context("infallible")? + .ne(&ValueSource::DefaultValue) + { ffmpeg.validate()?; avif2png = true; } @@ -207,7 +224,12 @@ fn parse_and_write_one_vromf( if file.0.extension() == Some(&OsStr::new("avif")) { // Convert image joined_final_path.set_extension("png"); - ffmpeg.convert_and_write(take(&mut file.1), joined_final_path.to_str().context("Final path is not a valid str")?)?; + ffmpeg.convert_and_write( + take(&mut file.1), + joined_final_path + .to_str() + .context("Final path is not a valid str")?, + )?; return Ok(CrlfWriter::Null); } } diff --git a/src/util.rs b/src/util.rs index 9056110..df25972 100644 --- a/src/util.rs +++ b/src/util.rs @@ -58,7 +58,7 @@ impl io::Write for CrlfWriter { fn flush(&mut self) -> io::Result<()> { match self { CrlfWriter::Enabled(i) | CrlfWriter::Disabled(i) => i.flush(), - CrlfWriter::Null =>Ok(()), + CrlfWriter::Null => Ok(()), } } }