Skip to main content

suno_core/
manifest.rs

1//! The on-disk manifest: the engine's record of prior download state.
2//!
3//! The manifest is the prior on the reconcile engine: it records, per clip id,
4//! where the file lives, its format, the content hashes used to detect tag and
5//! art drift, its size, and the state of each external sidecar artifact. The CLI
6//! loads and saves it; this module only models it and provides pure helpers. It
7//! is unversioned: serde round-trips it to a flat JSON object keyed by clip id
8//! with no envelope.
9
10use std::collections::BTreeMap;
11use std::collections::btree_map::Iter;
12
13use serde::{Deserialize, Serialize};
14
15use crate::config::AudioFormat;
16
17/// The prior known state of one external sidecar artifact for a clip.
18///
19/// Records where the sidecar lives and a hash of the content or source it was
20/// rendered from, so a later reconcile can detect drift and trigger a rewrite.
21#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
22#[serde(default)]
23pub struct ArtifactState {
24    /// Relative path of the sidecar file under the account root.
25    pub path: String,
26    /// Content/source change hash; a change triggers a rewrite.
27    pub hash: String,
28}
29
30/// One manifest record: the prior known state of a single downloaded clip.
31#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
32#[serde(default)]
33pub struct ManifestEntry {
34    /// Relative path of the audio file under the account root.
35    pub path: String,
36    /// Format the file was written in.
37    pub format: AudioFormat,
38    /// Hash of the clip's tag-bearing metadata, for detecting retag needs.
39    pub meta_hash: String,
40    /// Hash of the embedded cover art, for detecting art drift.
41    pub art_hash: String,
42    /// Size of the file in bytes when last written.
43    pub size: u64,
44    /// When set, this clip is held by a copy or archive source, or is private,
45    /// so it must never be deleted as an orphan no matter the current selection.
46    /// The caller writes this marker; the reconcile engine only reads it.
47    pub preserve: bool,
48    /// Prior state of the external `cover.jpg` sidecar, when one was written.
49    #[serde(default)]
50    pub cover_jpg: Option<ArtifactState>,
51    /// Prior state of the external `cover.webp` sidecar, when one was written.
52    #[serde(default)]
53    pub cover_webp: Option<ArtifactState>,
54    /// Prior state of the plain-text `.details.txt` sidecar, when one was written.
55    #[serde(default)]
56    pub details_txt: Option<ArtifactState>,
57    /// Prior state of the plain-text `.lyrics.txt` sidecar, when one was written.
58    #[serde(default)]
59    pub lyrics_txt: Option<ArtifactState>,
60    /// Prior state of the untimed `.lrc` sidecar, when one was written.
61    #[serde(default)]
62    pub lrc: Option<ArtifactState>,
63    /// Prior state of the standalone `.mp4` music video, when one was written.
64    #[serde(default)]
65    pub video_mp4: Option<ArtifactState>,
66}
67
68/// The full prior download state, keyed by clip id.
69///
70/// Backed by a [`BTreeMap`] so iteration order is stable, which keeps any plan
71/// derived from it deterministic.
72#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
73#[serde(transparent)]
74pub struct Manifest {
75    /// Records keyed by clip id.
76    pub entries: BTreeMap<String, ManifestEntry>,
77}
78
79impl Manifest {
80    /// Create an empty manifest.
81    pub fn new() -> Self {
82        Self::default()
83    }
84
85    /// Return the entry for `clip_id`, if present.
86    pub fn get(&self, clip_id: &str) -> Option<&ManifestEntry> {
87        self.entries.get(clip_id)
88    }
89
90    /// Insert or replace the entry for `clip_id`, returning any prior value.
91    pub fn insert(
92        &mut self,
93        clip_id: impl Into<String>,
94        entry: ManifestEntry,
95    ) -> Option<ManifestEntry> {
96        self.entries.insert(clip_id.into(), entry)
97    }
98
99    /// Remove and return the entry for `clip_id`, if present.
100    pub fn remove(&mut self, clip_id: &str) -> Option<ManifestEntry> {
101        self.entries.remove(clip_id)
102    }
103
104    /// Return true when an entry exists for `clip_id`.
105    pub fn contains(&self, clip_id: &str) -> bool {
106        self.entries.contains_key(clip_id)
107    }
108
109    /// Iterate entries in clip-id order.
110    pub fn iter(&self) -> Iter<'_, String, ManifestEntry> {
111        self.entries.iter()
112    }
113
114    /// Number of entries.
115    pub fn len(&self) -> usize {
116        self.entries.len()
117    }
118
119    /// True when there are no entries.
120    pub fn is_empty(&self) -> bool {
121        self.entries.is_empty()
122    }
123}
124
125#[cfg(test)]
126mod tests {
127    use super::*;
128
129    fn entry(path: &str, format: AudioFormat) -> ManifestEntry {
130        ManifestEntry {
131            path: path.to_string(),
132            format,
133            meta_hash: "m".to_string(),
134            art_hash: "a".to_string(),
135            size: 42,
136            preserve: false,
137            ..Default::default()
138        }
139    }
140
141    #[test]
142    fn new_is_empty() {
143        let m = Manifest::new();
144        assert!(m.is_empty());
145        assert_eq!(m.len(), 0);
146    }
147
148    #[test]
149    fn insert_get_contains() {
150        let mut m = Manifest::new();
151        assert!(m.insert("a", entry("a.flac", AudioFormat::Flac)).is_none());
152        assert!(m.contains("a"));
153        assert_eq!(m.get("a").unwrap().path, "a.flac");
154        assert_eq!(m.len(), 1);
155        assert!(!m.is_empty());
156    }
157
158    #[test]
159    fn insert_replaces_and_returns_prior() {
160        let mut m = Manifest::new();
161        m.insert("a", entry("a.flac", AudioFormat::Flac));
162        let prior = m.insert("a", entry("a.mp3", AudioFormat::Mp3));
163        assert_eq!(prior.unwrap().path, "a.flac");
164        assert_eq!(m.get("a").unwrap().format, AudioFormat::Mp3);
165        assert_eq!(m.len(), 1);
166    }
167
168    #[test]
169    fn remove_returns_prior_then_absent() {
170        let mut m = Manifest::new();
171        m.insert("a", entry("a.flac", AudioFormat::Flac));
172        let removed = m.remove("a");
173        assert_eq!(removed.unwrap().path, "a.flac");
174        assert!(!m.contains("a"));
175        assert!(m.remove("a").is_none());
176    }
177
178    #[test]
179    fn get_absent_is_none() {
180        let m = Manifest::new();
181        assert!(m.get("missing").is_none());
182    }
183
184    #[test]
185    fn iter_is_clip_id_sorted() {
186        let mut m = Manifest::new();
187        m.insert("c", entry("c.flac", AudioFormat::Flac));
188        m.insert("a", entry("a.flac", AudioFormat::Flac));
189        m.insert("b", entry("b.flac", AudioFormat::Flac));
190        let ids: Vec<&str> = m.iter().map(|(id, _)| id.as_str()).collect();
191        assert_eq!(ids, ["a", "b", "c"]);
192    }
193
194    #[test]
195    fn serde_roundtrip_preserves_entries() {
196        let mut m = Manifest::new();
197        m.insert("a", entry("a.flac", AudioFormat::Flac));
198        m.insert("b", entry("b.mp3", AudioFormat::Mp3));
199        // An entry carrying every sidecar artifact must round-trip intact.
200        let mut c = entry("c.flac", AudioFormat::Flac);
201        c.cover_jpg = Some(ArtifactState {
202            path: "c/cover.jpg".to_string(),
203            hash: "jpg-hash".to_string(),
204        });
205        c.cover_webp = Some(ArtifactState {
206            path: "c/cover.webp".to_string(),
207            hash: "webp-hash".to_string(),
208        });
209        c.details_txt = Some(ArtifactState {
210            path: "c.details.txt".to_string(),
211            hash: "details-hash".to_string(),
212        });
213        c.lyrics_txt = Some(ArtifactState {
214            path: "c.lyrics.txt".to_string(),
215            hash: "lyrics-hash".to_string(),
216        });
217        c.lrc = Some(ArtifactState {
218            path: "c.lrc".to_string(),
219            hash: "lrc-hash".to_string(),
220        });
221        m.insert("c", c);
222        let json = serde_json::to_string(&m).unwrap();
223        let back: Manifest = serde_json::from_str(&json).unwrap();
224        assert_eq!(m, back);
225    }
226
227    #[test]
228    fn serde_is_unversioned_flat_object() {
229        let mut m = Manifest::new();
230        m.insert("clip1", entry("song.flac", AudioFormat::Flac));
231        let value: serde_json::Value = serde_json::to_value(&m).unwrap();
232        // Top level is the clip-id map itself, with no envelope or version key.
233        assert!(value.is_object());
234        assert!(value.get("entries").is_none());
235        assert!(value.get("version").is_none());
236        let entry = value.get("clip1").unwrap();
237        assert_eq!(entry.get("format").unwrap(), "flac");
238        assert_eq!(entry.get("path").unwrap(), "song.flac");
239    }
240
241    #[test]
242    fn empty_manifest_roundtrips() {
243        let m = Manifest::new();
244        let json = serde_json::to_string(&m).unwrap();
245        assert_eq!(json, "{}");
246        let back: Manifest = serde_json::from_str(&json).unwrap();
247        assert!(back.is_empty());
248    }
249
250    #[test]
251    fn unicode_and_reserved_ids_roundtrip() {
252        let mut m = Manifest::new();
253        m.insert("ünïcode-🎵", entry("音楽.flac", AudioFormat::Flac));
254        m.insert("with\"quote", entry("a.flac", AudioFormat::Flac));
255        let json = serde_json::to_string(&m).unwrap();
256        let back: Manifest = serde_json::from_str(&json).unwrap();
257        assert_eq!(m, back);
258        assert!(back.contains("ünïcode-🎵"));
259    }
260
261    #[test]
262    fn default_format_deserialises_when_absent() {
263        // A record missing the format key falls back to the compiled default.
264        let json = r#"{"clip1":{"path":"a.flac","meta_hash":"","art_hash":"","size":0}}"#;
265        let m: Manifest = serde_json::from_str(json).unwrap();
266        assert_eq!(m.get("clip1").unwrap().format, AudioFormat::default());
267    }
268
269    #[test]
270    fn preserve_defaults_to_false_when_absent() {
271        // Older manifests written before the marker existed must load as not
272        // preserved, so the field is purely additive.
273        let json =
274            r#"{"clip1":{"path":"a.flac","format":"flac","meta_hash":"","art_hash":"","size":1}}"#;
275        let m: Manifest = serde_json::from_str(json).unwrap();
276        assert!(!m.get("clip1").unwrap().preserve);
277    }
278
279    #[test]
280    fn preserve_roundtrips() {
281        let mut m = Manifest::new();
282        let mut e = entry("a.flac", AudioFormat::Flac);
283        e.preserve = true;
284        m.insert("a", e);
285        let json = serde_json::to_string(&m).unwrap();
286        let back: Manifest = serde_json::from_str(&json).unwrap();
287        assert!(back.get("a").unwrap().preserve);
288        assert_eq!(m, back);
289    }
290
291    #[test]
292    fn cover_artifacts_default_to_none_when_absent() {
293        // A pre-growth manifest, written before the sidecar fields existed, must
294        // load with no artifacts and unpreserved, proving the growth is purely
295        // additive and backwards compatible.
296        let json = r#"{"clip1":{"path":"a.flac","format":"flac","meta_hash":"m","art_hash":"a","size":1}}"#;
297        let m: Manifest = serde_json::from_str(json).unwrap();
298        let e = m.get("clip1").unwrap();
299        assert_eq!(e.cover_jpg, None);
300        assert_eq!(e.cover_webp, None);
301        assert_eq!(e.details_txt, None);
302        assert_eq!(e.lyrics_txt, None);
303        assert_eq!(e.lrc, None);
304        assert!(!e.preserve);
305    }
306
307    #[test]
308    fn artifact_state_defaults_and_roundtrips() {
309        let empty = ArtifactState::default();
310        assert_eq!(empty.path, "");
311        assert_eq!(empty.hash, "");
312        let json = serde_json::to_string(&empty).unwrap();
313        let back: ArtifactState = serde_json::from_str(&json).unwrap();
314        assert_eq!(empty, back);
315
316        let populated = ArtifactState {
317            path: "x/cover.webp".to_string(),
318            hash: "content-hash".to_string(),
319        };
320        let json = serde_json::to_string(&populated).unwrap();
321        let back: ArtifactState = serde_json::from_str(&json).unwrap();
322        assert_eq!(populated, back);
323    }
324}