Compare commits
8 Commits
f4b71de969
...
c14d3e5a51
Author | SHA1 | Date | |
---|---|---|---|
c14d3e5a51
|
|||
8e2eb8a1b1
|
|||
d652d8588d
|
|||
08b441a2ee
|
|||
bde67b5014
|
|||
ccf6ee08b4
|
|||
b3bd009460
|
|||
cea522d6b8
|
66
src/lib.rs
66
src/lib.rs
@@ -1,2 +1,68 @@
|
|||||||
pub mod meta;
|
pub mod meta;
|
||||||
|
pub mod properties;
|
||||||
pub mod types;
|
pub mod types;
|
||||||
|
|
||||||
|
pub mod test_util {
|
||||||
|
pub mod util {
|
||||||
|
use std::io::{self, Write};
|
||||||
|
|
||||||
|
// Function to save a Vec<u8> to a file
|
||||||
|
pub fn save_bytes_to_file(bytes: &[u8], file_path: &String) -> io::Result<()> {
|
||||||
|
let file = std::path::Path::new(file_path);
|
||||||
|
let mut file = std::fs::File::create(file)?;
|
||||||
|
|
||||||
|
match file.write_all(bytes) {
|
||||||
|
Ok(_res) => Ok(()),
|
||||||
|
Err(err) => Err(err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn get_full_path(
|
||||||
|
directory: &String,
|
||||||
|
filename: &String,
|
||||||
|
) -> Result<String, std::io::Error> {
|
||||||
|
match path_buf(directory, filename) {
|
||||||
|
Ok(pf) => Ok(pf.display().to_string()),
|
||||||
|
Err(err) => Err(err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn copy_file(
|
||||||
|
source_path: &String,
|
||||||
|
destination_path: &String,
|
||||||
|
) -> Result<u64, std::io::Error> {
|
||||||
|
let src_path = std::path::Path::new(source_path);
|
||||||
|
let dest_path = std::path::Path::new(destination_path);
|
||||||
|
|
||||||
|
std::fs::copy(src_path, dest_path)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn file_exists(directory: &String, filename: &String) -> Result<bool, std::io::Error> {
|
||||||
|
match path_buf(directory, filename) {
|
||||||
|
Ok(pf) => Ok(pf.exists()),
|
||||||
|
Err(err) => Err(err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn path_buf(
|
||||||
|
directory: &String,
|
||||||
|
filename: &String,
|
||||||
|
) -> Result<std::path::PathBuf, std::io::Error> {
|
||||||
|
let dir_path = std::path::Path::new(&directory);
|
||||||
|
Ok(dir_path.join(filename))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const TESTFILEDIRECTORY: &str = "tests/sample_tracks3";
|
||||||
|
|
||||||
|
pub fn get_filename(track: i32) -> String {
|
||||||
|
const FLAC_EXTENSION: &str = ".flac";
|
||||||
|
|
||||||
|
let filename = if track < 10 {
|
||||||
|
format!("track0{}{}", track, FLAC_EXTENSION)
|
||||||
|
} else {
|
||||||
|
format!("track{}{}", track, FLAC_EXTENSION)
|
||||||
|
};
|
||||||
|
|
||||||
|
filename
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
87
src/meta.rs
87
src/meta.rs
@@ -275,83 +275,13 @@ pub mod metadata {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use util::{file_exists, get_full_path};
|
|
||||||
|
|
||||||
use super::*;
|
// use super::*;
|
||||||
|
|
||||||
mod util {
|
|
||||||
|
|
||||||
use std::io::{self, Write};
|
|
||||||
|
|
||||||
// Function to save a Vec<u8> to a file
|
|
||||||
pub fn save_bytes_to_file(bytes: &[u8], file_path: &String) -> io::Result<()> {
|
|
||||||
let file = std::path::Path::new(file_path);
|
|
||||||
let mut file = std::fs::File::create(file)?;
|
|
||||||
|
|
||||||
match file.write_all(bytes) {
|
|
||||||
Ok(_res) => Ok(()),
|
|
||||||
Err(err) => Err(err),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn get_full_path(
|
|
||||||
directory: &String,
|
|
||||||
filename: &String,
|
|
||||||
) -> Result<String, std::io::Error> {
|
|
||||||
match path_buf(directory, filename) {
|
|
||||||
Ok(pf) => Ok(pf.display().to_string()),
|
|
||||||
Err(err) => Err(err),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn copy_file(
|
|
||||||
source_path: &String,
|
|
||||||
destination_path: &String,
|
|
||||||
) -> Result<u64, std::io::Error> {
|
|
||||||
let src_path = std::path::Path::new(source_path);
|
|
||||||
let dest_path = std::path::Path::new(destination_path);
|
|
||||||
|
|
||||||
match std::fs::copy(src_path, dest_path) {
|
|
||||||
Ok(bytes) => Ok(bytes),
|
|
||||||
Err(err) => Err(err),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn file_exists(directory: &String, filename: &String) -> Result<bool, std::io::Error> {
|
|
||||||
match path_buf(directory, filename) {
|
|
||||||
Ok(pf) => Ok(pf.exists()),
|
|
||||||
Err(err) => Err(err),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn path_buf(
|
|
||||||
directory: &String,
|
|
||||||
filename: &String,
|
|
||||||
) -> Result<std::path::PathBuf, std::io::Error> {
|
|
||||||
let dir_path = std::path::Path::new(&directory);
|
|
||||||
Ok(dir_path.join(filename))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub const TESTFILEDIRECTORY: &str = "tests/sample_tracks3";
|
|
||||||
|
|
||||||
pub fn get_filename(track: i32) -> String {
|
|
||||||
let mut filename = String::from("track");
|
|
||||||
|
|
||||||
if track < 10 {
|
|
||||||
filename += "0";
|
|
||||||
filename += &track.to_string();
|
|
||||||
} else {
|
|
||||||
filename += &track.to_string();
|
|
||||||
}
|
|
||||||
|
|
||||||
filename += ".flac";
|
|
||||||
|
|
||||||
filename
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
mod get {
|
mod get {
|
||||||
use super::metadata::get_meta;
|
use super::super::metadata::get_meta;
|
||||||
use super::*;
|
use crate::test_util::util;
|
||||||
|
use crate::test_util::util::{file_exists, get_full_path};
|
||||||
use crate::types;
|
use crate::types;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -605,8 +535,9 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
mod set {
|
mod set {
|
||||||
use super::metadata::{get_meta, set_meta};
|
use super::super::metadata::{get_meta, set_meta};
|
||||||
use super::*;
|
use crate::test_util::util;
|
||||||
|
use crate::test_util::util::{file_exists, get_full_path};
|
||||||
use crate::types;
|
use crate::types;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -1142,7 +1073,9 @@ mod tests {
|
|||||||
|
|
||||||
mod pictures {
|
mod pictures {
|
||||||
|
|
||||||
use super::*;
|
use super::super::*;
|
||||||
|
use crate::test_util::util;
|
||||||
|
use crate::test_util::util::{file_exists, get_full_path};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_get_picture() {
|
fn test_get_picture() {
|
||||||
|
56
src/properties.rs
Normal file
56
src/properties.rs
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
use lofty::file::AudioFile;
|
||||||
|
|
||||||
|
#[derive(Debug, Default, Clone)]
|
||||||
|
pub struct Duration {
|
||||||
|
pub val: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_duration(song_path: &String) -> Result<std::time::Duration, std::io::Error> {
|
||||||
|
match std::fs::File::open(song_path) {
|
||||||
|
Ok(mut content) => {
|
||||||
|
match lofty::flac::FlacFile::read_from(&mut content, lofty::config::ParseOptions::new())
|
||||||
|
{
|
||||||
|
Ok(flac_file) => {
|
||||||
|
let properties = flac_file.properties();
|
||||||
|
Ok(properties.duration())
|
||||||
|
}
|
||||||
|
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => Err(err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::test_util;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_get_duration() {
|
||||||
|
let filename = test_util::util::get_filename(1);
|
||||||
|
let dir = String::from(test_util::util::TESTFILEDIRECTORY);
|
||||||
|
|
||||||
|
match test_util::util::file_exists(&dir, &filename) {
|
||||||
|
Ok(_) => {
|
||||||
|
let filepath = test_util::util::get_full_path(&dir, &filename).unwrap();
|
||||||
|
match super::get_duration(&filepath) {
|
||||||
|
Ok(duration) => {
|
||||||
|
let song_duration: u64 = 41;
|
||||||
|
let fetched_song_duration = duration.as_secs();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
song_duration, fetched_song_duration,
|
||||||
|
"Durations should match, but they don't {song_duration} {fetched_song_duration} ({duration:?})"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
assert!(false, "Error: {err:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
assert!(false, "Error: {err:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -32,6 +32,11 @@ impl MetadataType {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum Properties {
|
||||||
|
Duration,
|
||||||
|
}
|
||||||
|
|
||||||
pub fn all_metadata_types() -> Vec<Type> {
|
pub fn all_metadata_types() -> Vec<Type> {
|
||||||
vec![
|
vec![
|
||||||
Type::Album,
|
Type::Album,
|
||||||
|
Reference in New Issue
Block a user