Skip to main content

lyrics_core/models/
lyrics_data.rs

1use serde::{Deserialize, Serialize};
2
3use super::line_info::LineInfo;
4use super::lyrics_types::{LyricsTypes, SyncTypes};
5use super::track_metadata::TrackMetadata;
6
7/// 歌词数据的顶层容器,包含解析后的完整歌词信息。
8#[derive(Debug, Clone, Default, Serialize, Deserialize)]
9pub struct LyricsData {
10    /// 文件格式信息
11    pub file: Option<FileInfo>,
12    /// 歌词行列表
13    pub lines: Option<Vec<LineInfo>>,
14    /// 歌词作者列表
15    pub writers: Option<Vec<String>>,
16    /// 歌曲元数据(标题、艺术家等)
17    pub track_metadata: Option<TrackMetadata>,
18}
19
20/// 歌词文件的格式和同步类型信息。
21#[derive(Debug, Clone, Default, Serialize, Deserialize)]
22pub struct FileInfo {
23    /// 歌词格式类型
24    #[serde(rename = "type")]
25    pub lyrics_type: LyricsTypes,
26    /// 同步类型
27    pub sync_types: SyncTypes,
28    /// 格式相关的附加信息
29    pub additional_info: Option<AdditionalFileInfo>,
30}
31
32/// 不同歌词格式的附加文件信息。
33///
34/// 使用 tagged union 序列化,JSON 中通过 `type` 字段区分变体。
35#[derive(Debug, Clone, Serialize, Deserialize)]
36#[serde(tag = "type")]
37pub enum AdditionalFileInfo {
38    /// 通用格式附加信息(如 LRC 头部标签)
39    General {
40        /// 键值对属性列表
41        attributes: Vec<(String, String)>,
42    },
43    /// KRC 格式附加信息
44    Krc {
45        /// 键值对属性列表
46        attributes: Vec<(String, String)>,
47        /// 文件哈希值
48        hash: Option<String>,
49    },
50    /// Spotify 格式附加信息
51    Spotify {
52        /// 歌词提供商
53        provider: Option<String>,
54        /// 提供商歌词 ID
55        provider_lyrics_id: Option<String>,
56        /// 提供商显示名称
57        provider_display_name: Option<String>,
58        /// 歌词语言
59        lyrics_language: Option<String>,
60    },
61}
62
63impl AdditionalFileInfo {
64    /// 创建空的通用格式附加信息。
65    pub fn new_general() -> Self {
66        Self::General {
67            attributes: Vec::new(),
68        }
69    }
70
71    /// 创建空的 KRC 格式附加信息。
72    pub fn new_krc() -> Self {
73        Self::Krc {
74            attributes: Vec::new(),
75            hash: None,
76        }
77    }
78
79    /// 创建 Spotify 格式附加信息。
80    pub fn new_spotify(
81        provider: Option<String>,
82        provider_lyrics_id: Option<String>,
83        provider_display_name: Option<String>,
84        lyrics_language: Option<String>,
85    ) -> Self {
86        Self::Spotify {
87            provider,
88            provider_lyrics_id,
89            provider_display_name,
90            lyrics_language,
91        }
92    }
93
94    /// 获取属性列表的只读引用(仅 General 和 Krc 变体支持)。
95    pub fn attributes(&self) -> Option<&Vec<(String, String)>> {
96        match self {
97            Self::General { attributes } | Self::Krc { attributes, .. } => Some(attributes),
98            _ => None,
99        }
100    }
101
102    /// 获取属性列表的可变引用(仅 General 和 Krc 变体支持)。
103    pub fn attributes_mut(&mut self) -> Option<&mut Vec<(String, String)>> {
104        match self {
105            Self::General { attributes } | Self::Krc { attributes, .. } => Some(attributes),
106            _ => None,
107        }
108    }
109
110    /// 获取 KRC 文件哈希值(仅 Krc 变体支持)。
111    pub fn hash(&self) -> Option<&str> {
112        match self {
113            Self::Krc { hash, .. } => hash.as_deref(),
114            _ => None,
115        }
116    }
117}