Skip to content

Commit

Permalink
Improve ergonomics of url encoding aspect names
Browse files Browse the repository at this point in the history
  • Loading branch information
fsktom committed Oct 15, 2024
1 parent 6c98e3a commit bc5496d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
26 changes: 9 additions & 17 deletions endsong_web/src/artist.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::{encode_url, not_found, AppState, ArtistInfo};
use crate::{not_found, AppState, ArtistInfo, UrlEncoding};

use std::sync::Arc;

Expand Down Expand Up @@ -44,7 +44,7 @@ impl ArtistSelectionTemplate {
#[must_use]
pub fn new(artists: Vec<Artist>) -> Self {
Self {
link_base_artist: format!("/artist/{}", encode_url(&artists.first().unwrap().name)),
link_base_artist: format!("/artist/{}", &artists.first().unwrap().encode()),
artists,
}
}
Expand Down Expand Up @@ -144,7 +144,7 @@ pub async fn base(
.unwrap()
.timestamp;

let encoded_artist = encode_url(&artist.name);
let encoded_artist = artist.encode();
let (link_albums, link_songs, link_absolute, link_relative) =
if let Some(artist_id) = options.artist_id {
(
Expand Down Expand Up @@ -388,15 +388,11 @@ pub async fn albums(
if let Some(artist_id) = options.artist_id {
format!(
"/album/{}/{}?artist_id={artist_id}",
encode_url(&album.artist.name),
encode_url(&album.name)
album.artist.encode(),
album.encode()
)
} else {
format!(
"/album/{}/{}",
encode_url(&album.artist.name),
encode_url(&album.name)
)
format!("/album/{}/{}", album.artist.encode(), album.encode())
}
};

Expand Down Expand Up @@ -476,15 +472,11 @@ pub async fn songs(
if let Some(artist_id) = options.artist_id {
format!(
"/song/{}/{}?artist_id={artist_id}",
encode_url(&song.album.artist.name),
encode_url(&song.name)
song.album.artist.encode(),
song.encode()
)
} else {
format!(
"/song/{}/{}",
encode_url(&song.album.artist.name),
encode_url(&song.name)
)
format!("/song/{}/{}", song.album.artist.encode(), song.encode())
}
};

Expand Down
17 changes: 15 additions & 2 deletions endsong_web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl AppState {
.enumerate()
{
artist_info.entry(artist.clone()).or_insert(ArtistInfo {
link: Arc::from(format!("/artist/{}", encode_url(artist.as_ref()))),
link: Arc::from(format!("/artist/{}", artist.encode())),
plays: *plays,
duration: *duration,
// bc enumerate starts with 0
Expand Down Expand Up @@ -244,12 +244,25 @@ pub async fn top_artists(
TopArtistsTempate { artists }
}

/// Helper trait for encoding aspect names to make them work in URLs
pub trait UrlEncoding {
/// Encodes it for URL usage
fn encode(&self) -> String;
}
/// Auto implements this trait for [`Artist`], [`Album`] and [`Song`]
impl<Aspect: Music> UrlEncoding for Aspect {
/// Encodes the aspect's `name` for URL usage
fn encode(&self) -> String {
encode_url(self.as_ref())
}
}

/// Custom URL encoding
///
/// Mostly for encoding `/` in something like `AC/DC`
/// to make a working link
#[must_use]
pub fn encode_url(name: &str) -> String {
fn encode_url(name: &str) -> String {
urlencoding::encode(name).into_owned()
}

Expand Down
2 changes: 1 addition & 1 deletion src/aspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ use crate::entry::SongEntry;

/// Used for functions that accept either
/// a [`Song`], [`Album`] or [`Artist`] struct
pub trait Music: Display + Clone + Eq + Ord {
pub trait Music: Display + Clone + Eq + Ord + AsRef<str> {
/// Checks if a [`SongEntry`] is a [`Music`]
fn is_entry(&self, entry: &SongEntry) -> bool;

Expand Down

0 comments on commit bc5496d

Please sign in to comment.