From 03cb9fe656f91281098634c300e6b383a8cff0db Mon Sep 17 00:00:00 2001 From: Andrew Radev Date: Sat, 16 Feb 2019 10:52:45 +0200 Subject: [PATCH] Extract logic into lib.rs --- src/bin/id3-image-embed.rs | 33 ++++----------------------- src/bin/id3-image-extract.rs | 28 +++++------------------ src/lib.rs | 44 ++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 51 deletions(-) create mode 100644 src/lib.rs diff --git a/src/bin/id3-image-embed.rs b/src/bin/id3-image-embed.rs index cbeff29..aefdab2 100644 --- a/src/bin/id3-image-embed.rs +++ b/src/bin/id3-image-embed.rs @@ -1,6 +1,8 @@ use std::env; use std::process; +use id3_image::embed_image; + fn main() { let args: Vec<_> = env::args().collect(); @@ -12,35 +14,8 @@ fn main() { let music_filename = args[1].clone(); let image_filename = args[2].clone(); - let mut tag = match id3::Tag::read_from_path(&music_filename) { - Ok(t) => t, - Err(e) => { - eprintln!("Error reading music file {}: {}", music_filename, e); - process::exit(1); - }, - }; - - let image = match image::open(&image_filename) { - Ok(i) => i, - Err(e) => { - eprintln!("Error reading image {}: {}", image_filename, e); - process::exit(1); - }, - }; - - let mut encoded_image_bytes = Vec::new(); - // Unwrap: Writing to a Vec should always succeed; - image.write_to(&mut encoded_image_bytes, image::ImageOutputFormat::JPEG(90)).unwrap(); - - tag.add_picture(id3::frame::Picture { - mime_type: "image/jpeg".to_string(), - picture_type: id3::frame::PictureType::Other, - description: String::new(), - data: encoded_image_bytes, - }); - - if let Err(e) = tag.write_to_path(music_filename, id3::Version::Id3v23) { - eprintln!("Error writing image to file: {}", e); + if let Err(e) = embed_image(&music_filename, &image_filename) { + eprintln!("{}", e); process::exit(1); } } diff --git a/src/bin/id3-image-extract.rs b/src/bin/id3-image-extract.rs index 6cab190..1a14fbf 100644 --- a/src/bin/id3-image-extract.rs +++ b/src/bin/id3-image-extract.rs @@ -1,6 +1,8 @@ use std::env; use std::process; -use std::path::{Path, PathBuf}; +use std::path::PathBuf; + +use id3_image::extract_image; fn main() { let args: Vec<_> = env::args().collect(); @@ -13,27 +15,9 @@ fn main() { let music_filename = args[1].clone(); let image_filename = args.get(2).cloned().unwrap_or_else(|| replace_extension(&music_filename, "jpg")); - let tag = match id3::Tag::read_from_path(&music_filename) { - Ok(t) => t, - Err(e) => { - eprintln!("Error reading music file {}: {}", music_filename, e); - process::exit(1); - }, - }; - - let first_picture = tag.pictures().next(); - - if let Some(p) = first_picture { - match image::load_from_memory(&p.data) { - Ok(image) => { - image.save(&image_filename); - println!("{}", image_filename); - }, - Err(e) => { - eprintln!("Couldn't load image: {}", e); - process::exit(1); - } - }; + if let Err(e) = extract_image(&music_filename, &image_filename) { + eprintln!("{}", e); + process::exit(1); } } diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..0d09fa3 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,44 @@ +use std::error::Error; + +pub fn embed_image(music_filename: &str, image_filename: &str) -> Result<(), Box> { + let mut tag = id3::Tag::read_from_path(&music_filename). + map_err(|e| format!("Error reading music file {}: {}", music_filename, e))?; + + let image = image::open(&image_filename). + map_err(|e| format!("Error reading image {}: {}", image_filename, e))?; + + let mut encoded_image_bytes = Vec::new(); + // Unwrap: Writing to a Vec should always succeed; + image.write_to(&mut encoded_image_bytes, image::ImageOutputFormat::JPEG(90)).unwrap(); + + tag.add_picture(id3::frame::Picture { + mime_type: "image/jpeg".to_string(), + picture_type: id3::frame::PictureType::Other, + description: String::new(), + data: encoded_image_bytes, + }); + + tag.write_to_path(music_filename, id3::Version::Id3v23). + map_err(|e| format!("Error writing image to file: {}", e))?; + + Ok(()) +} + +pub fn extract_image(music_filename: &str, image_filename: &str) -> Result<(), Box> { + let tag = id3::Tag::read_from_path(&music_filename). + map_err(|e| format!("Error reading music file {}: {}", music_filename, e))?; + + let first_picture = tag.pictures().next(); + + if let Some(p) = first_picture { + match image::load_from_memory(&p.data) { + Ok(image) => { + image.save(&image_filename); + println!("{}", image_filename); + }, + Err(e) => return Err(format!("Couldn't load image: {}", e).into()), + }; + } + + Ok(()) +}