Skip to main content

elemental_shared/
profile.rs

1use crate::{
2    error::Result,
3    loader::ProfileLoader,
4    migrator::Migrator,
5    version::{Persistor, VersionControlled, Versioned},
6};
7use serde::{Deserialize, Serialize};
8
9#[derive(Serialize, Deserialize, Default)]
10pub struct Profile<C> {
11    /// Profile configuration data
12    pub config: C,
13    pub version: usize,
14}
15
16impl<C> Versioned for Profile<C> {
17    #[inline(always)]
18    fn version(&self) -> usize {
19        self.version
20    }
21}
22
23impl<C> VersionControlled for Profile<C> {
24    #[inline(always)]
25    fn with_version(mut self, version: usize) -> Self {
26        self.version = version;
27        self
28    }
29}
30
31impl<C: Default> Profile<C> {
32    pub async fn load<M, S, P>(
33        migrator: M,
34        persistor: P,
35        target_version: usize,
36    ) -> Result<ProfileLoader<M, S, C, P>>
37    where
38        S: Versioned,
39        M: Migrator<S, Profile<C>>,
40        P: Persistor<S>,
41    {
42        ProfileLoader::load(migrator, persistor, target_version).await
43    }
44}
45
46impl<C: Clone> Clone for Profile<C> {
47    fn clone(&self) -> Self {
48        Profile {
49            config: self.config.clone(),
50            version: self.version,
51        }
52    }
53}