//! Changing the library (likes, dislikes, playlists), lyrics, and the //! server-side play queue that other devices can continue from. use std::collections::HashSet; use std::time::Duration; use crate::library::PlaylistId; /// What the user has liked or disliked, by id, for drawing hearts and bans. #[derive(Debug, Clone, Default, PartialEq, Eq)] pub struct LikedIds { pub albums: HashSet, pub artists: HashSet, pub playlists: HashSet, pub disliked_tracks: HashSet, pub clips: HashSet, } /// One line of lyrics; synced lyrics carry the moment it's sung. #[derive(Debug, Clone, PartialEq, Eq)] pub struct LyricLine { pub at: Option, pub text: String, } #[derive(Debug, Clone, PartialEq, Eq)] pub struct Lyrics { pub lines: Vec, pub synced: bool, pub writers: Vec, } impl Lyrics { /// The line being sung at `position`: the last line that has started. pub fn current(&self, position: Duration) -> Option { if !self.synced { return None; } self.lines.iter().rposition(|l| l.at.is_some_and(|at| at <= position)) } /// Plain text: one line per line, blank lines kept as paragraph breaks. pub(crate) fn from_text(text: &str, writers: Vec) -> Self { Self { lines: text.lines().map(|l| LyricLine { at: None, text: l.trim_end().to_owned() }).collect(), synced: false, writers, } } /// LRC: `[mm:ss.xx] words`. Lines with several stamps repeat; metadata tags are dropped. pub(crate) fn from_lrc(lrc: &str, writers: Vec) -> Self { let mut lines = Vec::new(); for raw in lrc.lines() { let mut rest = raw.trim(); let mut stamps = Vec::new(); while let Some(stripped) = rest.strip_prefix('[') { let Some(end) = stripped.find(']') else { break }; match parse_stamp(&stripped[..end]) { Some(at) => stamps.push(at), // `[ar:Artist]` and friends: not a lyric line. None => { stamps.clear(); rest = ""; break; } } rest = stripped[end + 1..].trim_start(); } for at in stamps { lines.push(LyricLine { at: Some(at), text: rest.to_owned() }); } } lines.sort_by_key(|l| l.at); Self { lines, synced: true, writers } } } /// `mm:ss`, `mm:ss.x`, `mm:ss.xx` or `mm:ss.xxx`. fn parse_stamp(s: &str) -> Option { let (min, sec) = s.split_once(':')?; let min: u64 = min.trim().parse().ok()?; let (whole, frac) = sec.split_once('.').unwrap_or((sec, "")); let whole: u64 = whole.parse().ok()?; if whole >= 60 || frac.len() > 3 || !frac.chars().all(|c| c.is_ascii_digit()) { return None; } let millis = if frac.is_empty() { 0 } else { frac.parse::().ok()? * 10u64.pow(3 - frac.len() as u32) }; Some(Duration::from_millis((min * 60 + whole) * 1000 + millis)) } #[cfg(test)] mod tests { use super::*; #[test] fn lrc_parses_stamps_and_drops_tags() { let l = Lyrics::from_lrc( "[ar:Кино]\n[00:12.50] Первая\n[00:20.1]Вторая\n[01:02.345][00:05.00] Повтор\n\nбез метки", vec![], ); let got: Vec<(u64, &str)> = l.lines.iter().map(|x| (x.at.unwrap().as_millis() as u64, x.text.as_str())).collect(); assert_eq!(got, [(5_000, "Повтор"), (12_500, "Первая"), (20_100, "Вторая"), (62_345, "Повтор")]); assert!(l.synced); } #[test] fn current_line_follows_position() { let l = Lyrics::from_lrc("[00:10.00]a\n[00:20.00]b", vec![]); assert_eq!(l.current(Duration::from_secs(5)), None); assert_eq!(l.current(Duration::from_secs(10)), Some(0)); assert_eq!(l.current(Duration::from_secs(25)), Some(1)); let plain = Lyrics::from_text("a\nb", vec![]); assert_eq!(plain.current(Duration::from_secs(25)), None); } #[test] fn bad_stamps_are_not_lyrics() { assert_eq!(parse_stamp("01:02"), Some(Duration::from_secs(62))); assert_eq!(parse_stamp("1:75"), None); assert_eq!(parse_stamp("ti"), None); assert_eq!(parse_stamp("00:01.1234"), None); } }