Skip to content

Commit

Permalink
fix: doc upgrade feature demand error
Browse files Browse the repository at this point in the history
  • Loading branch information
kingwingfly committed Jan 16, 2024
1 parent 6d21f5b commit 289a5f5
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>"]
description = "A rust crate to manage, persist and encrypt your configurations."
license = "MIT"
Expand Down
4 changes: 2 additions & 2 deletions encrypt-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
67 changes: 30 additions & 37 deletions encrypt-config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?,
Expand All @@ -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)?)?,
Expand Down Expand Up @@ -153,52 +151,47 @@ impl Config {
}

/// Upgrade a value in the config. The key must already exist.
#[cfg(feature = "persist")]
pub fn upgrade<K, V>(&mut self, key: K, value: &V) -> ConfigResult<()>
where
K: AsRef<str>,
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<K, V>(&mut self, key: K, value: V) -> ConfigResult<()>
where
K: AsRef<str>,
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")))]
Expand Down
2 changes: 2 additions & 0 deletions encrypt-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.");

Expand Down

0 comments on commit 289a5f5

Please sign in to comment.