use indexmap::IndexMap;
use serde::{
Deserialize,
Serialize,
};
use serde_with::skip_serializing_none;
#[skip_serializing_none]
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct SubLyricContent {
pub language: Option<String>,
pub text: String,
pub words: Option<Vec<Syllable>>,
}
#[skip_serializing_none]
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct BackgroundVocal {
pub text: String,
pub start_time: u32,
pub end_time: u32,
pub words: Option<Vec<Syllable>>,
pub translations: Option<Vec<SubLyricContent>>,
pub romanizations: Option<Vec<SubLyricContent>>,
}
#[skip_serializing_none]
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct LyricLine {
pub text: String,
pub start_time: u32,
pub end_time: u32,
pub words: Option<Vec<Syllable>>,
pub translations: Option<Vec<SubLyricContent>>,
pub romanizations: Option<Vec<SubLyricContent>>,
pub background_vocal: Option<BackgroundVocal>,
pub id: Option<String>,
pub agent_id: Option<String>,
pub song_part: Option<String>,
pub block_index: Option<u32>,
}
impl LyricLine {
pub fn push_word(&mut self, syllable: Syllable) {
self.words.get_or_insert_with(Vec::new).push(syllable);
}
pub fn push_translation(&mut self, content: SubLyricContent) {
self.translations.get_or_insert_with(Vec::new).push(content);
}
pub fn push_romanization(&mut self, content: SubLyricContent) {
self.romanizations
.get_or_insert_with(Vec::new)
.push(content);
}
pub fn bg_vocal_mut(&mut self) -> &mut BackgroundVocal {
self.background_vocal
.get_or_insert_with(BackgroundVocal::default)
}
}
impl BackgroundVocal {
pub fn push_word(&mut self, syllable: Syllable) {
self.words.get_or_insert_with(Vec::new).push(syllable);
}
pub fn push_translation(&mut self, content: SubLyricContent) {
self.translations.get_or_insert_with(Vec::new).push(content);
}
pub fn push_romanization(&mut self, content: SubLyricContent) {
self.romanizations
.get_or_insert_with(Vec::new)
.push(content);
}
}
impl Syllable {
pub fn push_ruby(&mut self, tag: RubyTag) {
self.ruby.get_or_insert_with(Vec::new).push(tag);
}
}
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct RubyTag {
pub text: String,
pub start_time: u32,
pub end_time: u32,
}
#[skip_serializing_none]
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct Syllable {
pub text: String,
pub start_time: u32,
pub end_time: u32,
pub ends_with_space: Option<bool>,
pub ruby: Option<Vec<RubyTag>>,
pub obscene: Option<bool>,
pub empty_beat: Option<u32>,
}
#[skip_serializing_none]
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct Agent {
pub id: String,
pub name: Option<String>,
pub type_: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PlatformId {
NcmMusicId,
QqMusicId,
SpotifyId,
AppleMusicId,
}
#[skip_serializing_none]
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct TTMLMetadata {
pub language: Option<String>,
pub timing_mode: Option<String>,
pub songwriters: Option<Vec<String>>,
pub title: Option<Vec<String>>,
pub artist: Option<Vec<String>>,
pub album: Option<Vec<String>>,
pub isrc: Option<Vec<String>>,
pub author_ids: Option<Vec<String>>,
pub author_names: Option<Vec<String>>,
pub agents: Option<IndexMap<String, Agent>>,
pub platform_ids: Option<IndexMap<PlatformId, Vec<String>>>,
pub raw_properties: Option<IndexMap<String, Vec<String>>>,
}
impl TTMLMetadata {
pub fn push_title(&mut self, title: String) {
self.title.get_or_insert_with(Vec::new).push(title);
}
pub fn push_artist(&mut self, artist: String) {
self.artist.get_or_insert_with(Vec::new).push(artist);
}
pub fn push_album(&mut self, album: String) {
self.album.get_or_insert_with(Vec::new).push(album);
}
pub fn push_songwriter(&mut self, songwriter: String) {
self.songwriters
.get_or_insert_with(Vec::new)
.push(songwriter);
}
pub fn push_isrc(&mut self, isrc: String) {
self.isrc.get_or_insert_with(Vec::new).push(isrc);
}
pub fn push_author_id(&mut self, author_id: String) {
self.author_ids.get_or_insert_with(Vec::new).push(author_id);
}
pub fn push_author_name(&mut self, author_name: String) {
self.author_names
.get_or_insert_with(Vec::new)
.push(author_name);
}
pub fn insert_agent(&mut self, agent: Agent) {
self.agents
.get_or_insert_with(IndexMap::new)
.insert(agent.id.clone(), agent);
}
pub fn push_platform_id(&mut self, platform: PlatformId, id: String) {
self.platform_ids
.get_or_insert_with(IndexMap::new)
.entry(platform)
.or_default()
.push(id);
}
pub fn push_raw_property(&mut self, key: String, value: String) {
self.raw_properties
.get_or_insert_with(IndexMap::new)
.entry(key)
.or_default()
.push(value);
}
}
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct TTMLResult {
pub metadata: TTMLMetadata,
pub lines: Vec<LyricLine>,
}