refactor #24
172
src/meta.rs
172
src/meta.rs
@@ -1,20 +1,5 @@
|
||||
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 +136,100 @@ 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 {
|
||||
use crate::types;
|
||||
use lofty::file::AudioFile;
|
||||
use lofty::tag::Accessor;
|
||||
|
||||
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::o::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::o::get_type(t).unwrap(), pre_value);
|
||||
}
|
||||
types::Type::Artist => {
|
||||
vb.set_artist(pre_value);
|
||||
}
|
||||
types::Type::Date => {
|
||||
vb.insert(types::o::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());
|
||||
}
|
||||
};
|
||||
|
||||
Ok(value.to_owned())
|
||||
}
|
||||
None => Err(std::io::Error::new(
|
||||
Ok(value.to_owned())
|
||||
}
|
||||
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),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -317,6 +312,7 @@ mod tests {
|
||||
mod get {
|
||||
use super::*;
|
||||
use crate::types;
|
||||
use super::metadata::{get_meta, set_meta};
|
||||
|
||||
#[test]
|
||||
fn test_get_title() {
|
||||
@@ -520,6 +516,8 @@ mod tests {
|
||||
|
||||
mod set {
|
||||
use super::*;
|
||||
use crate::types;
|
||||
use super::metadata::{get_meta, set_meta};
|
||||
|
||||
#[test]
|
||||
fn test_set_title() {
|
||||
|
16
src/types.rs
16
src/types.rs
@@ -8,3 +8,19 @@ pub enum Type {
|
||||
Track,
|
||||
Disc,
|
||||
}
|
||||
|
||||
pub mod o {
|
||||
|
||||
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()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user