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!(
51            "{:?}",
52            loader.get(|profile| profile.config.user.clone()).await
53        ); // Player
54        loader
55            .set(|profile| profile.config.user = "NewPlayer".to_owned())
56            .await
57            .unwrap();
58        println!(
59            "{:?}",
60            loader.get(|profile| profile.config.user.clone()).await
61        ); // NewPlayer
62    }
63
64    #[tokio::test]
65    async fn v1v2v3() -> Result<()> {
66        #[derive(Serialize, Deserialize, Clone, Default)]
67        struct V1 {
68            name: String,
69        }
70        #[derive(Serialize, Deserialize, Clone, Default)]
71        struct V2 {
72            name2: String,
73        }
74        #[derive(Serialize, Deserialize, Clone, Default)]
75        struct V3 {
76            name3: String,
77        }
78
79        #[derive(Clone, Serialize, Deserialize)]
80        #[serde(untagged)]
81        enum Versions {
82            V1(V1),
83            V2(V2),
84            V3(V3),
85        }
86
87        struct ProfileMigrator;
88
89        impl Migrator<Profile<Versions>, Profile<V3>> for ProfileMigrator {
90            fn migrate(&self, stored: Profile<Versions>) -> Result<Profile<V3>> {
91                let Profile { config, version } = stored;
92                let config = match config {
93                    Versions::V1(config) => V3 { name3: config.name },
94                    Versions::V2(config) => V3 {
95                        name3: config.name2,
96                    },
97                    Versions::V3(config) => return Ok(Profile { config, version }),
98                };
99
100                Ok(Profile { config, version })
101            }
102
103            fn encode(&self, current: &Profile<V3>) -> Result<Profile<Versions>> {
104                Ok(Profile {
105                    config: Versions::V3(current.config.clone()),
106                    version: current.version,
107                })
108            }
109        }
110
111        let persistor = MemoryPersistor::new(Some(Profile {
112            config: Versions::V1(V1 {
113                name: "Player".to_string(),
114            }),
115            version: 1,
116        }));
117        let saved = persistor.clone();
118
119        let loader = Profile::<V3>::load(ProfileMigrator, persistor, 3).await?;
120        assert_eq!(
121            loader.get(|profile| profile.config.name3.clone()).await,
122            "Player"
123        );
124        assert!(matches!(
125            saved.load().await?,
126            Some(Profile {
127                config: Versions::V3(V3 { name3 }),
128                version: 3,
129            }) if name3 == "Player"
130        ));
131
132        Ok(())
133    }
134}