Sort albums by month if two releases of the same artist happen in the same year #155

Merged
2 changed files with 43 additions and 7 deletions
Showing only changes of commit dd00a90a10 - Show all commits

View File

@ -40,6 +40,18 @@ pub struct AlbumDate {
pub day: u8,
}
impl Display for AlbumDate {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.month.is_none() {
write!(f, "{}", self.year)
} else if self.day == 0 {
write!(f, "{}{:02}", self.year, self.month as u8)
} else {
write!(f, "{}{:02}{:02}", self.year, self.month as u8, self.day)
}
}
}
/// The album's sequence to determine order when two or more albums have the same release date.
#[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd, Ord, Eq, Hash)]
pub struct AlbumSeq(pub u8);
@ -83,6 +95,12 @@ impl From<u8> for AlbumMonth {
}
}
impl AlbumMonth {
fn is_none(&self) -> bool {
matches!(self, AlbumMonth::None)
}
}
impl Album {
pub fn get_sort_key(&self) -> (&AlbumDate, &AlbumSeq, &AlbumId) {
(&self.date, &self.seq, &self.id)
@ -148,6 +166,16 @@ mod tests {
use super::*;
impl AlbumDate {
fn new<M: Into<AlbumMonth>>(year: u32, month: M, day: u8) -> Self {
AlbumDate {
year,
month: month.into(),
day,
}
}
}
#[test]
fn album_month() {
assert_eq!(<u8 as Into<AlbumMonth>>::into(0), AlbumMonth::None);
@ -167,13 +195,17 @@ mod tests {
assert_eq!(<u8 as Into<AlbumMonth>>::into(255), AlbumMonth::None);
}
#[test]
fn album_display() {
assert_eq!(AlbumDate::default().to_string(), "0");
assert_eq!(AlbumDate::new(1990, 0, 0).to_string(), "1990");
assert_eq!(AlbumDate::new(1990, 5, 0).to_string(), "199005");
assert_eq!(AlbumDate::new(1990, 5, 6).to_string(), "19900506");
}
#[test]
fn same_date_seq_cmp() {
let date = AlbumDate {
year: 2024,
month: AlbumMonth::March,
day: 2,
};
let date = AlbumDate::new(2024, 3, 2);
let album_id_1 = AlbumId {
title: String::from("album z"),

View File

@ -287,9 +287,13 @@ impl<'a, 'b> AlbumState<'a, 'b> {
let album = state.list.selected().map(|i| &albums[i]);
let info = Paragraph::new(format!(
"Title: {}\n\
Year: {}",
Date: {}{}",
album.map(|a| a.id.title.as_str()).unwrap_or(""),
album.map(|a| a.date.year.to_string()).unwrap_or_default(),
album.map(|a| a.date.to_string()).unwrap_or_default(),
album
.filter(|a| a.seq.0 > 0)
.map(|a| format!(" ({})", a.seq.0))
.unwrap_or_default()
));
AlbumState {