amethystate 0.9.1

Type-safe reactive persistence for Rust GUI apps
Documentation
use amethystate::store::builder::StoreBuilder;
use amethystate::{ReactiveMap, amethystate};
use amethystate_core::test_utils::unique_path;

mod v1 {
    use super::*;
    #[amethystate(prefix = "app", version = 1)]
    pub struct AppConfig {
        #[amestate(default = {
            "HTTP_PROXY": "http://127.0.0.1:8080".to_string(),
            "NO_PROXY": "localhost".to_string()
        })]
        pub env: ReactiveMap<String, String>,
    }
}

#[amethystate(prefix = "app", version = 2)]
pub struct AppConfig {
    #[amestate(default = {
        "HTTP_PROXY": "http://127.0.0.1:8080".to_string(),
        "NO_PROXY": "localhost".to_string(),
        "NEW_KEY": "new_default".to_string()
    })]
    pub env: ReactiveMap<String, String>,
}

#[test]
fn test_map_defaults_applied_only_on_first_init() {
    let path = unique_path("first_init");

    {
        let store = StoreBuilder::new(&path).build().unwrap();
        let config = v1::AppConfig::new_with(&store).unwrap();

        let env = config.env();
        assert_eq!(
            env.get(&"HTTP_PROXY".to_string()).unwrap(),
            Some("http://127.0.0.1:8080".to_string())
        );
        assert_eq!(
            env.get(&"NO_PROXY".to_string()).unwrap(),
            Some("localhost".to_string())
        );
    }

    {
        let store = StoreBuilder::new(&path).build().unwrap();
        let config = v1::AppConfig::new_with(&store).unwrap();

        let env = config.env();
        assert_eq!(
            env.get(&"HTTP_PROXY".to_string()).unwrap(),
            Some("http://127.0.0.1:8080".to_string())
        );
        assert_eq!(
            env.get(&"NO_PROXY".to_string()).unwrap(),
            Some("localhost".to_string())
        );
    }
}

#[test]
fn test_deleted_map_key_does_not_resurrect() {
    let path = unique_path("no_resurrect");

    {
        let store = StoreBuilder::new(&path).build().unwrap();
        let config = v1::AppConfig::new_with(&store).unwrap();
        config.env().remove("NO_PROXY".to_string()).unwrap();
    }

    {
        let store = StoreBuilder::new(&path).build().unwrap();
        let config = v1::AppConfig::new_with(&store).unwrap();
        assert_eq!(config.env().get(&"NO_PROXY".to_string()).unwrap(), None);
    }
}

#[test]
fn test_new_defaults_applied_on_version_upgrade() {
    let path = unique_path("version_upgrade");

    {
        let store = StoreBuilder::new(&path).build().unwrap();
        let config = v1::AppConfig::new_with(&store).unwrap();
        config.env().remove("NO_PROXY".to_string()).unwrap();
    }

    {
        let store = StoreBuilder::new(&path).build().unwrap();
        let config = AppConfig::new_with(&store).unwrap();
        let env = config.env();

        assert_eq!(env.get(&"NO_PROXY".to_string()).unwrap(), None);

        assert_eq!(
            env.get(&"HTTP_PROXY".to_string()).unwrap(),
            Some("http://127.0.0.1:8080".to_string())
        );

        assert_eq!(env.get(&"NEW_KEY".to_string()).unwrap(), None);
    }
}

#[test]
fn test_user_set_value_not_overwritten_by_defaults() {
    let path = unique_path("no_overwrite");

    {
        let store = StoreBuilder::new(&path).build().unwrap();
        let config = v1::AppConfig::new_with(&store).unwrap();
        config
            .env()
            .set_or_create("HTTP_PROXY".to_string(), &"http://custom:9999".to_string())
            .unwrap();
    }

    {
        let store = StoreBuilder::new(&path).build().unwrap();
        let config = v1::AppConfig::new_with(&store).unwrap();
        assert_eq!(
            config.env().get(&"HTTP_PROXY".to_string()).unwrap(),
            Some("http://custom:9999".to_string())
        );
    }
}