diff --git a/Cargo.toml b/Cargo.toml index d6d10fb..4be10cd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ members = ["encrypt-config", "encrypt-config-derive", "tests", "examples"] resolver = "2" [workspace.package] -version = "0.1.4" +version = "0.1.5" authors = ["Louis <836250617@qq.com>"] description = "A rust crate to manage, persist and encrypt your configurations." license = "MIT" diff --git a/encrypt-config/Cargo.toml b/encrypt-config/Cargo.toml index 8121d7d..469c3e9 100644 --- a/encrypt-config/Cargo.toml +++ b/encrypt-config/Cargo.toml @@ -25,12 +25,12 @@ encrypt_config_derive = { workspace = true, optional = true } rustc_version = "0.4.0" [features] -default = ["serde", "save_on_change"] +default = ["serde"] full = ["default", "persist", "secret", "derive"] serde = ["dep:serde", "dep:serde_json"] derive = ["dep:encrypt_config_derive"] secret = ["dep:rsa", "dep:rand", "dep:keyring", "persist", "encrypt_config_derive?/secret"] -persist = ["encrypt_config_derive?/persist"] +persist = [ "save_on_change", "encrypt_config_derive?/persist"] default_config_dir = ["dep:dirs-next", "encrypt_config_derive?/default_config_dir"] protobuf = [] save_on_change = [] diff --git a/encrypt-config/src/config.rs b/encrypt-config/src/config.rs index ecd84b8..10e5028 100644 --- a/encrypt-config/src/config.rs +++ b/encrypt-config/src/config.rs @@ -98,7 +98,6 @@ impl Config { /// Add a persist source to the config. /// The source must implement [`PersistSource`] trait, which is for config that needs to be persisted. #[cfg(feature = "persist")] - #[cfg_attr(doc_cfg, doc(cfg(feature = "persist")))] pub fn add_persist_source(&mut self, source: impl PersistSource) -> ConfigResult<()> { let map = match std::fs::read(source.path()) { Ok(serded) => serde_json::from_slice(&serded)?, @@ -119,7 +118,6 @@ impl Config { /// Add a secret source to the config. /// The source must implement [`SecretSource`] trait, which is for config that needs to be encrypted and persisted. #[cfg(feature = "secret")] - #[cfg_attr(doc_cfg, doc(cfg(feature = "secret")))] pub fn add_secret_source(&mut self, source: impl SecretSource) -> ConfigResult<()> { let map = match std::fs::read(source.path()) { Ok(encrypted) => serde_json::from_slice(&self.encrypter.decrypt(&encrypted)?)?, @@ -153,52 +151,47 @@ impl Config { } /// Upgrade a value in the config. The key must already exist. - #[cfg(feature = "persist")] pub fn upgrade(&mut self, key: K, value: &V) -> ConfigResult<()> where K: AsRef, V: serde::Serialize, { - let (_kind, map) = self - .cache - .iter_mut() - .find(|(_, map)| map.contains_key(key.as_ref())) - .context(ConfigNotFound { + #[cfg(feature = "persist")] + { + let (_kind, map) = self + .cache + .iter_mut() + .find(|(_, map)| map.contains_key(key.as_ref())) + .context(ConfigNotFound { + key: key.as_ref().to_owned(), + })?; + + let v = map.get_mut(key.as_ref()).unwrap(); + *v = serde_json::to_vec(value)?; + #[cfg(feature = "save_on_change")] + match _kind { + Kind::Normal => {} + Kind::Persist(path) => { + let path = path.write().unwrap(); + std::fs::write(&*path, serde_json::to_vec(map)?)?; + } + #[cfg(feature = "secret")] + Kind::Secret(path) => { + let path = path.write().unwrap(); + std::fs::write(&*path, self.encrypter.encrypt(map)?)?; + } + } + } + #[cfg(not(feature = "persist"))] + { + let v = self.cache.get_mut(key.as_ref()).context(ConfigNotFound { key: key.as_ref().to_owned(), })?; - - let v = map.get_mut(key.as_ref()).unwrap(); - *v = serde_json::to_vec(value)?; - #[cfg(feature = "save_on_change")] - match _kind { - Kind::Normal => {} - Kind::Persist(path) => { - let path = path.write().unwrap(); - std::fs::write(&*path, serde_json::to_vec(map)?)?; - } - #[cfg(feature = "secret")] - Kind::Secret(path) => { - let path = path.write().unwrap(); - std::fs::write(&*path, self.encrypter.encrypt(map)?)?; - } + *v = serde_json::to_vec(value)?; } Ok(()) } - /// Upgrade a value in the config. The key must already exist. - #[cfg(not(feature = "persist"))] - pub fn upgrade(&mut self, key: K, value: V) -> ConfigResult<()> - where - K: AsRef, - V: serde::Serialize, - { - let v = self.cache.get_mut(key.as_ref()).context(ConfigNotFound { - key: key.as_ref().to_owned(), - })?; - *v = serde_json::to_vec(&value)?; - Ok(()) - } - #[allow(unused)] #[cfg(not(feature = "save_on_change"))] #[cfg_attr(doc_cfg, doc(cfg(feature = "save_on_change")))] diff --git a/encrypt-config/src/lib.rs b/encrypt-config/src/lib.rs index e8084f6..68f1580 100644 --- a/encrypt-config/src/lib.rs +++ b/encrypt-config/src/lib.rs @@ -4,6 +4,8 @@ #[cfg(all(not(feature = "persist"), feature = "default_config_dir"))] compile_error!("Feature `default_config_dir` only works with feature `persist` on."); +#[cfg(all(not(feature = "persist"), feature = "save_on_change"))] +compile_error!("Feature `save_on_change` is designed only for feature `persist` on."); #[cfg(all(not(feature = "secret"), feature = "mock"))] compile_error!("Feature `mock` is designed only for feature `secret` on.");