Compare commits
4 Commits
v0.4.1-mai
...
tsk-53
| Author | SHA1 | Date | |
|---|---|---|---|
| b73ffce048 | |||
| f5e6ac567e | |||
| c0607597f1 | |||
| 71374eb731 |
@@ -4,6 +4,9 @@ on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
pull_request:
|
||||
branches:
|
||||
- main
|
||||
|
||||
jobs:
|
||||
release:
|
||||
|
||||
120
Cargo.lock
generated
120
Cargo.lock
generated
@@ -14,12 +14,29 @@ version = "2.10.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3"
|
||||
|
||||
[[package]]
|
||||
name = "bumpalo"
|
||||
version = "3.19.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "46c5e41b57b8bba42a04676d81cb89e9ee8e859a1a66f80a5a72e1cb76b34d43"
|
||||
|
||||
[[package]]
|
||||
name = "byteorder"
|
||||
version = "1.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
|
||||
|
||||
[[package]]
|
||||
name = "cfb"
|
||||
version = "0.7.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d38f2da7a0a2c4ccf0065be06397cc26a81f4e528be095826eee9d4adbb8c60f"
|
||||
dependencies = [
|
||||
"byteorder",
|
||||
"fnv",
|
||||
"uuid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.4"
|
||||
@@ -67,6 +84,12 @@ dependencies = [
|
||||
"miniz_oxide",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fnv"
|
||||
version = "1.0.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.3.4"
|
||||
@@ -81,9 +104,10 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "icarus_meta"
|
||||
version = "0.4.1"
|
||||
version = "0.4.5"
|
||||
dependencies = [
|
||||
"imghdr",
|
||||
"infer",
|
||||
"lofty",
|
||||
"rand",
|
||||
"tempfile",
|
||||
@@ -95,6 +119,25 @@ version = "0.7.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c8b35f3ad95576ac81603375dfe47a0450b70a368aa34d2b6e5bb0a0d7f02428"
|
||||
|
||||
[[package]]
|
||||
name = "infer"
|
||||
version = "0.19.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a588916bfdfd92e71cacef98a63d9b1f0d74d6599980d11894290e7ddefffcf7"
|
||||
dependencies = [
|
||||
"cfb",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "js-sys"
|
||||
version = "0.3.81"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ec48937a97411dcb524a265206ccd4c90bb711fca92b2792c407f268825b9305"
|
||||
dependencies = [
|
||||
"once_cell",
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.177"
|
||||
@@ -245,6 +288,12 @@ dependencies = [
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustversion"
|
||||
version = "1.0.22"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d"
|
||||
|
||||
[[package]]
|
||||
name = "simd-adler32"
|
||||
version = "0.3.7"
|
||||
@@ -281,6 +330,16 @@ version = "1.0.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f63a545481291138910575129486daeaf8ac54aee4387fe7906919f7830c7d9d"
|
||||
|
||||
[[package]]
|
||||
name = "uuid"
|
||||
version = "1.18.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2f87b8aa10b915a06587d0dec516c282ff295b475d94abf425d62b57710070a2"
|
||||
dependencies = [
|
||||
"js-sys",
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasip2"
|
||||
version = "1.0.1+wasi-0.2.4"
|
||||
@@ -290,6 +349,65 @@ dependencies = [
|
||||
"wit-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen"
|
||||
version = "0.2.104"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c1da10c01ae9f1ae40cbfac0bac3b1e724b320abfcf52229f80b547c0d250e2d"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"once_cell",
|
||||
"rustversion",
|
||||
"wasm-bindgen-macro",
|
||||
"wasm-bindgen-shared",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-backend"
|
||||
version = "0.2.104"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "671c9a5a66f49d8a47345ab942e2cb93c7d1d0339065d4f8139c486121b43b19"
|
||||
dependencies = [
|
||||
"bumpalo",
|
||||
"log",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"wasm-bindgen-shared",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-macro"
|
||||
version = "0.2.104"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7ca60477e4c59f5f2986c50191cd972e3a50d8a95603bc9434501cf156a9a119"
|
||||
dependencies = [
|
||||
"quote",
|
||||
"wasm-bindgen-macro-support",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-macro-support"
|
||||
version = "0.2.104"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9f07d2f20d4da7b26400c9f4a0511e6e0345b040694e8a75bd41d578fa4421d7"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"wasm-bindgen-backend",
|
||||
"wasm-bindgen-shared",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-shared"
|
||||
version = "0.2.104"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bad67dc8b2a1a6e5448428adec4c3e84c43e561d8c9ee8a9e5aabeb193ec41d1"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-link"
|
||||
version = "0.2.1"
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
[package]
|
||||
name = "icarus_meta"
|
||||
version = "0.4.1"
|
||||
version = "0.4.5"
|
||||
edition = "2024"
|
||||
rust-version = "1.90"
|
||||
|
||||
[dependencies]
|
||||
lofty = { version = "0.22.4" }
|
||||
imghdr = { version = "0.7.0" }
|
||||
infer = { version = "0.19.0" }
|
||||
rand = { version = "0.9.2" }
|
||||
|
||||
[dev-dependencies]
|
||||
|
||||
@@ -1,14 +1,49 @@
|
||||
/// Gets the file type of a CoverArt given it's path
|
||||
pub fn file_type(filepath: &str) -> Result<String, std::io::Error> {
|
||||
pub fn file_type_from_filepath(
|
||||
filepath: &str,
|
||||
) -> Result<crate::detection::FileType, std::io::Error> {
|
||||
match imghdr::from_file(filepath) {
|
||||
Ok(Some(imghdr::Type::Jpeg)) => Ok(String::from("jpeg")),
|
||||
Ok(Some(imghdr::Type::Png)) => Ok(String::from("png")),
|
||||
Ok(Some(imghdr::Type::Jpeg)) => Ok(crate::detection::FileType {
|
||||
mime: String::from(constants::mime::JPEG),
|
||||
file_type: String::from(constants::JPEG_TYPE),
|
||||
}),
|
||||
Ok(Some(imghdr::Type::Png)) => Ok(crate::detection::FileType {
|
||||
mime: String::from(constants::mime::PNG),
|
||||
file_type: String::from(constants::PNG_TYPE),
|
||||
}),
|
||||
Ok(None) => Err(std::io::Error::other("Image file not supported")),
|
||||
Err(err) => Err(err),
|
||||
_ => Err(std::io::Error::other("Image file not supported")),
|
||||
}
|
||||
}
|
||||
|
||||
/// Gets the file type of a CoverArt given it's data
|
||||
pub fn file_type_from_data(data: &Vec<u8>) -> Result<crate::detection::FileType, std::io::Error> {
|
||||
match imghdr::from_bytes(data) {
|
||||
Some(imghdr::Type::Jpeg) => Ok(crate::detection::FileType {
|
||||
mime: String::from(constants::mime::JPEG),
|
||||
file_type: String::from(constants::JPEG_TYPE),
|
||||
}),
|
||||
Some(imghdr::Type::Png) => Ok(crate::detection::FileType {
|
||||
mime: String::from(constants::mime::PNG),
|
||||
file_type: String::from(constants::PNG_TYPE),
|
||||
}),
|
||||
None => Err(std::io::Error::other("Image file not supported")),
|
||||
_ => Err(std::io::Error::other("Image file not supported")),
|
||||
}
|
||||
}
|
||||
|
||||
pub mod constants {
|
||||
pub const PNG_TYPE: &str = "png";
|
||||
pub const JPEG_TYPE: &str = "jpeg";
|
||||
pub const JPG_TYPE: &str = "jpg";
|
||||
|
||||
pub mod mime {
|
||||
pub const JPEG: &str = "image/jpeg";
|
||||
pub const PNG: &str = "image/png";
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
@@ -17,10 +52,32 @@ mod tests {
|
||||
let filename = String::from("Sample Tracks 3.png");
|
||||
let filepath = format!("{directory}/{filename}");
|
||||
|
||||
match super::file_type(&filepath) {
|
||||
match super::file_type_from_filepath(&filepath) {
|
||||
Ok(filetype) => {
|
||||
assert_eq!(
|
||||
filetype, "png",
|
||||
filetype.file_type,
|
||||
super::constants::PNG_TYPE,
|
||||
"The file type of the CoverArt should have been png"
|
||||
);
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {err:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_coverart_file_type_from_data() {
|
||||
let directory = String::from(crate::test_util::util::TESTFILEDIRECTORY);
|
||||
let filename = String::from("Sample Tracks 3.png");
|
||||
let filepath = format!("{directory}/{filename}");
|
||||
let data = crate::test_util::util::get_data_from_file(&filepath).unwrap();
|
||||
|
||||
match super::file_type_from_data(&data) {
|
||||
Ok(filetype) => {
|
||||
assert_eq!(
|
||||
filetype.file_type,
|
||||
super::constants::PNG_TYPE,
|
||||
"The file type of the CoverArt should have been png"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1 +1,34 @@
|
||||
pub mod coverart;
|
||||
pub mod song;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct FileType {
|
||||
pub mime: String,
|
||||
pub file_type: String,
|
||||
}
|
||||
|
||||
/// Initializes a FileType given a filetype passed.
|
||||
pub fn init_filetype(filetype: &str) -> Result<FileType, std::io::Error> {
|
||||
if filetype == song::constants::FLAC_TYPE {
|
||||
Ok(FileType {
|
||||
mime: String::from(song::constants::mime::FLAC),
|
||||
file_type: String::from(song::constants::FLAC_TYPE),
|
||||
})
|
||||
} else if filetype == coverart::constants::PNG_TYPE {
|
||||
Ok(FileType {
|
||||
mime: String::from(coverart::constants::mime::PNG),
|
||||
file_type: String::from(coverart::constants::PNG_TYPE),
|
||||
})
|
||||
} else if filetype == coverart::constants::JPEG_TYPE
|
||||
|| filetype == coverart::constants::JPG_TYPE
|
||||
{
|
||||
Ok(FileType {
|
||||
mime: String::from(coverart::constants::mime::JPEG),
|
||||
file_type: String::from(coverart::constants::JPEG_TYPE),
|
||||
})
|
||||
} else {
|
||||
Err(std::io::Error::other(format!(
|
||||
"Unsupported FileType: {filetype:?}"
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
90
src/detection/song.rs
Normal file
90
src/detection/song.rs
Normal file
@@ -0,0 +1,90 @@
|
||||
/// Gets the file type of a Song from it's path
|
||||
pub fn file_type_from_filepath(
|
||||
filepath: &str,
|
||||
) -> Result<crate::detection::FileType, std::io::Error> {
|
||||
match infer::get_from_path(filepath) {
|
||||
Ok(Some(kind)) => {
|
||||
let mime = kind.mime_type();
|
||||
if mime == constants::mime::FLAC {
|
||||
Ok(crate::detection::FileType {
|
||||
mime: String::from(mime),
|
||||
file_type: String::from(constants::FLAC_TYPE),
|
||||
})
|
||||
} else {
|
||||
Err(std::io::Error::other("Unsupported file type"))
|
||||
}
|
||||
}
|
||||
Ok(None) => Err(std::io::Error::other("File type not determined")),
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
/// Gets the file type of a Song given it's data
|
||||
pub fn file_type_from_data(data: &[u8]) -> Result<crate::detection::FileType, std::io::Error> {
|
||||
match infer::get(data) {
|
||||
Some(kind) => {
|
||||
let mime = kind.mime_type();
|
||||
if mime == constants::mime::FLAC {
|
||||
Ok(crate::detection::FileType {
|
||||
mime: String::from(mime),
|
||||
file_type: String::from(constants::FLAC_TYPE),
|
||||
})
|
||||
} else {
|
||||
Err(std::io::Error::other("Unsupported file type"))
|
||||
}
|
||||
}
|
||||
None => Err(std::io::Error::other("File type not determined")),
|
||||
}
|
||||
}
|
||||
|
||||
pub mod constants {
|
||||
pub const FLAC_TYPE: &str = "flac";
|
||||
|
||||
pub mod mime {
|
||||
pub const FLAC: &str = "audio/x-flac";
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn test_song_file_type() {
|
||||
let directory = String::from(crate::test_util::util::TESTFILEDIRECTORY);
|
||||
let filename = String::from("track01.flac");
|
||||
let filepath = format!("{directory}/{filename}");
|
||||
|
||||
match super::file_type_from_filepath(&filepath) {
|
||||
Ok(filetype) => {
|
||||
assert_eq!(
|
||||
filetype.file_type,
|
||||
crate::detection::song::constants::FLAC_TYPE,
|
||||
"Types do not match"
|
||||
)
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {err:?}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_song_file_type_from_data() {
|
||||
let directory = String::from(crate::test_util::util::TESTFILEDIRECTORY);
|
||||
let filename = String::from("track01.flac");
|
||||
let filepath = format!("{directory}/{filename}");
|
||||
let data = crate::test_util::util::get_data_from_file(&filepath).unwrap();
|
||||
|
||||
match super::file_type_from_data(&data) {
|
||||
Ok(filetype) => {
|
||||
assert_eq!(
|
||||
filetype.file_type,
|
||||
crate::detection::song::constants::FLAC_TYPE,
|
||||
"Types do not match"
|
||||
)
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {err:?}")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
43
src/lib.rs
43
src/lib.rs
@@ -5,7 +5,7 @@ pub mod types;
|
||||
|
||||
pub mod test_util {
|
||||
pub mod util {
|
||||
use std::io::{self, Write};
|
||||
use std::io::{self, Read, Write};
|
||||
|
||||
use rand::Rng;
|
||||
|
||||
@@ -19,10 +19,7 @@ pub mod test_util {
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
pub fn get_full_path(
|
||||
directory: &String,
|
||||
filename: &String,
|
||||
) -> Result<String, std::io::Error> {
|
||||
pub fn get_full_path(directory: &str, filename: &str) -> Result<String, std::io::Error> {
|
||||
match path_buf(directory, filename) {
|
||||
Ok(pf) => Ok(pf.display().to_string()),
|
||||
Err(err) => Err(err),
|
||||
@@ -39,13 +36,42 @@ pub mod test_util {
|
||||
std::fs::copy(src_path, dest_path)
|
||||
}
|
||||
|
||||
pub fn file_exists(directory: &String, filename: &String) -> Result<bool, std::io::Error> {
|
||||
pub fn remove_file(filepath: &str) -> Result<(), std::io::Error> {
|
||||
let f_path = std::path::Path::new(filepath);
|
||||
|
||||
std::fs::remove_file(f_path)
|
||||
}
|
||||
|
||||
pub fn get_data_from_file(source_path: &str) -> Result<Vec<u8>, std::io::Error> {
|
||||
match std::fs::File::open(source_path) {
|
||||
Ok(mut file) => {
|
||||
let mut data: Vec<u8> = Vec::new();
|
||||
match file.read_to_end(&mut data) {
|
||||
Ok(_) => Ok(data),
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn file_exists(directory: &str, filename: &str) -> Result<bool, std::io::Error> {
|
||||
match path_buf(directory, filename) {
|
||||
Ok(pf) => Ok(pf.exists()),
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn generate_newfilepath(directory: &str) -> Result<String, std::io::Error> {
|
||||
match generate_filename() {
|
||||
Ok(filename) => match get_full_path(directory, &filename) {
|
||||
Ok(filepath) => Ok(filepath),
|
||||
Err(err) => Err(err),
|
||||
},
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn generate_filename() -> Result<String, std::io::Error> {
|
||||
let mut filename = String::from("track-");
|
||||
let length = 20;
|
||||
@@ -65,10 +91,7 @@ pub mod test_util {
|
||||
Ok(format!("{filename}.flac"))
|
||||
}
|
||||
|
||||
fn path_buf(
|
||||
directory: &String,
|
||||
filename: &String,
|
||||
) -> Result<std::path::PathBuf, std::io::Error> {
|
||||
fn path_buf(directory: &str, filename: &str) -> Result<std::path::PathBuf, std::io::Error> {
|
||||
let dir_path = std::path::Path::new(&directory);
|
||||
Ok(dir_path.join(filename))
|
||||
}
|
||||
|
||||
@@ -1,51 +0,0 @@
|
||||
use lofty::file::AudioFile;
|
||||
|
||||
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:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
14
src/properties/audio.rs
Normal file
14
src/properties/audio.rs
Normal file
@@ -0,0 +1,14 @@
|
||||
use lofty::file::AudioFile;
|
||||
|
||||
pub fn get_properties(songpath: &str) -> Result<lofty::flac::FlacProperties, std::io::Error> {
|
||||
match std::fs::File::open(songpath) {
|
||||
Ok(mut content) => {
|
||||
match lofty::flac::FlacFile::read_from(&mut content, lofty::config::ParseOptions::new())
|
||||
{
|
||||
Ok(flac_file) => Ok(*flac_file.properties()),
|
||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||
}
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
139
src/properties/mod.rs
Normal file
139
src/properties/mod.rs
Normal file
@@ -0,0 +1,139 @@
|
||||
pub mod audio;
|
||||
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct SongProperties {
|
||||
pub duration: std::time::Duration,
|
||||
pub sample_rate: u32,
|
||||
pub bitrate: u32,
|
||||
pub overall_bitrate: u32,
|
||||
pub bit_depth: u8,
|
||||
pub channels: u8,
|
||||
}
|
||||
|
||||
pub fn get_song_properties(song_path: &str) -> Result<SongProperties, std::io::Error> {
|
||||
match audio::get_properties(song_path) {
|
||||
Ok(flac_properties) => Ok(SongProperties {
|
||||
duration: flac_properties.duration(),
|
||||
sample_rate: flac_properties.sample_rate(),
|
||||
bitrate: flac_properties.audio_bitrate(),
|
||||
overall_bitrate: flac_properties.overall_bitrate(),
|
||||
bit_depth: flac_properties.bit_depth(),
|
||||
channels: flac_properties.channels(),
|
||||
}),
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_duration(song_path: &str) -> Result<std::time::Duration, std::io::Error> {
|
||||
match get_song_properties(song_path) {
|
||||
Ok(song_properties) => Ok(song_properties.duration),
|
||||
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();
|
||||
let new_filepath = test_util::util::generate_newfilepath(&dir).unwrap();
|
||||
|
||||
match test_util::util::copy_file(&filepath, &new_filepath) {
|
||||
Ok(_) => match super::get_duration(&new_filepath) {
|
||||
Ok(duration) => {
|
||||
test_util::util::remove_file(&new_filepath).unwrap();
|
||||
|
||||
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:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {err:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_song_properties() {
|
||||
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();
|
||||
let new_filepath = test_util::util::generate_newfilepath(&dir).unwrap();
|
||||
|
||||
match test_util::util::copy_file(&filepath, &new_filepath) {
|
||||
Ok(_) => match super::get_song_properties(&new_filepath) {
|
||||
Ok(song_properties) => {
|
||||
test_util::util::remove_file(&new_filepath).unwrap();
|
||||
|
||||
let song_duration: u64 = 41;
|
||||
let bitrate: u32 = 1;
|
||||
let overall_bitrate: u32 = 3;
|
||||
let bit_depth: u8 = 24;
|
||||
let channels: u8 = 2;
|
||||
|
||||
let fetched_song_duration = song_properties.duration.as_secs();
|
||||
let fetched_bitrate = song_properties.bitrate;
|
||||
let fetched_overall_bitrate = song_properties.overall_bitrate;
|
||||
let fetched_bit_depth = song_properties.bit_depth;
|
||||
let fetched_channels = song_properties.channels;
|
||||
|
||||
assert_eq!(
|
||||
song_duration, fetched_song_duration,
|
||||
"Durations should match, but they don't {song_duration} {fetched_song_duration} ({song_properties:?})"
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
bitrate, fetched_bitrate,
|
||||
"Bitrates do not match {bitrate:?} {fetched_bitrate:?} {song_properties:?}"
|
||||
);
|
||||
assert_eq!(
|
||||
overall_bitrate, fetched_overall_bitrate,
|
||||
"Overall bitrates do not match {overall_bitrate:?} {fetched_overall_bitrate:?} {song_properties:?}"
|
||||
);
|
||||
assert_eq!(
|
||||
bit_depth, fetched_bit_depth,
|
||||
"Bit depth do not match {bit_depth:?} {fetched_bit_depth:?} {song_properties:?}"
|
||||
);
|
||||
assert_eq!(
|
||||
channels, fetched_channels,
|
||||
"Channels do not match {channels:?} {fetched_channels:?} {song_properties:?}"
|
||||
);
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {err:?}");
|
||||
}
|
||||
},
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {err:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {err:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user