Compare commits
3 Commits
v0.1.20-de
...
v0.1.30-de
Author | SHA1 | Date | |
---|---|---|---|
8f55b28fbe | |||
859003fc65 | |||
000639b0f8 |
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "icarus_meta"
|
name = "icarus_meta"
|
||||||
version = "0.1.20"
|
version = "0.1.30"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
rust-version = "1.86"
|
rust-version = "1.86"
|
||||||
|
|
||||||
|
199
src/meta.rs
199
src/meta.rs
@@ -135,9 +135,11 @@ pub mod coverart {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub mod metadata {
|
pub mod metadata {
|
||||||
|
// TODO: Move this at the end after the non-std crates
|
||||||
use crate::types;
|
use crate::types;
|
||||||
use lofty::file::AudioFile;
|
use lofty::file::AudioFile;
|
||||||
use lofty::tag::Accessor;
|
use lofty::tag::Accessor;
|
||||||
|
use lofty::tag::TagExt;
|
||||||
|
|
||||||
pub fn get_meta(t: types::Type, filepath: &String) -> Result<String, std::io::Error> {
|
pub fn get_meta(t: types::Type, filepath: &String) -> Result<String, std::io::Error> {
|
||||||
match std::fs::File::open(filepath) {
|
match std::fs::File::open(filepath) {
|
||||||
@@ -211,9 +213,22 @@ pub mod metadata {
|
|||||||
types::Type::Track => {
|
types::Type::Track => {
|
||||||
vb.set_track(pre_value.parse().unwrap());
|
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())
|
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(
|
None => Err(std::io::Error::new(
|
||||||
std::io::ErrorKind::InvalidData,
|
std::io::ErrorKind::InvalidData,
|
||||||
@@ -229,6 +244,23 @@ pub mod metadata {
|
|||||||
Err(err) => Err(err),
|
Err(err) => Err(err),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn parse_value(value: types::MetadataType) -> String {
|
||||||
|
match value {
|
||||||
|
types::MetadataType::String(val) => val,
|
||||||
|
types::MetadataType::Int(val) => val.to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_meta_value(
|
||||||
|
t: types::Type,
|
||||||
|
filepath: &String,
|
||||||
|
value: types::MetadataType,
|
||||||
|
) -> Result<String, std::io::Error> {
|
||||||
|
let parsed_val = parse_value(value);
|
||||||
|
|
||||||
|
set_meta(t, filepath, &parsed_val)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@@ -510,6 +542,56 @@ 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 {
|
mod set {
|
||||||
@@ -879,6 +961,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]
|
#[test]
|
||||||
fn test_set_genre() {
|
fn test_set_genre() {
|
||||||
let filename = util::get_filename(1);
|
let filename = util::get_filename(1);
|
||||||
|
26
src/types.rs
26
src/types.rs
@@ -1,3 +1,4 @@
|
|||||||
|
// TODO: Have this derive Debug
|
||||||
pub enum Type {
|
pub enum Type {
|
||||||
Title,
|
Title,
|
||||||
Artist,
|
Artist,
|
||||||
@@ -7,10 +8,31 @@ pub enum Type {
|
|||||||
Date,
|
Date,
|
||||||
Track,
|
Track,
|
||||||
Disc,
|
Disc,
|
||||||
|
TrackCount,
|
||||||
|
DiscCount,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum MetadataType {
|
||||||
|
String(String),
|
||||||
|
Int(i32),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MetadataType {
|
||||||
|
pub fn from_std_str(s: &str) -> Self {
|
||||||
|
MetadataType::String(s.to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_string(s: String) -> Self {
|
||||||
|
MetadataType::String(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_int(i: i32) -> Self {
|
||||||
|
MetadataType::Int(i)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod access {
|
pub mod access {
|
||||||
|
|
||||||
pub fn get_type(t: super::Type) -> Result<String, std::io::Error> {
|
pub fn get_type(t: super::Type) -> Result<String, std::io::Error> {
|
||||||
match t {
|
match t {
|
||||||
super::Type::Title => Ok("TITLE".to_owned()),
|
super::Type::Title => Ok("TITLE".to_owned()),
|
||||||
@@ -21,6 +43,8 @@ pub mod access {
|
|||||||
super::Type::Date => Ok("DATE".to_owned()),
|
super::Type::Date => Ok("DATE".to_owned()),
|
||||||
super::Type::Track => Ok("TRACKNUMBER".to_owned()),
|
super::Type::Track => Ok("TRACKNUMBER".to_owned()),
|
||||||
super::Type::Disc => Ok("DISCNUMBER".to_owned()),
|
super::Type::Disc => Ok("DISCNUMBER".to_owned()),
|
||||||
|
super::Type::TrackCount => Ok("TRACKCOUNT".to_owned()),
|
||||||
|
super::Type::DiscCount => Ok("DISCCOUNT".to_owned()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user