use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, Default, Deserialize, Serialize)] #[serde(default, rename_all = "camelCase")] pub struct Song { pub id: String, pub title: String, pub artist: String, pub artist_id: Option, pub album: String, pub album_id: Option, pub cover_art: Option, pub duration: u64, pub year: Option, pub starred: Option, pub is_external: bool, pub octo_suggested_by: Option, // A documented server default, not the recommendation or catalog provider. pub playback_source: Option, } #[derive(Clone, Debug)] pub enum Item { HomeSection(HomeSection), Song(Song), Album { id: String, name: String, artist: String, cover_art: Option, }, Artist { id: String, name: String, }, Playlist { id: String, name: String, }, Genre(String), Station { id: String, name: String, }, } impl Item { pub fn title(&self) -> &str { match self { Self::HomeSection(section) => section.title(), Self::Song(s) => &s.title, Self::Album { name, .. } | Self::Artist { name, .. } | Self::Playlist { name, .. } | Self::Station { name, .. } => name, Self::Genre(name) => name, } } /// Prefer the server's explicit cover key, including for external discoveries. pub fn cover(&self) -> Option<(String, String)> { match self { Self::Song(s) => Some(( s.cover_art .as_ref() .or(s.album_id.as_ref()) .unwrap_or(&s.id) .clone(), s.title.clone(), )), Self::Album { id, name, cover_art, .. } => Some((cover_art.as_ref().unwrap_or(id).clone(), name.clone())), _ => None, } } pub fn playback_source(&self) -> &str { match self { Self::Song(s) if !s.is_external => "Library", Self::Song(s) => s.playback_source.as_deref().unwrap_or("Unknown"), _ => "—", } } pub fn recommendation_source(&self) -> &str { match self { Self::Song(s) => s .octo_suggested_by .as_deref() .filter(|s| !s.trim().is_empty()) .unwrap_or(if s.is_external { "Unknown" } else { "—" }), _ => "—", } } pub fn detail(&self) -> String { match self { Self::HomeSection(section) => section.description().into(), Self::Song(s) => format!("{} · {}", s.artist, s.album), Self::Album { artist, .. } => artist.clone(), Self::Artist { .. } => "Artist → tracks".into(), Self::Playlist { .. } => "Playlist → tracks".into(), Self::Station { .. } => "Octo station → tracks".into(), Self::Genre(_) => "Genre → tracks".into(), } } pub fn label(&self) -> &str { match self { Self::HomeSection(_) => "BROWSE", Self::Song(s) if s.is_external => "DISCOVER", Self::Song(s) if s.starred.is_some() => "♥", Self::Song(_) => "LIBRARY", Self::Album { .. } => "ALBUM", Self::Artist { .. } => "ARTIST", Self::Playlist { .. } => "LIST", Self::Genre(_) => "GENRE", Self::Station { .. } => "STATION", } } } #[derive(Clone, Debug)] pub enum View { Home, Albums { kind: String, offset: usize }, Artists, Songs { offset: usize }, History, Playlists, Genres, Discover, Stations, Search(String), Album(String), Artist(String), ArtistByName(String), Playlist(String), Genre(String), Radio(String), Station(String), Queue, } impl View { pub fn title(&self) -> String { match self { Self::Albums { kind, offset } => format!( "Albums · {} · page {}", album_order_title(kind), offset / 100 + 1 ), Self::Songs { offset } => format!("Songs · page {}", offset / 100 + 1), Self::Search(q) => format!("Search · {q}"), Self::Album(_) => "Album tracks".into(), Self::Artist(_) => "Artist tracks".into(), Self::ArtistByName(name) => format!("Artist tracks · {name}"), Self::Playlist(_) => "Playlist tracks".into(), Self::Genre(g) => format!("Genre · {g}"), Self::Radio(_) => "Song radio · similar tracks".into(), Self::Station(_) => "Station tracks".into(), other => format!("{other:?}"), } } } #[derive(Clone, Copy, Debug)] pub enum HomeSection { RecentlyPlayed, RecentlyAdded, MostPlayed, Rediscover, HighestRated, History, Discover, Stations, } impl HomeSection { pub const ALL: [Self; 8] = [ Self::RecentlyPlayed, Self::RecentlyAdded, Self::MostPlayed, Self::Rediscover, Self::HighestRated, Self::History, Self::Discover, Self::Stations, ]; pub fn title(self) -> &'static str { match self { Self::RecentlyPlayed => "Recently played", Self::RecentlyAdded => "Recently added", Self::MostPlayed => "Most played", Self::Rediscover => "Rediscover", Self::HighestRated => "Highest rated", Self::History => "History", Self::Discover => "Discover new music", Self::Stations => "Stations", } } pub fn description(self) -> &'static str { match self { Self::RecentlyPlayed => "Albums you listened to most recently", Self::RecentlyAdded => "The newest additions to your library", Self::MostPlayed => "Your most frequently played albums", Self::Rediscover => "A random selection from your library", Self::HighestRated => "Albums with the highest server ratings", Self::History => "Songs played in this player, newest first", Self::Discover => "Octo charts or a library shuffle", Self::Stations => "Explore your server's Octo stations", } } /// Reuse paginated album browsing and existing discovery views for each shelf. pub fn view(self) -> View { let kind = match self { Self::RecentlyPlayed => "recent", Self::RecentlyAdded => "newest", Self::MostPlayed => "frequent", Self::Rediscover => "random", Self::HighestRated => "highest", Self::History => return View::History, Self::Discover => return View::Discover, Self::Stations => return View::Stations, }; View::Albums { kind: kind.into(), offset: 0, } } } /// Home is available immediately, even while the server is offline. pub fn home_items() -> Vec { HomeSection::ALL .into_iter() .map(Item::HomeSection) .collect() } fn album_order_title(kind: &str) -> &str { match kind { "recent" => "Recently played", "newest" => "Recently added", "frequent" => "Most played", "random" => "Rediscover", "highest" => "Highest rated", other => other, } } pub fn time(seconds: u64) -> String { format!("{}:{:02}", seconds / 60, seconds % 60) }