octo-tui/src/model.rs
src/model.rsRaw
1use serde::{Deserialize, Serialize};
2
3#[derive(Clone, Debug, Default, Deserialize, Serialize)]
4#[serde(default, rename_all = "camelCase")]
5pub struct Song {
6 pub id: String,
7 pub title: String,
8 pub artist: String,
9 pub artist_id: Option<String>,
10 pub album: String,
11 pub album_id: Option<String>,
12 pub cover_art: Option<String>,
13 pub duration: u64,
14 pub year: Option<u32>,
15 pub starred: Option<String>,
16 pub is_external: bool,
17 pub octo_suggested_by: Option<String>,
18 // A documented server default, not the recommendation or catalog provider.
19 pub playback_source: Option<String>,
20}
21
22#[derive(Clone, Debug)]
23pub enum Item {
24 HomeSection(HomeSection),
25 Song(Song),
26 Album {
27 id: String,
28 name: String,
29 artist: String,
30 cover_art: Option<String>,
31 },
32 Artist {
33 id: String,
34 name: String,
35 },
36 Playlist {
37 id: String,
38 name: String,
39 },
40 Genre(String),
41 Station {
42 id: String,
43 name: String,
44 },
45}
46
47impl Item {
48 pub fn title(&self) -> &str {
49 match self {
50 Self::HomeSection(section) => section.title(),
51 Self::Song(s) => &s.title,
52 Self::Album { name, .. }
53 | Self::Artist { name, .. }
54 | Self::Playlist { name, .. }
55 | Self::Station { name, .. } => name,
56 Self::Genre(name) => name,
57 }
58 }
59 /// Prefer the server's explicit cover key, including for external discoveries.
60 pub fn cover(&self) -> Option<(String, String)> {
61 match self {
62 Self::Song(s) => Some((
63 s.cover_art
64 .as_ref()
65 .or(s.album_id.as_ref())
66 .unwrap_or(&s.id)
67 .clone(),
68 s.title.clone(),
69 )),
70 Self::Album {
71 id,
72 name,
73 cover_art,
74 ..
75 } => Some((cover_art.as_ref().unwrap_or(id).clone(), name.clone())),
76 _ => None,
77 }
78 }
79 pub fn playback_source(&self) -> &str {
80 match self {
81 Self::Song(s) if !s.is_external => "Library",
82 Self::Song(s) => s.playback_source.as_deref().unwrap_or("Unknown"),
83 _ => "—",
84 }
85 }
86 pub fn recommendation_source(&self) -> &str {
87 match self {
88 Self::Song(s) => s
89 .octo_suggested_by
90 .as_deref()
91 .filter(|s| !s.trim().is_empty())
92 .unwrap_or(if s.is_external { "Unknown" } else { "—" }),
93 _ => "—",
94 }
95 }
96 pub fn detail(&self) -> String {
97 match self {
98 Self::HomeSection(section) => section.description().into(),
99 Self::Song(s) => format!("{} · {}", s.artist, s.album),
100 Self::Album { artist, .. } => artist.clone(),
101 Self::Artist { .. } => "Artist → tracks".into(),
102 Self::Playlist { .. } => "Playlist → tracks".into(),
103 Self::Station { .. } => "Octo station → tracks".into(),
104 Self::Genre(_) => "Genre → tracks".into(),
105 }
106 }
107 pub fn label(&self) -> &str {
108 match self {
109 Self::HomeSection(_) => "BROWSE",
110 Self::Song(s) if s.is_external => "DISCOVER",
111 Self::Song(s) if s.starred.is_some() => "♥",
112 Self::Song(_) => "LIBRARY",
113 Self::Album { .. } => "ALBUM",
114 Self::Artist { .. } => "ARTIST",
115 Self::Playlist { .. } => "LIST",
116 Self::Genre(_) => "GENRE",
117 Self::Station { .. } => "STATION",
118 }
119 }
120}
121
122#[derive(Clone, Debug)]
123pub enum View {
124 Home,
125 Albums { kind: String, offset: usize },
126 Artists,
127 Songs { offset: usize },
128 History,
129 Playlists,
130 Genres,
131 Discover,
132 Stations,
133 Search(String),
134 Album(String),
135 Artist(String),
136 ArtistByName(String),
137 Playlist(String),
138 Genre(String),
139 Radio(String),
140 Station(String),
141 Queue,
142}
143
144impl View {
145 pub fn title(&self) -> String {
146 match self {
147 Self::Albums { kind, offset } => format!(
148 "Albums · {} · page {}",
149 album_order_title(kind),
150 offset / 100 + 1
151 ),
152 Self::Songs { offset } => format!("Songs · page {}", offset / 100 + 1),
153 Self::Search(q) => format!("Search · {q}"),
154 Self::Album(_) => "Album tracks".into(),
155 Self::Artist(_) => "Artist tracks".into(),
156 Self::ArtistByName(name) => format!("Artist tracks · {name}"),
157 Self::Playlist(_) => "Playlist tracks".into(),
158 Self::Genre(g) => format!("Genre · {g}"),
159 Self::Radio(_) => "Song radio · similar tracks".into(),
160 Self::Station(_) => "Station tracks".into(),
161 other => format!("{other:?}"),
162 }
163 }
164}
165
166#[derive(Clone, Copy, Debug)]
167pub enum HomeSection {
168 RecentlyPlayed,
169 RecentlyAdded,
170 MostPlayed,
171 Rediscover,
172 HighestRated,
173 History,
174 Discover,
175 Stations,
176}
177
178impl HomeSection {
179 pub const ALL: [Self; 8] = [
180 Self::RecentlyPlayed,
181 Self::RecentlyAdded,
182 Self::MostPlayed,
183 Self::Rediscover,
184 Self::HighestRated,
185 Self::History,
186 Self::Discover,
187 Self::Stations,
188 ];
189 pub fn title(self) -> &'static str {
190 match self {
191 Self::RecentlyPlayed => "Recently played",
192 Self::RecentlyAdded => "Recently added",
193 Self::MostPlayed => "Most played",
194 Self::Rediscover => "Rediscover",
195 Self::HighestRated => "Highest rated",
196 Self::History => "History",
197 Self::Discover => "Discover new music",
198 Self::Stations => "Stations",
199 }
200 }
201 pub fn description(self) -> &'static str {
202 match self {
203 Self::RecentlyPlayed => "Albums you listened to most recently",
204 Self::RecentlyAdded => "The newest additions to your library",
205 Self::MostPlayed => "Your most frequently played albums",
206 Self::Rediscover => "A random selection from your library",
207 Self::HighestRated => "Albums with the highest server ratings",
208 Self::History => "Songs played in this player, newest first",
209 Self::Discover => "Octo charts or a library shuffle",
210 Self::Stations => "Explore your server's Octo stations",
211 }
212 }
213 /// Reuse paginated album browsing and existing discovery views for each shelf.
214 pub fn view(self) -> View {
215 let kind = match self {
216 Self::RecentlyPlayed => "recent",
217 Self::RecentlyAdded => "newest",
218 Self::MostPlayed => "frequent",
219 Self::Rediscover => "random",
220 Self::HighestRated => "highest",
221 Self::History => return View::History,
222 Self::Discover => return View::Discover,
223 Self::Stations => return View::Stations,
224 };
225 View::Albums {
226 kind: kind.into(),
227 offset: 0,
228 }
229 }
230}
231
232/// Home is available immediately, even while the server is offline.
233pub fn home_items() -> Vec<Item> {
234 HomeSection::ALL
235 .into_iter()
236 .map(Item::HomeSection)
237 .collect()
238}
239
240fn album_order_title(kind: &str) -> &str {
241 match kind {
242 "recent" => "Recently played",
243 "newest" => "Recently added",
244 "frequent" => "Most played",
245 "random" => "Rediscover",
246 "highest" => "Highest rated",
247 other => other,
248 }
249}
250
251pub fn time(seconds: u64) -> String {
252 format!("{}:{:02}", seconds / 60, seconds % 60)
253}