Skip to main content

elemental_shared/
lib.rs

1extern crate self as elemental_shared;
2
3pub mod error;
4pub mod loader;
5pub mod migrator;
6pub mod persistor;
7pub mod profile;
8pub mod scope;
9pub mod store;
10pub mod version;
11
12#[cfg(test)]
13mod shared_test {
14    use crate::{
15        error::Result,
16        migrator::{Migrator, NoMigrator},
17        persistor::{MemoryPersistor, json_persistor},
18        profile::Profile,
19        scope::Scope,
20        version::Persistor,
21    };
22    use serde::{Deserialize, Serialize};
23
24    /// A sample configuration struct for testing purposes.
25    #[derive(Serialize, Deserialize, Clone)]
26    pub struct Config {
27        user: String,
28    }
29
30    impl Default for Config {
31        fn default() -> Self {
32            Config {
33                user: "Player".to_owned(),
34            }
35        }
36    }
37
38    type ProfileConfig = Profile<Config>;
39
40    #[tokio::test]
41    async fn test_profile() {
42        let loader = ProfileConfig::load(
43            NoMigrator,
44            json_persistor("foobar".to_string(), Scope::Dot),
45            0,
46        )
47        .await
48        .unwrap();
49
50        println!("{:?}", loader.get(|config| config.user.clone()).await); // Player
51        loader
52            .set(|config| config.user = "NewPlayer".to_owned())
53            .await
54            .unwrap();
55        println!("{:?}", loader.get(|config| config.user.clone()).await); // NewPlayer
56    }
57
58    #[tokio::test]
59    async fn v1v2v3() -> Result<()> {
60        #[derive(Serialize, Deserialize, Clone, Default)]
61        struct V1 {
62            name: String,
63        }
64        #[derive(Serialize, Deserialize, Clone, Default)]
65        struct V2 {
66            name2: String,
67        }
68        #[derive(Serialize, Deserialize, Clone, Default)]
69        struct V3 {
70            name3: String,
71        }
72
73        #[derive(Clone, Serialize, Deserialize)]
74        #[serde(untagged)]
75        enum Versions {
76            V1(V1),
77            V2(V2),
78            V3(V3),
79        }
80
81        struct ProfileMigrator;
82
83        impl Migrator<Profile<Versions>, Profile<V3>> for ProfileMigrator {
84            fn migrate(&self, stored: Profile<Versions>) -> Result<Profile<V3>> {
85                let Profile { config, version } = stored;
86                let config = match config {
87                    Versions::V1(config) => V3 { name3: config.name },
88                    Versions::V2(config) => V3 {
89                        name3: config.name2,
90                    },
91                    Versions::V3(config) => return Ok(Profile { config, version }),
92                };
93
94                Ok(Profile { config, version })
95            }
96
97            fn encode(&self, current: &Profile<V3>) -> Result<Profile<Versions>> {
98                Ok(Profile {
99                    config: Versions::V3(current.config.clone()),
100                    version: current.version,
101                })
102            }
103        }
104
105        let persistor = MemoryPersistor::new(Some(Profile {
106            config: Versions::V1(V1 {
107                name: "Player".to_string(),
108            }),
109            version: 1,
110        }));
111        let saved = persistor.clone();
112
113        let loader = Profile::<V3>::load(ProfileMigrator, persistor, 3).await?;
114        assert_eq!(loader.get(|config| config.name3.clone()).await, "Player");
115        assert!(matches!(
116            saved.load().await?,
117            Some(Profile {
118                config: Versions::V3(V3 { name3 }),
119                version: 3,
120            }) if name3 == "Player"
121        ));
122
123        Ok(())
124    }
125}