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

31 release 002 #32

Merged
merged 2 commits into from
Dec 24, 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
4 changes: 2 additions & 2 deletions 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.0.1"
version = "0.0.2"
authors = ["Louis <[email protected]>"]
description = "A rust crate to manage, persist and encrypt your configurations."
license = "MIT"
Expand All @@ -12,7 +12,7 @@ repository = "https://github.com/kingwingfly/encrypt-config"

[workspace.dependencies]
encrypt_config = { path = "encrypt-config" }
encrypt_config_derive = { path = "encrypt-config-derive", version = "^0.0.1-alpha6" }
encrypt_config_derive = { path = "encrypt-config-derive", version = ">=0.0.1" }

[profile.dev]
opt-level = 3
5 changes: 5 additions & 0 deletions encrypt-config-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ license.workspace = true
edition.workspace = true
repository.workspace = true

[package.metadata.docs.rs]
features = ["full"]
rustdoc-args = ["--cfg", "doc_cfg", "--generate-link-to-definition"]

[dependencies]
syn = { version = "2.0", features = ["full"] }
quote = "1.0"
Expand All @@ -20,6 +24,7 @@ proc-macro = true

[features]
default = []
full = ["persist", "secret"]
persist = []
secret = []
default_config_dir = []
87 changes: 34 additions & 53 deletions encrypt-config-derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg_attr(doc_cfg, feature(doc_cfg))]

#[cfg(all(not(feature = "persist"), feature = "default_config_dir"))]
compile_error!("Feature `default_config_dir` only works with feature `persist` on.");

Expand Down Expand Up @@ -41,15 +43,13 @@ pub fn derive_normal_source(input: TokenStream) -> TokenStream {
{
attr.parse_nested_meta(|meta| {
if let Some(i) = meta.path.get_ident() {
let content;
parenthesized!(content in meta.input);
match i.to_string().as_str() {
"default" => {
let content;
parenthesized!(content in meta.input);
default_expr = content.parse()?;
}
"value" => {
let content;
parenthesized!(content in meta.input);
value = content.parse()?;
}
attr => {
Expand Down Expand Up @@ -98,6 +98,7 @@ pub fn derive_normal_source(input: TokenStream) -> TokenStream {
/// struct SourceFoo;
/// ```
#[cfg(feature = "persist")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "persist")))]
#[proc_macro_derive(PersistSource, attributes(source))]
pub fn derive_persist_source(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
Expand All @@ -120,27 +121,21 @@ pub fn derive_persist_source(input: TokenStream) -> TokenStream {
{
attr.parse_nested_meta(|meta| {
if let Some(i) = meta.path.get_ident() {
let content;
parenthesized!(content in meta.input);
match i.to_string().as_str() {
"default" => {
let content;
parenthesized!(content in meta.input);
default_expr = content.parse()?;
}
"value" => {
let content;
parenthesized!(content in meta.input);
value = content.parse()?;
}
#[cfg(not(feature = "default_config_dir"))]
"path" => {
let content;
parenthesized!(content in meta.input);
path = content.parse().ok();
}
#[cfg(feature = "default_config_dir")]
"source_name" => {
let content;
parenthesized!(content in meta.input);
source_name = content.parse().ok();
}
attr => {
Expand All @@ -159,30 +154,26 @@ pub fn derive_persist_source(input: TokenStream) -> TokenStream {
})
.expect("");
};
#[cfg(not(feature = "default_config_dir"))]
let expanded = quote! {
impl #impl_generics encrypt_config::PersistSource for #name #ty_generics #where_clause {
type Value = #value;
type Map = ::std::collections::HashMap<String, Self::Value>;

fn path(&self) -> ::std::path::PathBuf {
::std::path::PathBuf::from(#path)
}

fn default(&self) -> Result<Self::Map, Box<dyn std::error::Error>> {
Ok(#default_expr.into_iter().collect())
}
#[cfg(not(feature = "default_config_dir"))]
let func = quote!(
fn path(&self) -> ::std::path::PathBuf {
::std::path::PathBuf::from(#path)
}
};
);
#[cfg(feature = "default_config_dir")]
let func = quote!(
fn source_name(&self) -> String {
#source_name.to_owned()
}
);

let expanded = quote! {
impl #impl_generics encrypt_config::PersistSource for #name #ty_generics #where_clause {
type Value = #value;
type Map = ::std::collections::HashMap<String, Self::Value>;

fn source_name(&self) -> String {
#source_name.to_owned()
}
#func

fn default(&self) -> Result<Self::Map, Box<dyn std::error::Error>> {
Ok(#default_expr.into_iter().collect())
Expand Down Expand Up @@ -215,6 +206,7 @@ pub fn derive_persist_source(input: TokenStream) -> TokenStream {
/// struct SourceFoo;
/// ```
#[cfg(feature = "secret")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "secret")))]
#[proc_macro_derive(SecretSource, attributes(source))]
pub fn derive_secret_source(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
Expand All @@ -237,27 +229,21 @@ pub fn derive_secret_source(input: TokenStream) -> TokenStream {
{
attr.parse_nested_meta(|meta| {
if let Some(i) = meta.path.get_ident() {
let content;
parenthesized!(content in meta.input);
match i.to_string().as_str() {
"default" => {
let content;
parenthesized!(content in meta.input);
default_expr = content.parse()?;
}
"value" => {
let content;
parenthesized!(content in meta.input);
value = content.parse()?;
}
#[cfg(not(feature = "default_config_dir"))]
"path" => {
let content;
parenthesized!(content in meta.input);
path = content.parse().ok();
}
#[cfg(feature = "default_config_dir")]
"source_name" => {
let content;
parenthesized!(content in meta.input);
source_name = content.parse().ok();
}
attr => {
Expand All @@ -276,31 +262,26 @@ pub fn derive_secret_source(input: TokenStream) -> TokenStream {
})
.expect("");
};
#[cfg(not(feature = "default_config_dir"))]
let expanded = quote! {
impl #impl_generics encrypt_config::SecretSource for #name #ty_generics #where_clause {
type Value = #value;
type Map = ::std::collections::HashMap<String, Self::Value>;


fn path(&self) -> ::std::path::PathBuf {
::std::path::PathBuf::from(#path)
}

fn default(&self) -> Result<Self::Map, Box<dyn std::error::Error>> {
Ok(#default_expr.into_iter().collect())
}
#[cfg(not(feature = "default_config_dir"))]
let func = quote!(
fn path(&self) -> ::std::path::PathBuf {
::std::path::PathBuf::from(#path)
}
};
);
#[cfg(feature = "default_config_dir")]
let func = quote!(
fn source_name(&self) -> String {
#source_name.to_owned()
}
);

let expanded = quote! {
impl #impl_generics encrypt_config::SecretSource for #name #ty_generics #where_clause {
type Value = #value;
type Map = ::std::collections::HashMap<String, Self::Value>;

fn source_name(&self) -> String {
#source_name.to_owned()
}
#func

fn default(&self) -> Result<Self::Map, Box<dyn std::error::Error>> {
Ok(#default_expr.into_iter().collect())
Expand Down
5 changes: 4 additions & 1 deletion encrypt-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ description.workspace = true
license.workspace = true
edition.workspace = true
repository.workspace = true
metadata.docs.rs.features = ["full"]

[package.metadata.docs.rs]
features = ["full"]
rustdoc-args = ["--cfg", "doc_cfg", "--generate-link-to-definition"]

[dependencies]
snafu = { version = "0.7.5" }
Expand Down
7 changes: 5 additions & 2 deletions encrypt-config/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! # Config
//! This module provides a `Config` struct that can be used to store configuration values.
//! This module provides a [`Config`] struct that can be used to store configuration values.

#[cfg(feature = "secret")]
use crate::encrypt_utils::Encrypter;
Expand Down Expand Up @@ -44,7 +44,7 @@ pub struct Config {
}

impl Config {
/// Create a new `Config` struct.
/// Create a new [`Config`] struct.
/// # Arguments
/// * `config_name` - The name of the rsa private key stored by `keyring`.
pub fn new(#[cfg(feature = "secret")] secret_name: impl AsRef<str>) -> Self {
Expand Down Expand Up @@ -110,6 +110,7 @@ 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 @@ -130,6 +131,7 @@ 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 @@ -211,6 +213,7 @@ impl Config {

#[allow(unused)]
#[cfg(not(feature = "save_on_change"))]
#[cfg_attr(doc_cfg, doc(cfg(feature = "save_on_change")))]
fn save(&self) -> ConfigResult<()> {
unimplemented!();
}
Expand Down
3 changes: 3 additions & 0 deletions encrypt-config/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg_attr(doc_cfg, feature(doc_cfg))]
#![doc = include_str!("../README.md")]
#![deny(missing_docs, rustdoc::broken_intra_doc_links)]

Expand All @@ -8,6 +9,7 @@ compile_error!("Feature `mock` is designed only for feature `secret` on.");

mod config;
#[cfg(feature = "secret")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "secret")))]
mod encrypt_utils;
mod error;
mod source;
Expand All @@ -17,6 +19,7 @@ pub use error::*;
pub use source::*;

#[cfg(feature = "derive")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "derive")))]
pub use encrypt_config_derive::*;

#[cfg(test)]
Expand Down
2 changes: 2 additions & 0 deletions encrypt-config/src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ pub trait Source {
/// assert_eq!(config_new.get::<_, Foo>("persist").unwrap(), new_value);
/// ```
#[cfg(feature = "persist")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "persist")))]
pub trait PersistSource {
/// The type of the config value
type Value: Serialize + DeserializeOwned;
Expand Down Expand Up @@ -152,6 +153,7 @@ pub trait PersistSource {
/// assert_eq!(config.get::<_, Foo>("secret").unwrap(), new_value);
/// ```
#[cfg(feature = "secret")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "secret")))]
pub trait SecretSource {
/// The type of the config value
type Value: Serialize + DeserializeOwned;
Expand Down
2 changes: 2 additions & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[toolchain]
channel = "nightly"