Skip to content

Commit

Permalink
improve example
Browse files Browse the repository at this point in the history
  • Loading branch information
kingwingfly committed Jul 24, 2024
1 parent d30164a commit b8d4f1c
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 6 deletions.
1 change: 1 addition & 0 deletions .github/scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ statements=(
"cargo test --no-default-features --features full,mock,default_config_dir"

"cargo run --example example --no-default-features --features full,mock"
"cargo run --example example --no-default-features --features full,mock,default_config_dir"

"cargo doc --no-deps --no-default-features --features full,mock"
)
Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ Note: In this file, do not use the hard wrap in the middle of a sentence for com
-->

## [Unreleased]
### [0.4.2] - 2024-07-24
## [0.4.2] - 2024-07-24

- improve doc
- multiple get functions for `Config` are added

## [0.4.1] - 2024-07-24

Expand Down
2 changes: 1 addition & 1 deletion encrypt-config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ cfg.save(SecretConfig {
// Restart again
let cfg = Config::default();
assert_eq!(cfg.get::<SecretConfig>().password, "123");
// You can also get multiple configs at once
// You can also get multiple configs at once. Usage see example below.
let (_normal, _persist, _secret) =
cfg.get_many::<(NormalConfig, PersistConfig, SecretConfig)>();
# }
Expand Down
29 changes: 27 additions & 2 deletions examples/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,33 @@ fn main() {
let cfg = Config::default();
assert_eq!(cfg.get::<SecretConfig>().password, "123");
// You can also get multiple configs at once
let (_normal, _persist, _secret) =
cfg.get_many::<(NormalConfig, PersistConfig, SecretConfig)>();
{
let (mut normal, mut persist, mut secret) =
cfg.get_mut_many::<(NormalConfig, PersistConfig, SecretConfig)>();
normal.count = 1;
persist.name = "kingwingfly".to_string();
persist.age = 23;
secret.password = "password".to_string();
}
{
let (normal, persist, secret) =
cfg.get_many::<(NormalConfig, PersistConfig, SecretConfig)>();
assert_eq!(normal.count, 1);
assert_eq!(persist.name, "kingwingfly");
assert_eq!(persist.age, 23);
assert_eq!(secret.password, "password");
}

// Restart again
let cfg = Config::default();
{
let (normal, persist, secret) =
cfg.get_many::<(NormalConfig, PersistConfig, SecretConfig)>();
assert_eq!(normal.count, 0);
assert_eq!(persist.name, "kingwingfly");
assert_eq!(persist.age, 23);
assert_eq!(secret.password, "password");
}

// clean after test
for file in files {
Expand Down
21 changes: 19 additions & 2 deletions tests/secret_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,24 @@ fn secret_test() {
}

let config = Config::default();
let secret_config = config.get::<SecretConfig>();
assert_eq!(secret_config.value, 42);
{
let secret_config = config.get::<SecretConfig>();
assert_eq!(secret_config.value, 42);
}
{
let (mut secret,) = config.get_mut_many::<(SecretConfig,)>();
secret.value = 0;
}
{
let secret_config = config.get::<SecretConfig>();
assert_eq!(secret_config.value, 0);
}

let config = Config::default();
{
let secret_config = config.get::<SecretConfig>();
assert_eq!(secret_config.value, 0);
}

std::fs::remove_file(SecretConfig::path()).ok();
}

0 comments on commit b8d4f1c

Please sign in to comment.