use compact_str::CompactString;
use indexmap::IndexMap;
use serde::{
Deserialize,
Serialize,
};
use serde_with::skip_serializing_none;
use crate::utils::{
build_full_text,
normalize_line_text,
normalize_words_spaces,
strip_outer_parens,
strip_outer_parens_from_words,
};
#[skip_serializing_none]
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SubLyricContent {
pub language: Option<CompactString>,
pub automatically_created: Option<bool>,
pub text: String,
pub words: Option<Vec<Syllable>>,
}
#[skip_serializing_none]
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
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, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
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<CompactString>,
pub agent_id: Option<CompactString>,
pub song_part: Option<CompactString>,
pub block_index: Option<u32>,
}
impl LyricLine {
pub fn push_word(&mut self, syllable: Syllable) {
self.words
.get_or_insert_with(|| Vec::with_capacity(12))
.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)
}
pub fn rebuild_text(&mut self) {
if let Some(words) = &self.words {
self.text = build_full_text(words, false);
}
}
}
impl BackgroundVocal {
pub fn push_word(&mut self, syllable: Syllable) {
self.words
.get_or_insert_with(|| Vec::with_capacity(12))
.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 rebuild_text(&mut self) {
if let Some(words) = &self.words {
self.text = build_full_text(words, false);
}
}
pub fn normalize(&mut self) {
if let Some(bg_words) = &mut self.words {
strip_outer_parens_from_words(bg_words);
normalize_words_spaces(bg_words);
self.text = build_full_text(bg_words, false);
} else {
strip_outer_parens(&mut self.text);
normalize_line_text(&mut self.text);
}
if let Some(translations) = &mut self.translations {
for t in translations {
t.normalize_with_parens(false);
}
}
if let Some(romanizations) = &mut self.romanizations {
for r in romanizations {
r.normalize_with_parens(true);
}
}
}
}
impl Syllable {
pub fn push_ruby(&mut self, tag: RubyTag) {
self.ruby.get_or_insert_with(Vec::new).push(tag);
}
}
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RubyTag {
pub text: CompactString,
pub start_time: u32,
pub end_time: u32,
}
#[skip_serializing_none]
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Syllable {
pub text: CompactString,
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, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Agent {
pub id: CompactString,
pub name: Option<CompactString>,
pub type_: Option<CompactString>,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum PlatformId {
NcmMusicId,
QqMusicId,
SpotifyId,
AppleMusicId,
}
#[skip_serializing_none]
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AudioMetadata {
pub duration: Option<u32>,
pub lyric_offset: Option<i32>,
pub role: Option<CompactString>,
pub leading_silence: Option<u32>,
}
impl AudioMetadata {
#[must_use]
pub const fn is_empty(&self) -> bool {
self.duration.is_none()
&& self.lyric_offset.is_none()
&& self.role.is_none()
&& self.leading_silence.is_none()
}
}
#[skip_serializing_none]
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TTMLMetadata {
pub language: Option<CompactString>,
pub timing_mode: Option<CompactString>,
pub audio: Option<AudioMetadata>,
pub songwriters: Option<Vec<CompactString>>,
pub title: Option<Vec<CompactString>>,
pub artist: Option<Vec<CompactString>>,
pub album: Option<Vec<CompactString>>,
pub isrc: Option<Vec<CompactString>>,
pub author_ids: Option<Vec<CompactString>>,
pub author_names: Option<Vec<CompactString>>,
pub agents: Option<IndexMap<CompactString, Agent>>,
pub platform_ids: Option<IndexMap<PlatformId, Vec<CompactString>>>,
pub raw_properties: Option<IndexMap<CompactString, Vec<CompactString>>>,
}
impl TTMLMetadata {
pub fn audio_mut(&mut self) -> &mut AudioMetadata {
self.audio.get_or_insert_with(AudioMetadata::default)
}
pub fn push_title(&mut self, title: CompactString) {
self.title.get_or_insert_with(Vec::new).push(title);
}
pub fn push_artist(&mut self, artist: CompactString) {
self.artist.get_or_insert_with(Vec::new).push(artist);
}
pub fn push_album(&mut self, album: CompactString) {
self.album.get_or_insert_with(Vec::new).push(album);
}
pub fn push_songwriter(&mut self, songwriter: CompactString) {
self.songwriters
.get_or_insert_with(Vec::new)
.push(songwriter);
}
pub fn push_isrc(&mut self, isrc: CompactString) {
self.isrc.get_or_insert_with(Vec::new).push(isrc);
}
pub fn push_author_id(&mut self, author_id: CompactString) {
self.author_ids.get_or_insert_with(Vec::new).push(author_id);
}
pub fn push_author_name(&mut self, author_name: CompactString) {
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: CompactString) {
self.platform_ids
.get_or_insert_with(IndexMap::new)
.entry(platform)
.or_default()
.push(id);
}
pub fn push_raw_property(&mut self, key: CompactString, value: CompactString) {
self.raw_properties
.get_or_insert_with(IndexMap::new)
.entry(key)
.or_default()
.push(value);
}
}
impl SubLyricContent {
pub fn rebuild_text(&mut self) {
if let Some(words) = &self.words {
self.text = build_full_text(words, false);
}
}
pub fn normalize(&mut self, space_joined: bool) {
if let Some(words) = &mut self.words {
normalize_words_spaces(words);
self.text = build_full_text(words, space_joined);
} else {
normalize_line_text(&mut self.text);
}
}
pub fn normalize_with_parens(&mut self, space_joined: bool) {
if let Some(words) = &mut self.words {
strip_outer_parens_from_words(words);
normalize_words_spaces(words);
self.text = build_full_text(words, space_joined);
} else {
strip_outer_parens(&mut self.text);
normalize_line_text(&mut self.text);
}
}
}
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TTMLResult {
pub metadata: TTMLMetadata,
pub lines: Vec<LyricLine>,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_core_structs_equality() {
let res1 = TTMLResult {
metadata: TTMLMetadata {
language: Some("zh-CN".into()),
..Default::default()
},
lines: vec![LyricLine {
text: "测试".into(),
start_time: 100,
end_time: 200,
words: Some(vec![Syllable {
text: "测".into(),
start_time: 100,
end_time: 150,
..Default::default()
}]),
..Default::default()
}],
};
let res2 = res1.clone();
assert_eq!(res1, res2);
let mut res3 = res1.clone();
res3.lines[0].end_time = 300;
assert_ne!(res1, res3);
}
}