From fa47ac962212c50ea3a769e1cb9ec4819dce4eb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20W=C3=BCnsche?= Date: Sun, 7 Nov 2021 22:01:36 +0100 Subject: [PATCH] Clippy lints --- src/image.rs | 20 +++++++++----------- src/main.rs | 40 ++++++++++------------------------------ src/metadata.rs | 6 +++--- src/serializer.rs | 25 +++++++++++-------------- src/timebased.rs | 7 ++----- src/util/png.rs | 3 +-- 6 files changed, 36 insertions(+), 65 deletions(-) diff --git a/src/image.rs b/src/image.rs index ff958a5..e95fbef 100644 --- a/src/image.rs +++ b/src/image.rs @@ -33,7 +33,7 @@ pub fn process_img(pt: ImagePoint) -> Result<()> { // Add to Background Structure pt.background.images.push(Static { - duration: 1 as f32, + duration: 1f32, file: format!("{}/{}.png", pt.parent_directory.to_string_lossy(), pt.index), idx: pt.index, }); @@ -62,12 +62,11 @@ pub fn process_img(pt: ImagePoint) -> Result<()> { } pub fn save_xml(xml: &mut Background, parent_directory: &Path, image_name: &str) -> Result<()> { - println!("{}: {}", "Conversion".yellow(), "Done!"); + println!("{}: Done!", "Conversion".yellow()); println!( - "{}: {}", + "{}: Creating xml description for new wallpaper", "Conversion".green(), - "Creating xml description for new wallpaper" ); xml.images.sort_by(|a, b| match (a, b) { ( @@ -78,7 +77,7 @@ pub fn save_xml(xml: &mut Background, parent_directory: &Path, image_name: &str) idx: transition_idx, .. }, - ) => static_idx.cmp(&transition_idx), + ) => static_idx.cmp(transition_idx), ( Static { idx: static_idx, .. @@ -87,7 +86,7 @@ pub fn save_xml(xml: &mut Background, parent_directory: &Path, image_name: &str) idx: transition_idx, .. }, - ) => static_idx.cmp(&transition_idx), + ) => static_idx.cmp(transition_idx), ( Transition { idx: static_idx, .. @@ -96,7 +95,7 @@ pub fn save_xml(xml: &mut Background, parent_directory: &Path, image_name: &str) idx: transition_idx, .. }, - ) => static_idx.cmp(&transition_idx), + ) => static_idx.cmp(transition_idx), ( Transition { idx: static_idx, .. @@ -105,13 +104,12 @@ pub fn save_xml(xml: &mut Background, parent_directory: &Path, image_name: &str) idx: transition_idx, .. }, - ) => static_idx.cmp(&transition_idx), + ) => static_idx.cmp(transition_idx), }); println!( - "{}: {}", + "{}: Writing wallpaper description", "Conversion".green(), - "Writing wallpaper description" ); let result_file = std::fs::OpenOptions::new() .write(true) @@ -124,7 +122,7 @@ pub fn save_xml(xml: &mut Background, parent_directory: &Path, image_name: &str) ))?; let mut result = BufWriter::new(result_file); let mut ser = GnomeXMLBackgroundSerializer::new(&mut result); - ser.serialize(&xml)?; + ser.serialize(xml)?; println!("{}", "Done".green()); Ok(()) } diff --git a/src/main.rs b/src/main.rs index cee1850..3400572 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,7 +37,7 @@ fn main() -> Result<()> { .get_matches(); let path = matches .value_of(INPUT) - .ok_or(anyhow::Error::msg("Could not read INPUT"))?; + .ok_or_else(|| anyhow::Error::msg("Could not read INPUT"))?; let parent_directory; if matches.is_present(DIR) { @@ -47,35 +47,18 @@ fn main() -> Result<()> { } parent_directory = nu_path.canonicalize()?; } else { - let p = std::path::Path::new(path) + parent_directory = std::path::Path::new(path) .ancestors() - .nth(1); - - if p.is_none() { - return Err(anyhow::Error::msg("Cannot get parent of given image path.")); - } - let pd = p.unwrap() - .canonicalize(); - - if let Err(e) = pd { - let msg = format!("Cannot get absolute path: {}", e); - return Err(anyhow::Error::msg(msg)); - } - parent_directory = pd.unwrap(); - } - let image_ctx_opt = HeifContext::read_from_file(path); - - if let Err(e) = image_ctx_opt { - let msg = format!("{}", e.message); - return Err(anyhow::Error::msg(msg)) + .nth(1) + .ok_or_else(|| anyhow::Error::msg("Cannot get parent of given image path."))? + .canonicalize()?; } - let image_ctx = image_ctx_opt.unwrap(); + let image_ctx = HeifContext::read_from_file(path)?; // FETCH file wide metadata println!( - "{}: {}", + "{}: Fetch metadata from image", "Preparation".bright_blue(), - "Fetch metadata from image" ); let base64plist = metadata::get_wallpaper_metadata(&image_ctx); @@ -89,16 +72,14 @@ fn main() -> Result<()> { .to_string_lossy(); println!( - "{}: {}", + "{}: Detecting wallpaper description kind", "Preparation".bright_blue(), - "Detecting wallpaper description kind" ); match base64plist.unwrap() { metadata::WallPaperMode::H24(content) => { println!( - "{}: {}", + "{}: Detected time-based wallpaper", "Preparation".bright_blue(), - "Detected time-based wallpaper" ); timebased::compute_time_based_wallpaper( image_ctx, @@ -109,9 +90,8 @@ fn main() -> Result<()> { } metadata::WallPaperMode::Solar(content) => { println!( - "{}: {}", + "{}: Detected solar-based wallpaper", "Preparation".bright_blue(), - "Detected solar-based wallpaper" ); solar::compute_solar_based_wallpaper(image_ctx, content, &parent_directory, &image_name) } diff --git a/src/metadata.rs b/src/metadata.rs index 001316c..c8d9e7c 100644 --- a/src/metadata.rs +++ b/src/metadata.rs @@ -20,12 +20,12 @@ pub fn get_wallpaper_metadata(image_ctx: &HeifContext) -> Option .get(0) .expect("Could not get metadata information"); let base64plist = { - let foo = image_ctx + let tmp = image_ctx .primary_image_handle() .unwrap() .metadata(*metadata_id) .unwrap(); - let content = String::from_utf8_lossy(&foo); + let content = String::from_utf8_lossy(&tmp); //println!("{:?}", content); let mut reader = Reader::from_str(&content); reader.trim_text(true); @@ -65,7 +65,7 @@ pub fn get_wallpaper_metadata(image_ctx: &HeifContext) -> Option } h24 }; - return base64plist; + base64plist } pub fn get_time_plist_from_base64(input: &str) -> Result { diff --git a/src/serializer.rs b/src/serializer.rs index 9bb8933..0db1c8a 100644 --- a/src/serializer.rs +++ b/src/serializer.rs @@ -16,26 +16,23 @@ where pub fn serialize(&mut self, background: &Background) -> Result<()> { // By definition we can only find one starttime - match background.starttime { - StartTime { + let StartTime { year, month, day, hour, minute, second, - } => { - writeln!(self.writer, "")?; - writeln!(self.writer, "\t")?; - writeln!(self.writer, "\t\t{}", year)?; - writeln!(self.writer, "\t\t{}", month)?; - writeln!(self.writer, "\t\t{}", day)?; - writeln!(self.writer, "\t\t{}", hour)?; - writeln!(self.writer, "\t\t{}", minute)?; - writeln!(self.writer, "\t\t{}", second)?; - writeln!(self.writer, "\t")?; - } - } + } = background.starttime; + writeln!(self.writer, "")?; + writeln!(self.writer, "\t")?; + writeln!(self.writer, "\t\t{}", year)?; + writeln!(self.writer, "\t\t{}", month)?; + writeln!(self.writer, "\t\t{}", day)?; + writeln!(self.writer, "\t\t{}", hour)?; + writeln!(self.writer, "\t\t{}", minute)?; + writeln!(self.writer, "\t\t{}", second)?; + writeln!(self.writer, "\t")?; for entry in background.images.iter() { match entry { diff --git a/src/timebased.rs b/src/timebased.rs index 6b8ca1f..950d231 100644 --- a/src/timebased.rs +++ b/src/timebased.rs @@ -42,18 +42,15 @@ pub fn compute_time_based_wallpaper( let number_of_images = image_ctx.number_of_top_level_images(); println!( - "{}: {} {} {}", + "{}: Found {} images", "Preparation".bright_blue(), - "Found", number_of_images, - "images" ); let mut image_ids = vec![0u32; number_of_images]; image_ctx.top_level_image_ids(&mut image_ids); println!( - "{}: {}", + "{}: Converting embedded images to png format", "Conversion".yellow(), - "Converting embedded images to png format" ); let pb = ProgressBar::new(number_of_images as u64).with_style( ProgressStyle::default_bar() diff --git a/src/util/png.rs b/src/util/png.rs index 6e55842..4deedc6 100644 --- a/src/util/png.rs +++ b/src/util/png.rs @@ -50,9 +50,8 @@ pub fn write_png(path: &str, handle: ImageHandle) -> Result<()> { return Ok(()); } println!( - "{}: {}", + "{}: Could not determine color space. Colorspace RGB C444 could not be applied", "Error".red(), - "Could not determine color space. Colorspace RGB C444 could not be applied" ); Err(anyhow::Error::msg("Colorspace invalid")) }