Compare commits
4 Commits
v0.1.11-de
...
v0.1.30-de
Author | SHA1 | Date | |
---|---|---|---|
859003fc65 | |||
000639b0f8 | |||
2932cdbe1c | |||
f534687292 |
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "icarus_meta"
|
||||
version = "0.1.11"
|
||||
version = "0.1.30"
|
||||
edition = "2024"
|
||||
rust-version = "1.86"
|
||||
|
||||
|
354
src/meta.rs
354
src/meta.rs
@@ -1,20 +1,3 @@
|
||||
use lofty::{file::AudioFile, tag::Accessor};
|
||||
|
||||
use crate::types;
|
||||
|
||||
fn get_type(t: types::Type) -> Result<String, std::io::Error> {
|
||||
match t {
|
||||
types::Type::Title => Ok("TITLE".to_owned()),
|
||||
types::Type::Artist => Ok("ARTIST".to_owned()),
|
||||
types::Type::Album => Ok("ALBUM".to_owned()),
|
||||
types::Type::AlbumArtist => Ok("ALBUMARTIST".to_owned()),
|
||||
types::Type::Genre => Ok("GENRE".to_owned()),
|
||||
types::Type::Date => Ok("DATE".to_owned()),
|
||||
types::Type::Track => Ok("TRACKNUMBER".to_owned()),
|
||||
types::Type::Disc => Ok("DISCNUMBER".to_owned()),
|
||||
}
|
||||
}
|
||||
|
||||
pub mod coverart {
|
||||
|
||||
use lofty::{file::AudioFile, ogg::OggPictureStorage};
|
||||
@@ -151,90 +134,115 @@ pub mod coverart {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_meta(t: types::Type, filepath: &String) -> Result<String, std::io::Error> {
|
||||
match std::fs::File::open(filepath) {
|
||||
Ok(mut content) => {
|
||||
match lofty::flac::FlacFile::read_from(&mut content, lofty::config::ParseOptions::new())
|
||||
{
|
||||
Ok(flac_file) => match flac_file.vorbis_comments() {
|
||||
Some(vb) => {
|
||||
let type_str: String = get_type(t).unwrap();
|
||||
match vb.get(&type_str) {
|
||||
Some(val) => Ok(val.to_owned()),
|
||||
None => Err(std::io::Error::new(
|
||||
std::io::ErrorKind::InvalidData,
|
||||
"Could not get tag data",
|
||||
)),
|
||||
pub mod metadata {
|
||||
// TODO: Move this at the end after the non-std crates
|
||||
use crate::types;
|
||||
use lofty::file::AudioFile;
|
||||
use lofty::tag::Accessor;
|
||||
use lofty::tag::TagExt;
|
||||
|
||||
pub fn get_meta(t: types::Type, filepath: &String) -> Result<String, std::io::Error> {
|
||||
match std::fs::File::open(filepath) {
|
||||
Ok(mut content) => {
|
||||
match lofty::flac::FlacFile::read_from(
|
||||
&mut content,
|
||||
lofty::config::ParseOptions::new(),
|
||||
) {
|
||||
Ok(flac_file) => match flac_file.vorbis_comments() {
|
||||
Some(vb) => {
|
||||
let type_str: String = types::access::get_type(t).unwrap();
|
||||
match vb.get(&type_str) {
|
||||
Some(val) => Ok(val.to_owned()),
|
||||
None => Err(std::io::Error::new(
|
||||
std::io::ErrorKind::InvalidData,
|
||||
"Could not get tag data",
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
None => Err(std::io::Error::new(
|
||||
None => Err(std::io::Error::new(
|
||||
std::io::ErrorKind::InvalidData,
|
||||
"No tags found",
|
||||
)),
|
||||
},
|
||||
Err(err) => Err(std::io::Error::new(
|
||||
std::io::ErrorKind::InvalidData,
|
||||
"No tags found",
|
||||
err.to_string(),
|
||||
)),
|
||||
},
|
||||
Err(err) => Err(std::io::Error::new(
|
||||
std::io::ErrorKind::InvalidData,
|
||||
err.to_string(),
|
||||
)),
|
||||
}
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_meta(
|
||||
t: types::Type,
|
||||
filepath: &String,
|
||||
value: &String,
|
||||
) -> Result<String, std::io::Error> {
|
||||
match std::fs::File::open(filepath) {
|
||||
Ok(mut content) => {
|
||||
match lofty::flac::FlacFile::read_from(&mut content, lofty::config::ParseOptions::new())
|
||||
{
|
||||
Ok(mut flac_file) => match flac_file.vorbis_comments_mut() {
|
||||
Some(vb) => {
|
||||
let pre_value = value.clone();
|
||||
match t {
|
||||
types::Type::Album => {
|
||||
vb.set_album(pre_value);
|
||||
}
|
||||
types::Type::AlbumArtist => {
|
||||
vb.insert(get_type(t).unwrap(), pre_value);
|
||||
}
|
||||
types::Type::Artist => {
|
||||
vb.set_artist(pre_value);
|
||||
}
|
||||
types::Type::Date => {
|
||||
vb.insert(get_type(t).unwrap(), pre_value);
|
||||
}
|
||||
types::Type::Disc => {
|
||||
vb.set_disk(pre_value.parse().unwrap());
|
||||
}
|
||||
types::Type::Genre => {
|
||||
vb.set_genre(pre_value);
|
||||
}
|
||||
types::Type::Title => {
|
||||
vb.set_title(pre_value);
|
||||
}
|
||||
types::Type::Track => {
|
||||
vb.set_track(pre_value.parse().unwrap());
|
||||
}
|
||||
};
|
||||
pub fn set_meta(
|
||||
t: types::Type,
|
||||
filepath: &String,
|
||||
value: &String,
|
||||
) -> Result<String, std::io::Error> {
|
||||
match std::fs::File::open(filepath) {
|
||||
Ok(mut content) => {
|
||||
match lofty::flac::FlacFile::read_from(
|
||||
&mut content,
|
||||
lofty::config::ParseOptions::new(),
|
||||
) {
|
||||
Ok(mut flac_file) => match flac_file.vorbis_comments_mut() {
|
||||
Some(vb) => {
|
||||
let pre_value = value.clone();
|
||||
match t {
|
||||
types::Type::Album => {
|
||||
vb.set_album(pre_value);
|
||||
}
|
||||
types::Type::AlbumArtist => {
|
||||
vb.insert(types::access::get_type(t).unwrap(), pre_value);
|
||||
}
|
||||
types::Type::Artist => {
|
||||
vb.set_artist(pre_value);
|
||||
}
|
||||
types::Type::Date => {
|
||||
vb.insert(types::access::get_type(t).unwrap(), pre_value);
|
||||
}
|
||||
types::Type::Disc => {
|
||||
vb.set_disk(pre_value.parse().unwrap());
|
||||
}
|
||||
types::Type::Genre => {
|
||||
vb.set_genre(pre_value);
|
||||
}
|
||||
types::Type::Title => {
|
||||
vb.set_title(pre_value);
|
||||
}
|
||||
types::Type::Track => {
|
||||
vb.set_track(pre_value.parse().unwrap());
|
||||
}
|
||||
types::Type::TrackCount => {
|
||||
vb.set_track_total(pre_value.parse().unwrap());
|
||||
}
|
||||
types::Type::DiscCount => {
|
||||
vb.set_disk_total(pre_value.parse().unwrap());
|
||||
}
|
||||
};
|
||||
|
||||
Ok(value.to_owned())
|
||||
}
|
||||
None => Err(std::io::Error::new(
|
||||
match vb.save_to_path(filepath, lofty::config::WriteOptions::default())
|
||||
{
|
||||
Ok(_) => Ok(value.to_owned()),
|
||||
Err(err) => Err(std::io::Error::new(
|
||||
std::io::ErrorKind::InvalidData,
|
||||
err.to_string(),
|
||||
)),
|
||||
}
|
||||
}
|
||||
None => Err(std::io::Error::new(
|
||||
std::io::ErrorKind::InvalidData,
|
||||
"No tags found",
|
||||
)),
|
||||
},
|
||||
Err(err) => Err(std::io::Error::new(
|
||||
std::io::ErrorKind::InvalidData,
|
||||
"No tags found",
|
||||
err.to_string(),
|
||||
)),
|
||||
},
|
||||
Err(err) => Err(std::io::Error::new(
|
||||
std::io::ErrorKind::InvalidData,
|
||||
err.to_string(),
|
||||
)),
|
||||
}
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -315,6 +323,7 @@ mod tests {
|
||||
}
|
||||
|
||||
mod get {
|
||||
use super::metadata::get_meta;
|
||||
use super::*;
|
||||
use crate::types;
|
||||
|
||||
@@ -516,10 +525,62 @@ mod tests {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_track_total() {
|
||||
let filename = util::get_filename(1);
|
||||
let dir = String::from(util::TESTFILEDIRECTORY);
|
||||
|
||||
match file_exists(&dir, &filename) {
|
||||
Ok(_) => {
|
||||
let filepath = get_full_path(&dir, &filename).unwrap();
|
||||
|
||||
match get_meta(types::Type::TrackCount, &filepath) {
|
||||
Ok(track_total) => {
|
||||
let found = track_total == "3";
|
||||
assert!(found, "Meta information was not found {:?}", track_total);
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {:?}", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: File does not exist {:?}", err.to_string());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_disc_total() {
|
||||
let filename = util::get_filename(1);
|
||||
let dir = String::from(util::TESTFILEDIRECTORY);
|
||||
|
||||
match file_exists(&dir, &filename) {
|
||||
Ok(_) => {
|
||||
let filepath = get_full_path(&dir, &filename).unwrap();
|
||||
|
||||
match get_meta(types::Type::DiscCount, &filepath) {
|
||||
Ok(disc_total) => {
|
||||
let found = disc_total == "1";
|
||||
assert!(found, "Meta information was not found {:?}", disc_total);
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {:?}", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: File does not exist {:?}", err.to_string());
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
mod set {
|
||||
use super::metadata::{get_meta, set_meta};
|
||||
use super::*;
|
||||
use crate::types;
|
||||
|
||||
#[test]
|
||||
fn test_set_title() {
|
||||
@@ -883,6 +944,121 @@ mod tests {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_set_track_total() {
|
||||
let filename = util::get_filename(1);
|
||||
let dir = String::from(util::TESTFILEDIRECTORY);
|
||||
|
||||
let temp_file = tempfile::tempdir().expect("Could not create test directory");
|
||||
let test_dir = String::from(temp_file.path().to_str().unwrap());
|
||||
let test_filename = String::from("track08.flac");
|
||||
let new_filepath = test_dir + "/" + &test_filename;
|
||||
|
||||
match file_exists(&dir, &filename) {
|
||||
Ok(_) => {
|
||||
let filepath = get_full_path(&dir, &filename).unwrap();
|
||||
|
||||
match util::copy_file(&filepath, &new_filepath) {
|
||||
Ok(_o) => match get_meta(types::Type::TrackCount, &filepath) {
|
||||
Ok(track_total) => {
|
||||
let found = track_total == "3";
|
||||
assert!(found, "Meta information was not found {:?}", track_total);
|
||||
let new_track_total = String::from("5");
|
||||
|
||||
match set_meta(
|
||||
types::Type::TrackCount,
|
||||
&new_filepath,
|
||||
&new_track_total,
|
||||
) {
|
||||
Ok(m) => {
|
||||
assert_eq!(
|
||||
new_track_total, m,
|
||||
"New track does not match {:?}",
|
||||
m
|
||||
);
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {:?}", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {:?}", err);
|
||||
}
|
||||
},
|
||||
Err(err) => {
|
||||
assert!(
|
||||
false,
|
||||
"Error: {:?} source {:?} destination {:?}",
|
||||
err, filepath, new_filepath
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: File does not exist {:?}", err.to_string());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_set_disc_total() {
|
||||
let filename = util::get_filename(1);
|
||||
let dir = String::from(util::TESTFILEDIRECTORY);
|
||||
|
||||
let temp_file = tempfile::tempdir().expect("Could not create test directory");
|
||||
let test_dir = String::from(temp_file.path().to_str().unwrap());
|
||||
let test_filename = String::from("track08.flac");
|
||||
let new_filepath = test_dir + "/" + &test_filename;
|
||||
|
||||
match file_exists(&dir, &filename) {
|
||||
Ok(_) => {
|
||||
let filepath = get_full_path(&dir, &filename).unwrap();
|
||||
|
||||
match util::copy_file(&filepath, &new_filepath) {
|
||||
Ok(_o) => match get_meta(types::Type::DiscCount, &filepath) {
|
||||
Ok(disc_total) => {
|
||||
let found = disc_total == "1";
|
||||
assert!(found, "Meta information was not found {:?}", disc_total);
|
||||
let new_disc_total = String::from("2");
|
||||
|
||||
match set_meta(
|
||||
types::Type::DiscCount,
|
||||
&new_filepath,
|
||||
&new_disc_total,
|
||||
) {
|
||||
Ok(m) => {
|
||||
assert_eq!(
|
||||
new_disc_total, m,
|
||||
"New disc_total does not match {:?}",
|
||||
m
|
||||
);
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {:?}", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {:?}", err);
|
||||
}
|
||||
},
|
||||
Err(err) => {
|
||||
assert!(
|
||||
false,
|
||||
"Error: {:?} source {:?} destination {:?}",
|
||||
err, filepath, new_filepath
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: File does not exist {:?}", err.to_string());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_set_genre() {
|
||||
let filename = util::get_filename(1);
|
||||
|
20
src/types.rs
20
src/types.rs
@@ -7,4 +7,24 @@ pub enum Type {
|
||||
Date,
|
||||
Track,
|
||||
Disc,
|
||||
TrackCount,
|
||||
DiscCount,
|
||||
}
|
||||
|
||||
pub mod access {
|
||||
|
||||
pub fn get_type(t: super::Type) -> Result<String, std::io::Error> {
|
||||
match t {
|
||||
super::Type::Title => Ok("TITLE".to_owned()),
|
||||
super::Type::Artist => Ok("ARTIST".to_owned()),
|
||||
super::Type::Album => Ok("ALBUM".to_owned()),
|
||||
super::Type::AlbumArtist => Ok("ALBUMARTIST".to_owned()),
|
||||
super::Type::Genre => Ok("GENRE".to_owned()),
|
||||
super::Type::Date => Ok("DATE".to_owned()),
|
||||
super::Type::Track => Ok("TRACKNUMBER".to_owned()),
|
||||
super::Type::Disc => Ok("DISCNUMBER".to_owned()),
|
||||
super::Type::TrackCount => Ok("TRACKCOUNT".to_owned()),
|
||||
super::Type::DiscCount => Ok("DISCCOUNT".to_owned()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user