Deserialize the AppConfig from storage, handling upgrades from older versions.
(storage: &dyn Storage)
| 24 | |
| 25 | /// Deserialize the AppConfig from storage, handling upgrades from older versions. |
| 26 | pub fn deserialize_config(storage: &dyn Storage) -> Option<AppConfig> { |
| 27 | let str = storage.get_string(CONFIG_KEY)?; |
| 28 | match ron::from_str::<AppConfigVersion>(&str) { |
| 29 | Ok(version) => match version.version { |
| 30 | 3 => from_str::<AppConfig>(&str), |
| 31 | 2 => from_str::<AppConfigV2>(&str).map(|c| c.into_config()), |
| 32 | 1 => from_str::<AppConfigV1>(&str).map(|c| c.into_config()), |
| 33 | _ => { |
| 34 | log::warn!("Unknown config version: {}", version.version); |
| 35 | None |
| 36 | } |
| 37 | }, |
| 38 | Err(e) => { |
| 39 | log::warn!("Failed to decode config version: {e}"); |
| 40 | // Try to decode as v0 |
| 41 | from_str::<AppConfigV0>(&str).map(|c| c.into_config()) |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | fn from_str<T>(str: &str) -> Option<T> |
| 47 | where T: serde::de::DeserializeOwned { |