Skip to content

Commit

Permalink
Fix bug, do minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
fsktom committed Oct 19, 2024
1 parent 8a40742 commit 3b2734b
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 23 deletions.
3 changes: 2 additions & 1 deletion endsong_ui/src/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ pub mod relative {
/// Creates an empty trace if `aspect` is not in `entries`
#[must_use]
pub fn to_artist<Asp: AsRef<Album> + Music>(entries: &SongEntries, aspect: &Asp) -> TraceType {
let artist = &aspect.as_ref().artist;
let album: &Album = aspect.as_ref();
let artist = &album.artist;

let mut times = Vec::<String>::new();
// percentages relative to the sum of respective artist plays
Expand Down
2 changes: 1 addition & 1 deletion endsong_ui/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ fn match_print_songs(
songs.len(),
&songs[0].name,
&songs[0].album.artist.name,
entries.gather_plays_of_many(&songs)
gather::plays_of_many(entries, &songs)
);
}
for song in songs {
Expand Down
23 changes: 8 additions & 15 deletions endsong_web/src/album.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![allow(clippy::module_name_repetitions, reason = "looks nicer")]

use crate::{artist::ArtistSelectionTemplate, encode_url, not_found, AppState};
use crate::{artist::ArtistSelectionTemplate, encode_url, not_found, AppState, UrlEncoding};

use std::sync::Arc;

Expand Down Expand Up @@ -128,10 +128,11 @@ pub async fn base(
None
};

let encoded_artist = encode_url(&artist.name);
let encoded_artist = artist.encode();

let Some(album) = album else {
let encoded_album = encode_url(&albums.first().unwrap().name);
// unwrap ok - find().album() guaranteed to contain at least one album if Some, see earlier
let encoded_album = albums.first().unwrap().encode();

let link_base_album = if let Some(artist_id) = options.artist_id {
format!("/album/{encoded_artist}/{encoded_album}?artist_id={artist_id}")
Expand All @@ -148,9 +149,10 @@ pub async fn base(

// http://localhost:3000/album/TiA/%E6%B5%81%E6%98%9F
// (i.e. could only happen on manual link entry)
if &album.artist != artist {
if album.artist != artist {
return Redirect::permanent(&format!(
"/artist/{encoded_artist}?artist_id={}",
// unwrap ok - you're right after the artist sel page if this happens -> artist_id exists
options.artist_id.unwrap()
))
.into_response();
Expand All @@ -167,17 +169,8 @@ pub async fn base(
);

// unwrap ok bc already made sure artist exists earlier
let first_listen = entries
.iter()
.find(|entry| album.is_entry(entry))
.unwrap()
.timestamp;
let last_listen = entries
.iter()
.rev()
.find(|entry| album.is_entry(entry))
.unwrap()
.timestamp;
let first_listen = gather::first_entry_of(entries, album).unwrap().timestamp;
let last_listen = gather::last_entry_of(entries, album).unwrap().timestamp;

let link_artist = if let Some(artist_id) = options.artist_id {
format!("/artist/{encoded_artist}?artist_id={artist_id}")
Expand Down
2 changes: 2 additions & 0 deletions endsong_web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ pub async fn top_artists(
.map(|artist| {
(
artist.clone(),
// unwrap ok - artist guaranteed to be in map
state.artist_info.get(artist).unwrap().clone(),
)
})
Expand All @@ -233,6 +234,7 @@ pub async fn top_artists(
.map(|artist| {
(
artist.clone(),
// unwrap ok - artist guaranteed to be in map
state.artist_info.get(artist).unwrap().clone(),
)
})
Expand Down
30 changes: 30 additions & 0 deletions src/aspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,16 @@ impl AsRef<str> for Artist {
&self.name
}
}
impl PartialEq<&Artist> for Artist {
fn eq(&self, other: &&Artist) -> bool {
self.name == other.name
}
}
impl PartialEq<Artist> for &Artist {
fn eq(&self, other: &Artist) -> bool {
self.name == other.name
}
}
impl Music for Artist {
fn is_entry(&self, entry: &SongEntry) -> bool {
entry.artist == self.name
Expand Down Expand Up @@ -263,6 +273,16 @@ impl AsRef<str> for Album {
&self.name
}
}
impl PartialEq<&Album> for Album {
fn eq(&self, other: &&Album) -> bool {
self.name == other.name
}
}
impl PartialEq<Album> for &Album {
fn eq(&self, other: &Album) -> bool {
self.name == other.name
}
}
impl Music for Album {
fn is_entry(&self, entry: &SongEntry) -> bool {
self.artist.is_entry(entry) && entry.album == self.name
Expand Down Expand Up @@ -389,6 +409,16 @@ impl AsRef<str> for Song {
&self.name
}
}
impl PartialEq<&Song> for Song {
fn eq(&self, other: &&Song) -> bool {
self.name == other.name
}
}
impl PartialEq<Song> for &Song {
fn eq(&self, other: &Song) -> bool {
self.name == other.name
}
}
impl Music for Song {
fn is_entry(&self, entry: &SongEntry) -> bool {
self.album.is_entry(entry) && entry.track == self.name
Expand Down
6 changes: 0 additions & 6 deletions src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,12 +459,6 @@ impl SongEntries {
.collect_vec()
}

/// Counts up the plays of all [`Music`] in a collection
#[must_use]
pub fn gather_plays_of_many<Asp: Music>(&self, aspects: &[Asp]) -> usize {
gather::plays_of_many(self, aspects)
}

/// Adds search capability
///
/// Use with methods from [`Find`] to search for valid artist, album or song
Expand Down
16 changes: 16 additions & 0 deletions src/gather.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,19 @@ pub fn listening_time_of_many<Asp: Music>(entries: &[SongEntry], aspects: &[Asp]
.map(|entry| entry.time_played)
.sum()
}

/// Returns first occurence of an aspect
pub fn first_entry_of<'a, Asp: Music>(
entries: &'a [SongEntry],
aspect: &Asp,
) -> Option<&'a SongEntry> {
entries.iter().find(|entry| aspect.is_entry(entry))
}

/// Returns last occurence of an aspect
pub fn last_entry_of<'a, Asp: Music>(
entries: &'a [SongEntry],
aspect: &Asp,
) -> Option<&'a SongEntry> {
entries.iter().rev().find(|entry| aspect.is_entry(entry))
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// https://blog.rust-lang.org/2024/09/05/Rust-1.81.0.html#expectlint
#![warn(clippy::allow_attributes_without_reason)]
#![warn(clippy::allow_attributes)]
#![allow(clippy::too_many_lines, reason = "stoopid")]

pub mod aspect;
pub mod entry;
Expand Down

0 comments on commit 3b2734b

Please sign in to comment.