From ae7810702ae6ef8020c6a823da35e8c96c3261e6 Mon Sep 17 00:00:00 2001 From: user Date: Mon, 27 Nov 2023 18:02:27 +0100 Subject: [PATCH] Initial commit for savefile migration --- backend/src/persist/migration.rs | 52 ++++++++++++++++++++++++++++++++ backend/src/persist/mod.rs | 4 ++- 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 backend/src/persist/migration.rs diff --git a/backend/src/persist/migration.rs b/backend/src/persist/migration.rs new file mode 100644 index 0000000..33f21bb --- /dev/null +++ b/backend/src/persist/migration.rs @@ -0,0 +1,52 @@ +use std::collections::HashMap; + +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize)] +pub struct OldOnEventJson { + pub on_save: Option, + pub on_load: Option, + pub on_set: Option, + pub on_resume: Option, +} + +#[derive(Serialize, Deserialize)] +pub struct OldSettingsJson { + pub version: u64, + pub name: String, + pub persistent: bool, + pub cpus: Vec, + pub gpu: super::GpuJson, + pub battery: super::BatteryJson, + pub provider: Option, + pub events: Option, +} + +impl From<&OldSettingsJson> for super::SettingsJson { + fn from(old_settings: &OldSettingsJson) -> Self { + Self { + version: old_settings.version, + name: old_settings.name.clone(), + variant: 0, + persistent: old_settings.persistent, + cpus: old_settings.cpus.clone(), + gpu: old_settings.gpu.clone(), + battery: old_settings.battery.clone(), + provider: old_settings.provider.clone(), + } + } +} + +impl From<&OldSettingsJson> for super::FileJson { + fn from(old_settings: &OldSettingsJson) -> Self { + let mut variants = HashMap::new(); + let variant = super::SettingsJson::from(old_settings); + variants.insert(0, variant); + + Self { + version: 0, + name: old_settings.name.clone(), + variants, + } + } +} diff --git a/backend/src/persist/mod.rs b/backend/src/persist/mod.rs index 4c9a31b..d991a88 100644 --- a/backend/src/persist/mod.rs +++ b/backend/src/persist/mod.rs @@ -5,6 +5,7 @@ mod error; mod file; mod general; mod gpu; +mod migration; pub use battery::{BatteryEventJson, BatteryJson}; pub use cpu::CpuJson; @@ -12,5 +13,6 @@ pub use driver::DriverJson; pub use file::FileJson; pub use general::{MinMaxJson, SettingsJson}; pub use gpu::GpuJson; +pub use migration::OldSettingsJson; -pub use error::{SerdeError, RonError}; +pub use error::{RonError, SerdeError};