diff --git a/Cargo.toml b/Cargo.toml index 2e34b96..03a74ac 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.0.1" +version = "0.0.2" authors = ["Louis <836250617@qq.com>"] description = "A rust crate to manage, persist and encrypt your configurations." license = "MIT" @@ -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 diff --git a/encrypt-config-derive/Cargo.toml b/encrypt-config-derive/Cargo.toml index 9c5103a..be55cc7 100644 --- a/encrypt-config-derive/Cargo.toml +++ b/encrypt-config-derive/Cargo.toml @@ -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" @@ -20,6 +24,7 @@ proc-macro = true [features] default = [] +full = ["persist", "secret"] persist = [] secret = [] default_config_dir = [] diff --git a/encrypt-config-derive/src/lib.rs b/encrypt-config-derive/src/lib.rs index 929ae5b..a74e0cf 100644 --- a/encrypt-config-derive/src/lib.rs +++ b/encrypt-config-derive/src/lib.rs @@ -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."); @@ -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 => { @@ -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); @@ -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 => { @@ -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; - - fn path(&self) -> ::std::path::PathBuf { - ::std::path::PathBuf::from(#path) - } - fn default(&self) -> Result> { - 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; - fn source_name(&self) -> String { - #source_name.to_owned() - } + #func fn default(&self) -> Result> { Ok(#default_expr.into_iter().collect()) @@ -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); @@ -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 => { @@ -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; - - - fn path(&self) -> ::std::path::PathBuf { - ::std::path::PathBuf::from(#path) - } - fn default(&self) -> Result> { - 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; - fn source_name(&self) -> String { - #source_name.to_owned() - } + #func fn default(&self) -> Result> { Ok(#default_expr.into_iter().collect()) diff --git a/encrypt-config/Cargo.toml b/encrypt-config/Cargo.toml index 6466aba..e54735b 100644 --- a/encrypt-config/Cargo.toml +++ b/encrypt-config/Cargo.toml @@ -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" } diff --git a/encrypt-config/src/config.rs b/encrypt-config/src/config.rs index e850a4e..100f5a3 100644 --- a/encrypt-config/src/config.rs +++ b/encrypt-config/src/config.rs @@ -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; @@ -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) -> Self { @@ -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)?, @@ -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)?)?, @@ -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!(); } diff --git a/encrypt-config/src/lib.rs b/encrypt-config/src/lib.rs index c79754c..ae2e8da 100644 --- a/encrypt-config/src/lib.rs +++ b/encrypt-config/src/lib.rs @@ -1,3 +1,4 @@ +#![cfg_attr(doc_cfg, feature(doc_cfg))] #![doc = include_str!("../README.md")] #![deny(missing_docs, rustdoc::broken_intra_doc_links)] @@ -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; @@ -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)] diff --git a/encrypt-config/src/source.rs b/encrypt-config/src/source.rs index df7abe2..e572173 100644 --- a/encrypt-config/src/source.rs +++ b/encrypt-config/src/source.rs @@ -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; @@ -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; diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 0000000..5d56faf --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,2 @@ +[toolchain] +channel = "nightly"