Replace TrackFormat with a Quality field

This commit is contained in:
Wojciech Kozlowski 2023-05-10 23:38:24 +02:00
parent 282e0e6f19
commit ef37b9d7bc
8 changed files with 187 additions and 124 deletions

View File

@ -61,7 +61,7 @@ mod tests {
use super::*;
use crate::{tests::COLLECTION, Artist, ArtistId, TrackFormat};
use crate::{tests::COLLECTION, Artist, ArtistId, Quality};
fn artist_to_json(artist: &Artist) -> String {
let album_artist = &artist.id.name;
@ -82,19 +82,26 @@ mod tests {
}
let track_artist = track_artist.join(",");
let track_format: &'static str = match track.format {
TrackFormat::Flac => stringify!(Flac),
TrackFormat::Mp3 => stringify!(Mp3),
match track.quality {
Quality::Flac => tracks.push(format!(
"{{\
\"number\":{track_number},\
\"title\":\"{track_title}\",\
\"artist\":[{track_artist}],\
\"quality\":\"{format}\"\
}}",
format = stringify!(Flac)
)),
Quality::Mp3(bitrate) => tracks.push(format!(
"{{\
\"number\":{track_number},\
\"title\":\"{track_title}\",\
\"artist\":[{track_artist}],\
\"quality\":{{\"{format}\":{bitrate}}}\
}}",
format = stringify!(Mp3)
)),
};
tracks.push(format!(
"{{\
\"number\":{track_number},\
\"title\":\"{track_title}\",\
\"artist\":[{track_artist}],\
\"format\":\"{track_format}\"\
}}"
));
}
let tracks = tracks.join(",");

View File

@ -15,9 +15,9 @@ pub type Mbid = Uuid;
/// The track file format.
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
pub enum TrackFormat {
pub enum Quality {
Flac,
Mp3,
Mp3(u32),
}
/// A single track on an album.
@ -26,7 +26,7 @@ pub struct Track {
pub number: u32,
pub title: String,
pub artist: Vec<String>,
pub format: TrackFormat,
pub quality: Quality,
}
/// The album identifier.

View File

@ -8,7 +8,7 @@ use std::{
str,
};
use super::{IBeetsLibraryExecutor, Error};
use super::{Error, IBeetsLibraryExecutor};
const BEET_DEFAULT: &str = "beet";

View File

@ -9,7 +9,7 @@ use std::{
#[cfg(test)]
use mockall::automock;
use crate::{Album, AlbumId, Artist, ArtistId, Track, TrackFormat};
use crate::{Album, AlbumId, Artist, ArtistId, Quality, Track};
use super::{Error, Field, ILibrary, Query};
@ -37,7 +37,9 @@ const LIST_FORMAT_ARG: &str = concat!(
list_format_separator!(),
"$artist",
list_format_separator!(),
"$format"
"$format",
list_format_separator!(),
"$bitrate"
);
const TRACK_FORMAT_FLAC: &str = "FLAC";
const TRACK_FORMAT_MP3: &str = "MP3";
@ -127,7 +129,7 @@ impl<BLE: IBeetsLibraryExecutor> ILibraryPrivate for BeetsLibrary<BLE> {
}
let split: Vec<&str> = line.split(LIST_FORMAT_SEPARATOR).collect();
if split.len() != 7 {
if split.len() != 8 {
return Err(Error::Invalid(line.to_string()));
}
@ -138,6 +140,7 @@ impl<BLE: IBeetsLibraryExecutor> ILibraryPrivate for BeetsLibrary<BLE> {
let track_title = split[4].to_string();
let track_artist = split[5].to_string();
let track_format = split[6].to_string();
let track_bitrate = split[7].trim_end_matches("kbps").parse::<u32>()?;
let artist_id = ArtistId { name: album_artist };
@ -150,9 +153,9 @@ impl<BLE: IBeetsLibraryExecutor> ILibraryPrivate for BeetsLibrary<BLE> {
number: track_number,
title: track_title,
artist: track_artist.split("; ").map(|s| s.to_owned()).collect(),
format: match track_format.as_ref() {
TRACK_FORMAT_FLAC => TrackFormat::Flac,
TRACK_FORMAT_MP3 => TrackFormat::Mp3,
quality: match track_format.as_ref() {
TRACK_FORMAT_FLAC => Quality::Flac,
TRACK_FORMAT_MP3 => Quality::Mp3(track_bitrate),
_ => return Err(Error::Invalid(line.to_string())),
},
};
@ -219,14 +222,15 @@ mod tests {
let track_number = &track.number;
let track_title = &track.title;
let track_artist = &track.artist.join("; ");
let track_format = match track.format {
TrackFormat::Flac => TRACK_FORMAT_FLAC,
TrackFormat::Mp3 => TRACK_FORMAT_MP3,
let (track_format, track_bitrate) = match track.quality {
Quality::Flac => (TRACK_FORMAT_FLAC, 1411),
Quality::Mp3(bitrate) => (TRACK_FORMAT_MP3, bitrate),
};
strings.push(format!(
"{album_artist}{0}{album_year}{0}{album_title}{0}\
{track_number}{0}{track_title}{0}{track_artist}{0}{track_format}",
{track_number}{0}{track_title}{0}\
{track_artist}{0}{track_format}{0}{track_bitrate}kbps",
LIST_FORMAT_SEPARATOR,
));
}
@ -453,11 +457,8 @@ mod tests {
.split(LIST_FORMAT_SEPARATOR)
.map(|s| s.to_owned())
.collect::<Vec<String>>();
invalid_string.last_mut().unwrap().clear();
invalid_string
.last_mut()
.unwrap()
.push_str("invalid format");
invalid_string[6].clear();
invalid_string[6].push_str("invalid format");
let invalid_string = invalid_string.join(LIST_FORMAT_SEPARATOR);
output[2] = invalid_string.clone();
let result = Ok(output);

View File

@ -16,7 +16,7 @@ macro_rules! collection {
number: 1,
title: "track a.a.1".to_string(),
artist: vec!["artist a.a.1".to_string()],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 2,
@ -25,13 +25,13 @@ macro_rules! collection {
"artist a.a.2.1".to_string(),
"artist a.a.2.2".to_string(),
],
format: TrackFormat::Mp3,
quality: Quality::Mp3(320),
},
Track {
number: 3,
title: "track a.a.3".to_string(),
artist: vec!["artist a.a.3".to_string()],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
],
},
@ -45,13 +45,13 @@ macro_rules! collection {
number: 1,
title: "track a.b.1".to_string(),
artist: vec!["artist a.b.1".to_string()],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 2,
title: "track a.b.2".to_string(),
artist: vec!["artist a.b.2".to_string()],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
],
},
@ -72,7 +72,7 @@ macro_rules! collection {
number: 1,
title: "track b.a.1".to_string(),
artist: vec!["artist b.a.1".to_string()],
format: TrackFormat::Mp3,
quality: Quality::Mp3(190),
},
Track {
number: 2,
@ -81,7 +81,7 @@ macro_rules! collection {
"artist b.a.2.1".to_string(),
"artist b.a.2.2".to_string(),
],
format: TrackFormat::Mp3,
quality: Quality::Mp3(120),
},
],
},
@ -95,7 +95,7 @@ macro_rules! collection {
number: 1,
title: "track b.b.1".to_string(),
artist: vec!["artist b.b.1".to_string()],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 2,
@ -104,7 +104,7 @@ macro_rules! collection {
"artist b.b.2.1".to_string(),
"artist b.b.2.2".to_string(),
],
format: TrackFormat::Mp3,
quality: Quality::Mp3(320),
},
],
},
@ -125,7 +125,7 @@ macro_rules! collection {
number: 1,
title: "track c.a.1".to_string(),
artist: vec!["artist c.a.1".to_string()],
format: TrackFormat::Mp3,
quality: Quality::Mp3(320),
},
Track {
number: 2,
@ -134,7 +134,7 @@ macro_rules! collection {
"artist c.a.2.1".to_string(),
"artist c.a.2.2".to_string(),
],
format: TrackFormat::Mp3,
quality: Quality::Mp3(120),
},
],
},
@ -148,7 +148,7 @@ macro_rules! collection {
number: 1,
title: "track c.b.1".to_string(),
artist: vec!["artist c.b.1".to_string()],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 2,
@ -157,7 +157,7 @@ macro_rules! collection {
"artist c.b.2.1".to_string(),
"artist c.b.2.2".to_string(),
],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
],
},

View File

@ -1,4 +1,4 @@
use musichoard::{Album, Artist, Collection, Track, TrackFormat};
use musichoard::{Album, Artist, Collection, Quality, Track};
use ratatui::{
backend::Backend,
layout::{Alignment, Rect},
@ -422,11 +422,11 @@ impl<'a, 'b> TrackState<'a, 'b> {
.map(|t| t.artist.join("; "))
.unwrap_or_else(|| "".to_string()),
track
.map(|t| match t.format {
TrackFormat::Flac => "FLAC",
TrackFormat::Mp3 => "MP3",
.map(|t| match t.quality {
Quality::Flac => "FLAC".to_string(),
Quality::Mp3(bitrate) => format!("MP3 {bitrate}"),
})
.unwrap_or(""),
.unwrap_or_else(|| "".to_string()),
));
TrackState {

File diff suppressed because one or more lines are too long

View File

@ -1,7 +1,7 @@
mod database;
mod library;
use musichoard::{Album, AlbumId, Artist, ArtistId, Track, TrackFormat};
use musichoard::{Album, AlbumId, Artist, ArtistId, Quality, Track};
use once_cell::sync::Lazy;
static COLLECTION: Lazy<Vec<Artist>> = Lazy::new(|| {
@ -20,85 +20,85 @@ static COLLECTION: Lazy<Vec<Artist>> = Lazy::new(|| {
number: 01,
title: String::from("Az"),
artist: vec![String::from("Аркона")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 02,
title: String::from("Arkaim"),
artist: vec![String::from("Аркона")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 03,
title: String::from("Bolno mne"),
artist: vec![String::from("Аркона")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 04,
title: String::from("Leshiy"),
artist: vec![String::from("Аркона")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 05,
title: String::from("Zakliatie"),
artist: vec![String::from("Аркона")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 06,
title: String::from("Predok"),
artist: vec![String::from("Аркона")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 07,
title: String::from("Nikogda"),
artist: vec![String::from("Аркона")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 08,
title: String::from("Tam za tumanami"),
artist: vec![String::from("Аркона")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 09,
title: String::from("Potomok"),
artist: vec![String::from("Аркона")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 10,
title: String::from("Slovo"),
artist: vec![String::from("Аркона")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 11,
title: String::from("Odna"),
artist: vec![String::from("Аркона")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 12,
title: String::from("Vo moiom sadochke…"),
artist: vec![String::from("Аркона")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 13,
title: String::from("Stenka na stenku"),
artist: vec![String::from("Аркона")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 14,
title: String::from("Zimushka"),
artist: vec![String::from("Аркона")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
],
}],
@ -118,73 +118,73 @@ static COLLECTION: Lazy<Vec<Artist>> = Lazy::new(|| {
number: 01,
title: String::from("Samon"),
artist: vec![String::from("Eluveitie")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 02,
title: String::from("Primordial Breath"),
artist: vec![String::from("Eluveitie")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 03,
title: String::from("Inis Mona"),
artist: vec![String::from("Eluveitie")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 04,
title: String::from("Gray Sublime Archon"),
artist: vec![String::from("Eluveitie")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 05,
title: String::from("Anagantios"),
artist: vec![String::from("Eluveitie")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 06,
title: String::from("Bloodstained Ground"),
artist: vec![String::from("Eluveitie")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 07,
title: String::from("The Somber Lay"),
artist: vec![String::from("Eluveitie")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 08,
title: String::from("Slanias Song"),
artist: vec![String::from("Eluveitie")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 09,
title: String::from("Giamonios"),
artist: vec![String::from("Eluveitie")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 10,
title: String::from("Tarvos"),
artist: vec![String::from("Eluveitie")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 11,
title: String::from("Calling the Rain"),
artist: vec![String::from("Eluveitie")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 12,
title: String::from("Elembivos"),
artist: vec![String::from("Eluveitie")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
],
},
@ -198,37 +198,37 @@ static COLLECTION: Lazy<Vec<Artist>> = Lazy::new(|| {
number: 01,
title: String::from("Verja Urit an Bitus"),
artist: vec![String::from("Eluveitie")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 02,
title: String::from("Uis Elveti"),
artist: vec![String::from("Eluveitie")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 03,
title: String::from("Ôrô"),
artist: vec![String::from("Eluveitie")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 04,
title: String::from("Lament"),
artist: vec![String::from("Eluveitie")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 05,
title: String::from("Druid"),
artist: vec![String::from("Eluveitie")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 06,
title: String::from("Jêzaïg"),
artist: vec![String::from("Eluveitie")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
],
},
@ -248,67 +248,122 @@ static COLLECTION: Lazy<Vec<Artist>> = Lazy::new(|| {
number: 01,
title: String::from("Intro = Chaos"),
artist: vec![String::from("Frontside")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 02,
title: String::from("Modlitwa"),
artist: vec![String::from("Frontside")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 03,
title: String::from("Długa droga z piekła"),
artist: vec![String::from("Frontside")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 04,
title: String::from("Synowie ognia"),
artist: vec![String::from("Frontside")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 05,
title: String::from("1902"),
artist: vec![String::from("Frontside")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 06,
title: String::from("Krew za krew"),
artist: vec![String::from("Frontside")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 07,
title: String::from("Kulminacja"),
artist: vec![String::from("Frontside")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 08,
title: String::from("Judasz"),
artist: vec![String::from("Frontside")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 09,
title: String::from("Więzy"),
artist: vec![String::from("Frontside")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 10,
title: String::from("Zagubione dusze"),
artist: vec![String::from("Frontside")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 11,
title: String::from("Linia życia"),
artist: vec![String::from("Frontside")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
],
}],
},
Artist {
id: ArtistId {
name: String::from("Heavens Basement"),
},
albums: vec![Album {
id: AlbumId {
year: 2011,
title: String::from("Unbreakable"),
},
tracks: vec![
Track {
number: 01,
title: String::from("Unbreakable"),
artist: vec![String::from("Heavens Basement")],
quality: Quality::Mp3(208),
},
Track {
number: 02,
title: String::from("Guilt Trips and Sins"),
artist: vec![String::from("Heavens Basement")],
quality: Quality::Mp3(205),
},
Track {
number: 03,
title: String::from("The Long Goodbye"),
artist: vec![String::from("Heavens Basement")],
quality: Quality::Mp3(227),
},
Track {
number: 04,
title: String::from("Close Encounters"),
artist: vec![String::from("Heavens Basement")],
quality: Quality::Mp3(213),
},
Track {
number: 05,
title: String::from("Paranoia"),
artist: vec![String::from("Heavens Basement")],
quality: Quality::Mp3(218),
},
Track {
number: 06,
title: String::from("Let Me Out of Here"),
artist: vec![String::from("Heavens Basement")],
quality: Quality::Mp3(207),
},
Track {
number: 07,
title: String::from("Leeches"),
artist: vec![String::from("Heavens Basement")],
quality: Quality::Mp3(225),
},
],
}],
@ -328,49 +383,49 @@ static COLLECTION: Lazy<Vec<Artist>> = Lazy::new(|| {
number: 01,
title: String::from("Fight Fire with Fire"),
artist: vec![String::from("Metallica")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 02,
title: String::from("Ride the Lightning"),
artist: vec![String::from("Metallica")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 03,
title: String::from("For Whom the Bell Tolls"),
artist: vec![String::from("Metallica")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 04,
title: String::from("Fade to Black"),
artist: vec![String::from("Metallica")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 05,
title: String::from("Trapped under Ice"),
artist: vec![String::from("Metallica")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 06,
title: String::from("Escape"),
artist: vec![String::from("Metallica")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 07,
title: String::from("Creeping Death"),
artist: vec![String::from("Metallica")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 08,
title: String::from("The Call of Ktulu"),
artist: vec![String::from("Metallica")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
],
},
@ -384,127 +439,127 @@ static COLLECTION: Lazy<Vec<Artist>> = Lazy::new(|| {
number: 01,
title: String::from("The Ecstasy of Gold"),
artist: vec![String::from("Metallica")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 02,
title: String::from("The Call of Ktulu"),
artist: vec![String::from("Metallica")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 03,
title: String::from("Master of Puppets"),
artist: vec![String::from("Metallica")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 04,
title: String::from("Of Wolf and Man"),
artist: vec![String::from("Metallica")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 05,
title: String::from("The Thing That Should Not Be"),
artist: vec![String::from("Metallica")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 06,
title: String::from("Fuel"),
artist: vec![String::from("Metallica")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 07,
title: String::from("The Memory Remains"),
artist: vec![String::from("Metallica")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 08,
title: String::from("No Leaf Clover"),
artist: vec![String::from("Metallica")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 09,
title: String::from("Hero of the Day"),
artist: vec![String::from("Metallica")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 10,
title: String::from("Devils Dance"),
artist: vec![String::from("Metallica")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 11,
title: String::from("Bleeding Me"),
artist: vec![String::from("Metallica")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 12,
title: String::from("Nothing Else Matters"),
artist: vec![String::from("Metallica")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 13,
title: String::from("Until It Sleeps"),
artist: vec![String::from("Metallica")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 14,
title: String::from("For Whom the Bell Tolls"),
artist: vec![String::from("Metallica")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 15,
title: String::from("Human"),
artist: vec![String::from("Metallica")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 16,
title: String::from("Wherever I May Roam"),
artist: vec![String::from("Metallica")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 17,
title: String::from("Outlaw Torn"),
artist: vec![String::from("Metallica")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 18,
title: String::from("Sad but True"),
artist: vec![String::from("Metallica")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 19,
title: String::from("One"),
artist: vec![String::from("Metallica")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 20,
title: String::from("Enter Sandman"),
artist: vec![String::from("Metallica")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
Track {
number: 21,
title: String::from("Battery"),
artist: vec![String::from("Metallica")],
format: TrackFormat::Flac,
quality: Quality::Flac,
},
],
},