use std::fmt;
use serde::{Deserialize, Serialize};
use crate::library::metadata::{INSTRUMENTAL_KEY, LYRIC_KEY};
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub enum LyricData {
Instrumental,
Plain(String),
Synced(String),
}
impl LyricData {
pub fn infer_from_string(string: impl Into<String>) -> LyricData {
let string = string.into();
if &string == "[au: instrumental]" {
return LyricData::Instrumental;
}
let regex = regex::Regex::new(r"^\[\d{2}:\d{2}(\.\d{2,3})?\]").unwrap();
let total_lines = string.lines().count();
let timestamped_lines = string.lines().filter(|line| regex.is_match(line)).count();
if timestamped_lines * 2 > total_lines {
LyricData::Synced(string)
} else {
LyricData::Plain(string)
}
}
#[must_use]
pub fn get_metadata_value(&self) -> (&'static str, String) {
match self {
LyricData::Instrumental => (INSTRUMENTAL_KEY, "1".to_owned()),
LyricData::Plain(lyrics) | LyricData::Synced(lyrics) => (LYRIC_KEY, lyrics.to_owned()),
}
}
}
impl fmt::Display for LyricData {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
LyricData::Instrumental => write!(f, "instrumental"),
LyricData::Plain(lyrics) => write!(f, "plain: {{{lyrics}}}"),
LyricData::Synced(lyrics) => write!(f, "synced: {{{lyrics}}}"),
}
}
}