Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

18 new feature derive macros #19

Merged
merged 4 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ jobs:
- name: Run all tests with default features
run: cargo test

- name: Run tests(no doc test) with all features
run: cargo test test --all-features
- name: Run tests with all features
run: cargo test --all-features

- name: publish crates
uses: katyo/publish-crates@v2
Expand All @@ -49,7 +49,7 @@ jobs:
uses: actions/github-script@v6
with:
script: |
const {VERSION} = process.env
const {VERSION} = process.env
github.rest.git.createRef({
owner: context.repo.owner,
repo: context.repo.repo,
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ jobs:
- name: Run all tests with default features
run: cargo test

- name: Run tests(no doc test) with all features
run: cargo test test --all-features
- name: Run tests with all features
run: cargo test --all-features

- name: Cache save
if: steps.cache-cargo-restore.outputs.cache-hit != 'true'
Expand Down
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "encrypt_config"
version = "0.0.1-alpha3"
name = "encrypt-config"
version = "0.0.1-alpha4"
authors = ["Louis <[email protected]>"]
description = "A rust crate to manage, persist and encrypt your configurations."
license = "MIT"
Expand All @@ -15,7 +15,7 @@ rsa = { version = "0.9.6", features = ["serde"] }
keyring = "2.1.0"
dirs-next = { version = "2.0.0", optional = true }
rand = "0.8.5"

encrypt-config-derive = { path = "encrypt-config-derive", version = "0.0.1-alpha4" }

[profile.release]
lto = true
Expand All @@ -28,5 +28,5 @@ opt-level = 3
[features]
default = ["serde"]
serde = ["dep:serde", "dep:serde_json"]
default_config_dir = ["dep:dirs-next"]
default_config_dir = ["dep:dirs-next", "encrypt-config-derive/default_config_dir"]
protobuf = []
39 changes: 37 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ This crate also has some optional features:

<!-- USAGE EXAMPLES -->
## Usage
_(You may see many `#[cfg(feature = "...")]` in the example below, if you are not familar to Rust, you may not know this attribute is for `Conditinal Compile`, so that I can test it in `cargo test --all-features` automatically to ensure all go right.)_

You can implement the `Source`, `PersistSource` and `SecretSource` yourself.
```rust no_run
use encrypt_config::{Config, SecretSource};
use serde::{Deserialize, Serialize};
Expand All @@ -109,10 +112,15 @@ struct SecretSourceImpl;
impl SecretSource for SecretSourceImpl {
type Value = Foo;

// Where the encypted file is. Don't need if turning on `default_config_dir` feature.
#[cfg(not(feature = "default_config_dir"))]
fn path(&self) -> std::path::PathBuf {
std::path::PathBuf::from("tests").join("secret.conf")
}

#[cfg(feature = "default_config_dir")]
fn source_name(&self) -> String {
"secret.conf".to_owned()
}
}

let mut config = Config::new("test"); // `test` is the name of rsa private key in OS' secret manager
Expand All @@ -125,7 +133,34 @@ let v: Foo = config.get("key").unwrap();
assert_eq!(v, Foo("value".to_owned()));
```

_For more examples, please refer to the [Example](examples/example.rs) or [Documentation](https://docs.rs/encrypt_config)_
You can also use the derive macros.

```rust no_run
// To derive [`Source`]
use encrypt_config::{PersistSource, SecretSource, Source};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct Foo(String);

#[derive(Source)]
#[source(value(Foo), default([("key".to_owned(), Foo("value".to_owned()))]))]
struct SourceFoo;

//To derive [`PersistSource`]
#[cfg(not(feature = "default_config_dir"))]
#[derive(PersistSource)]
#[source(value(Foo), path("tests/persist.conf"), default([("key".to_owned(), Foo("value".to_owned()))]))]
struct PersistSourceFoo;

// To derive [`SecretSource`]
#[cfg(not(feature = "default_config_dir"))]
#[derive(SecretSource)]
#[source(value(Foo), path("tests/secret.conf"), default([("key".to_owned(), Foo("value".to_owned()))]))]
struct SecretSourceFoo;
```

_For more examples, please refer to the [Example](examples) or [Documentation](https://docs.rs/encrypt_config)_

<p align="right">(<a href="#readme-top">back to top</a>)</p>

Expand Down
18 changes: 18 additions & 0 deletions encrypt-config-derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "encrypt-config-derive"
version = "0.0.1-alpha4"
authors = ["Louis <[email protected]>"]
description = "A rust crate to manage, persist and encrypt your configurations."
license = "MIT"
edition = "2021"
repository = "https://github.com/kingwingfly/encrypt-config"

[dependencies]
syn = { version = "2.0", features = ["full"] }
quote = "1.0"

[lib]
proc-macro = true

[features]
default_config_dir = []
Loading