From 05ca9311d1662c85fe8083a9de8feafee2a95bc5 Mon Sep 17 00:00:00 2001 From: Louis <836250617@qq.com> Date: Wed, 19 Jun 2024 16:30:12 +0800 Subject: [PATCH] - fix: (bug) always use the same encrypter even if different keyring entries are given - doc: (derive macros) add doc for `PersistSource` and `SecretSource` --- .github/scripts/test.sh | 22 +++++----- CHANGELOG.md | 16 +++++++ Cargo.toml | 4 +- encrypt-config-derive/Cargo.toml | 12 +++--- encrypt-config-derive/src/lib.rs | 47 ++++++++++++++++++++- encrypt-config/Cargo.toml | 2 - encrypt-config/README.md | 8 ++-- encrypt-config/src/encrypt_utils.rs | 65 +++++++++++++++++++---------- 8 files changed, 126 insertions(+), 50 deletions(-) create mode 100644 CHANGELOG.md diff --git a/.github/scripts/test.sh b/.github/scripts/test.sh index f0f5a69..7fb536d 100755 --- a/.github/scripts/test.sh +++ b/.github/scripts/test.sh @@ -5,20 +5,20 @@ export TERM=xterm-256color # Statements waiting to be executed statements=( - "cargo clippy --all-targets -- -D warnings" - "cargo clippy --all-targets --features persist -- -D warnings" - "cargo clippy --all-targets --features secret -- -D warnings" - "cargo clippy --all-targets --features secret,mock -- -D warnings" + "cargo clippy --all-targets --no-default-features -- -D warnings" + "cargo clippy --all-targets --no-default-features --features persist -- -D warnings" + "cargo clippy --all-targets --no-default-features --features secret -- -D warnings" + "cargo clippy --all-targets --no-default-features --features secret,mock -- -D warnings" - "cargo test" - "cargo test --features persist" - "cargo test --features persist,default_config_dir" - "cargo test --features secret,mock" - "cargo test --features secret,mock,default_config_dir" + "cargo test --no-default-features" + "cargo test --no-default-features --features persist" + "cargo test --no-default-features --features persist,default_config_dir" + "cargo test --no-default-features --features secret,mock" + "cargo test --no-default-features --features secret,mock,default_config_dir" - "cargo run --example example --features full,mock" + "cargo run --example example --no-default-features --features full,mock" - "cargo doc --no-deps --features full" + "cargo doc --no-deps --no-default-features --features full" ) # loop echo and executing statements diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..c3a2470 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,16 @@ +# Changelog + +All notable changes to this project will be documented in this file. + +This project adheres to [Semantic Versioning](https://semver.org). + + + +## [Unreleased] + +## [0.2.4] - 2024-06-19 + +- fix: (bug) always use the same encrypter even if different keyring entries are given +- doc: (derive macros) add doc for `PersistSource` and `SecretSource` diff --git a/Cargo.toml b/Cargo.toml index 65709f2..9f432b7 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.2.3" +version = "0.2.4" authors = ["Louis <836250617@qq.com>"] description = "A Rust crate to manage, persist and encrypt your configurations." license = "MIT" @@ -13,7 +13,7 @@ documentation = "https://docs.rs/encrypt-config" [workspace.dependencies] encrypt_config = { path = "encrypt-config" } -encrypt_config_derive = { path = "encrypt-config-derive", version = "0.2.2" } +encrypt_config_derive = { path = "encrypt-config-derive", version = "0.2.4" } [profile.dev] opt-level = 3 diff --git a/encrypt-config-derive/Cargo.toml b/encrypt-config-derive/Cargo.toml index c5e3459..af0ef82 100644 --- a/encrypt-config-derive/Cargo.toml +++ b/encrypt-config-derive/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "encrypt_config_derive" -version = "0.2.2" +version.workspace = true authors.workspace = true description.workspace = true license.workspace = true @@ -22,11 +22,11 @@ quote = "1.0" [dev-dependencies] serde = { version = "1", features = ["derive"] } -encrypt_config = { workspace = true } +encrypt_config = { workspace = true, default-features = false } [features] default = [] -full = ["persist", "secret"] -persist = [] -secret = ["persist"] -default_config_dir = [] +full = ["persist", "secret", "encrypt_config/full"] +persist = ["encrypt_config/persist"] +secret = ["persist", "encrypt_config/secret"] +default_config_dir = ["encrypt_config/default_config_dir"] diff --git a/encrypt-config-derive/src/lib.rs b/encrypt-config-derive/src/lib.rs index a263a5b..7f5f662 100644 --- a/encrypt-config-derive/src/lib.rs +++ b/encrypt-config-derive/src/lib.rs @@ -15,20 +15,63 @@ mod secret; use proc_macro::TokenStream; /// Derive macro for `NormalSource`. +/// # Example +/// ``` +/// # use encrypt_config_derive::NormalSource; +/// #[derive(Default, NormalSource)] +/// struct NormalConfig { +/// count: i32, +/// } +/// ``` #[proc_macro_derive(NormalSource)] pub fn derive_normal_source(input: TokenStream) -> TokenStream { normal::derive_normal_source(input) } -#[cfg(feature = "persist")] /// Derive macro for `PersistSource`. +/// # Example +/// ``` +/// # use encrypt_config_derive::PersistSource; +/// # use serde::{Deserialize, Serialize}; +/// #[derive(Serialize, Deserialize, Default, PersistSource)] +#[cfg_attr( + feature = "default_config_dir", + doc = "#[source(name = \"persist_config.json\")]" +)] +#[cfg_attr( + not(feature = "default_config_dir"), + doc = "#[source(path = \"/path/to/persist_config.json\")]" +)] +/// struct PersistConfig { +/// name: String, +/// age: i32, +/// } +/// ``` +#[cfg(feature = "persist")] #[proc_macro_derive(PersistSource, attributes(source))] pub fn derive_persist_source(input: TokenStream) -> TokenStream { persist::derive_persist_source(input) } -#[cfg(feature = "secret")] /// Derive macro for `SecretSource`. +/// # Example +/// ``` +/// # use encrypt_config_derive::SecretSource; +/// # use serde::{Deserialize, Serialize}; +/// #[derive(Serialize, Deserialize, Default, SecretSource)] +#[cfg_attr( + feature = "default_config_dir", + doc = "#[source(name = \"secret_config.json\", keyring_entry = \"secret\")]" +)] +#[cfg_attr( + not(feature = "default_config_dir"), + doc = "#[source(path = \"/path/to/secret_config\", keyring_entry = \"secret\")]" +)] +/// struct SecretConfig { +/// password: String, +/// } +/// ``` +#[cfg(feature = "secret")] #[proc_macro_derive(SecretSource, attributes(source))] pub fn derive_secret_source(input: TokenStream) -> TokenStream { secret::derive_secret_source(input) diff --git a/encrypt-config/Cargo.toml b/encrypt-config/Cargo.toml index 0a87179..96834bf 100644 --- a/encrypt-config/Cargo.toml +++ b/encrypt-config/Cargo.toml @@ -23,8 +23,6 @@ keyring = { version = "2.1.0", optional = true } rand = { version = "0.8.5", optional = true } dirs = { version = "5.0.1", optional = true } -[build-dependencies] - [features] default = [] full = ["persist", "secret", "derive"] diff --git a/encrypt-config/README.md b/encrypt-config/README.md index 7339923..09012e0 100644 --- a/encrypt-config/README.md +++ b/encrypt-config/README.md @@ -103,7 +103,7 @@ _(You may see many `#[cfg(feature = "...")]` in the example below, if you are no You can implement the [`NormalSource`], [`PersistSource`] and [`SecretSource`] yourself. ```rust no_run -# #[cfg(feature = "full")] +# #[cfg(all(feature = "full", feature = "default_config_dir"))] # { use encrypt_config::{Config, NormalSource, PersistSource, SecretSource, TEST_OUT_DIR}; use serde::{Deserialize, Serialize}; @@ -115,14 +115,14 @@ struct NormalConfig { } #[derive(Default, Serialize, Deserialize, PersistSource)] -#[source(path = const_str::concat!(TEST_OUT_DIR, "/persist_config.json"))] +#[source(name = "persist_config.json")] struct PersistConfig { name: String, age: usize, } #[derive(Default, Serialize, Deserialize, SecretSource)] -#[source(path = const_str::concat!(TEST_OUT_DIR, "/secret_config.json"), keyring_entry = "secret")] +#[source(name = "secret_config", keyring_entry = "secret")] struct SecretConfig { password: String, } @@ -190,7 +190,7 @@ _For more examples, please refer to the [Example](https://github.com/kingwingfly ## Changelog -- v0.1.x -> v0.2.x: A broken change has been made. Heavily refactored with `std::any` and methods from `dependencies injection`. +- v0.1.x -> v0.2.x: A broken change has been made. Heavily refactored with `std::any` and methods from `dependencies injection`.
diff --git a/encrypt-config/src/encrypt_utils.rs b/encrypt-config/src/encrypt_utils.rs index 21966d9..27373b8 100644 --- a/encrypt-config/src/encrypt_utils.rs +++ b/encrypt-config/src/encrypt_utils.rs @@ -4,11 +4,13 @@ use crate::error::{ConfigError, ConfigResult}; use keyring::Entry; use rsa::{Pkcs1v15Encrypt, RsaPrivateKey, RsaPublicKey}; -use std::sync::OnceLock; +use std::{ + collections::HashMap, + sync::{OnceLock, RwLock}, +}; -#[derive(Debug, serde::Serialize, serde::Deserialize)] -#[cfg_attr(feature = "mock", derive(Clone))] -#[cfg_attr(test, derive(PartialEq))] +#[derive(serde::Serialize, serde::Deserialize)] +#[cfg_attr(test, derive(PartialEq, Debug))] pub struct Encrypter { priv_key: RsaPrivateKey, } @@ -27,25 +29,39 @@ impl Default for Encrypter { impl Encrypter { pub(crate) fn new(secret_name: impl AsRef