From 333ed1324ec3e450659bae36c7650b321c593ac1 Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Thu, 26 Jan 2023 19:07:46 +0100
Subject: [PATCH] RUST: The cache will also be saved -> don't need to import
 all the overrides at each launch

---
 src/rust/amalib/src/db_cache.rs    | 19 +++++++++++++++++--
 src/rust/amalib/src/db_kara.rs     | 10 +++++-----
 src/rust/amalib/src/db_playlist.rs |  2 +-
 src/rust/amalib/src/db_tags.rs     |  2 +-
 4 files changed, 24 insertions(+), 9 deletions(-)

diff --git a/src/rust/amalib/src/db_cache.rs b/src/rust/amalib/src/db_cache.rs
index e51ba7ed..1ad263d4 100644
--- a/src/rust/amalib/src/db_cache.rs
+++ b/src/rust/amalib/src/db_cache.rs
@@ -9,15 +9,17 @@ use lru::LruCache;
 /// cache, if its the playlists that are updated, we clear only the playlist, if
 /// it's the database, we clear everything.
 #[allow(dead_code)]
-#[derive(Debug)]
+#[derive(Debug, Serialize, Deserialize)]
 pub struct AmaDB {
     /// Cache requests to lektord and patched karas. The first element is the
     /// lektord entry, the second one is the patched version.
     /// TODO: Find a structure that does COW to not duplicate too much
     ///       information in the patched version...
+    #[serde(skip, default = "new_empty_kara_lru")]
     karas: LruCache<i64, (KaraInfo, Option<KaraInfo>)>,
 
     /// The playlists. We don't save them...
+    #[serde(skip, default = "new_empty_playlist_lru")]
     playlists: LruCache<SmallString, Playlist>,
 
     /// The list of overrides, indexed by the local id of the kara.
@@ -53,9 +55,22 @@ fn new_empty_playlist_lru() -> LruCache<SmallString, Playlist> {
     )
 }
 
+impl Default for AmaDB {
+    fn default() -> Self {
+        Self {
+            karas: new_empty_kara_lru(),
+            playlists: new_empty_playlist_lru(),
+            overrides: Default::default(),
+            local_ids: Default::default(),
+        }
+    }
+}
+
 impl AmaDB {
     /// Create a new empty [AmaDB].
-    pub fn new() -> Self {}
+    pub fn new() -> Self {
+        Default::default()
+    }
 
     /// Clear the database cache, and only the caches about karas.
     pub fn clear_database_cache(&mut self) {
diff --git a/src/rust/amalib/src/db_kara.rs b/src/rust/amalib/src/db_kara.rs
index d635786b..4aaffc1d 100644
--- a/src/rust/amalib/src/db_kara.rs
+++ b/src/rust/amalib/src/db_kara.rs
@@ -4,7 +4,7 @@ use crate::*;
 
 /// Represent the possible types for a song. The custom field is here for
 /// flexibility, we box it so it's not too big and because it won't be common.
-#[derive(Debug, Clone, PartialEq, Eq)]
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
 pub enum SongType {
     OP,
     ED,
@@ -16,7 +16,7 @@ pub enum SongType {
 
 /// Represent the possible origins for a song. The custom field is here for
 /// flexibility, we box it so it's not too big and because it won't be common.
-#[derive(Debug, Clone, PartialEq, Eq)]
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
 pub enum SongOrigin {
     Anime,
     VN,
@@ -27,7 +27,7 @@ pub enum SongOrigin {
 }
 
 /// Represent a global id for a kara, the pair (database, id).
-#[derive(Debug, Getters, CopyGetters)]
+#[derive(Debug, Getters, CopyGetters, Serialize, Deserialize)]
 pub struct KaraId {
     #[getset(get = "pub")]
     repo: SmallString,
@@ -38,7 +38,7 @@ pub struct KaraId {
 
 /// A list of informations about a kara. This is the data from lektor. When
 /// queried we patch it with the overrides given by the user.
-#[derive(Debug, Getters)]
+#[derive(Debug, Getters, Serialize, Deserialize)]
 #[getset(get = "pub")]
 pub struct KaraInfo {
     song_title: SmallString,
@@ -107,7 +107,7 @@ impl<'a> TaggedObject<'a> for KaraInfo {
 }
 
 /// Represent what we want to override in a kara.
-#[derive(Debug, Clone)]
+#[derive(Debug, Clone, Serialize, Deserialize)]
 pub enum KaraInfoOverride {
     SongTitle(SmallString),
     SongSource(SmallString),
diff --git a/src/rust/amalib/src/db_playlist.rs b/src/rust/amalib/src/db_playlist.rs
index c50d6fcc..696f8c9f 100644
--- a/src/rust/amalib/src/db_playlist.rs
+++ b/src/rust/amalib/src/db_playlist.rs
@@ -2,7 +2,7 @@
 
 use crate::*;
 
-#[derive(Debug, Getters)]
+#[derive(Debug, Getters, Serialize, Deserialize)]
 #[getset(get = "pub")]
 pub struct Playlist {
     name: SmallString,
diff --git a/src/rust/amalib/src/db_tags.rs b/src/rust/amalib/src/db_tags.rs
index c04e6d47..a586f614 100644
--- a/src/rust/amalib/src/db_tags.rs
+++ b/src/rust/amalib/src/db_tags.rs
@@ -164,7 +164,7 @@ impl<'a> TaggedObject<'a> for SimpleTagSystem {
     }
 }
 
-#[derive(Debug, Clone)]
+#[derive(Debug, Clone, Serialize, Deserialize)]
 /// Represents a patch action to do on a tagged object.
 pub enum TagOverride<TK: AsRef<str> + Clone, TV: AsRef<str>> {
     /// Remove all tags.
-- 
GitLab