diff --git a/tardis/Cargo.toml b/tardis/Cargo.toml index f91ed708..78d5b2e6 100644 --- a/tardis/Cargo.toml +++ b/tardis/Cargo.toml @@ -17,11 +17,12 @@ name = "tardis" path = "src/lib.rs" [features] -default = ["tardis-macros", "async-trait"] +default = ["tardis-macros", "async-trait", "base64"] conf-remote = ["web-client", "async-trait", "crypto"] digest = ["sha1", "sha2", "hmac", "md-5", "sm3", "dep:digest"] aead = ["aes-gcm-siv", "aes-gcm", "aes-siv", "dep:aead"] block_modes = ["cbc", "ecb", "aes", "cipher"] +base64 = [] crypto = ["rsa", "digest", "aead", "block_modes"] crypto-with-sm = ["crypto", "libsm", "num-bigint"] future = ["futures", "async-stream", "futures-util", "async-trait"] diff --git a/tardis/src/basic/dto.rs b/tardis/src/basic/dto.rs index bda7d885..b0851af8 100644 --- a/tardis/src/basic/dto.rs +++ b/tardis/src/basic/dto.rs @@ -103,12 +103,6 @@ impl TardisContext { TardisFuns::json.obj_to_string(self) } - pub fn to_base64(&self) -> TardisResult { - let ctx = TardisContext::default(); - let ctx = TardisFuns::json.obj_to_string(&ctx)?; - Ok(TardisFuns::crypto.base64.encode(ctx)) - } - pub async fn add_ext(&self, key: &str, value: &str) -> TardisResult<()> { self.ext.write().await.insert(key.to_string(), value.to_string()); Ok(()) diff --git a/tardis/src/basic/tracing.rs b/tardis/src/basic/tracing.rs index daeaf58e..c8af75be 100644 --- a/tardis/src/basic/tracing.rs +++ b/tardis/src/basic/tracing.rs @@ -10,6 +10,7 @@ use crate::consts::*; use tracing_subscriber::layer::Layered; use tracing_subscriber::util::SubscriberInitExt; use tracing_subscriber::EnvFilter; +#[allow(unused_imports)] use tracing_subscriber::{fmt::Layer as FmtLayer, layer::SubscriberExt, prelude::*, reload::Layer as ReloadLayer, Registry}; #[derive(Default)] pub struct TardisTracing { diff --git a/tardis/src/config/config_processor.rs b/tardis/src/config/config_processor.rs index e6ef0996..859efc20 100644 --- a/tardis/src/config/config_processor.rs +++ b/tardis/src/config/config_processor.rs @@ -12,7 +12,6 @@ use crate::basic::fetch_profile; use crate::basic::locale::TardisLocale; use crate::basic::result::TardisResult; use crate::config::config_dto::FrameworkConfig; -use crate::TardisFuns; use tracing::{debug, info}; use super::config_dto::{ConfCenterConfig, TardisConfig}; @@ -210,7 +209,7 @@ impl ConfCenterConfig { } }; if let Ok(config) = TardisConfig::init(relative_path.as_deref()).await { - match TardisFuns::hot_reload(config).await { + match crate::TardisFuns::hot_reload(config).await { Ok(_) => { tracing::info!("[Tardis.config] Tardis hot reloaded"); } diff --git a/tardis/src/crypto.rs b/tardis/src/crypto.rs index 7a3b8d0e..b23c3b6f 100644 --- a/tardis/src/crypto.rs +++ b/tardis/src/crypto.rs @@ -1,9 +1,16 @@ +#[cfg(feature = "aead")] pub mod crypto_aead; +#[cfg(any(feature = "crypto", feature = "base64"))] pub mod crypto_base64; +#[cfg(feature = "digest")] pub mod crypto_digest; +#[cfg(feature = "crypto")] pub mod crypto_hex; +#[cfg(feature = "crypto")] pub mod crypto_key; +#[cfg(feature = "crypto")] pub mod crypto_main; +#[cfg(feature = "rsa")] pub mod crypto_rsa; #[cfg(feature = "crypto-with-sm")] pub mod crypto_sm2_4; diff --git a/tardis/src/crypto/crypto_base64.rs b/tardis/src/crypto/crypto_base64.rs index 11c0c6a9..7124f3aa 100644 --- a/tardis/src/crypto/crypto_base64.rs +++ b/tardis/src/crypto/crypto_base64.rs @@ -1,8 +1,11 @@ use base64::engine::general_purpose; use base64::Engine; +use crate::basic::dto::TardisContext; use crate::basic::error::TardisError; use crate::basic::result::TardisResult; +use crate::utils::mapper::{Base64Decode, Base64Encode, Mapper}; +use crate::TardisFuns; pub struct TardisCryptoBase64; impl TardisCryptoBase64 { @@ -24,3 +27,39 @@ impl TardisCryptoBase64 { general_purpose::STANDARD.encode(data) } } + +impl TardisContext { + pub fn to_base64(&self) -> TardisResult { + let ctx = TardisContext::default(); + let ctx = TardisFuns::json.obj_to_string(&ctx)?; + Ok(TardisCryptoBase64.encode(ctx)) + } +} + +impl Mapper for Base64Encode { + type Output = String; + fn map(value: String) -> String { + TardisCryptoBase64.encode(value) + } +} + +impl<'a> Mapper<&'a str> for Base64Encode { + type Output = String; + fn map(value: &'a str) -> String { + TardisCryptoBase64.encode(value) + } +} + +impl Mapper for Base64Decode { + type Output = TardisResult; + fn map(value: String) -> TardisResult { + TardisCryptoBase64.decode(value) + } +} + +impl<'a> Mapper<&'a str> for Base64Decode { + type Output = TardisResult; + fn map(value: &'a str) -> TardisResult { + TardisCryptoBase64.decode(value) + } +} diff --git a/tardis/src/lib.rs b/tardis/src/lib.rs index 2304d9ef..a010fa23 100644 --- a/tardis/src/lib.rs +++ b/tardis/src/lib.rs @@ -1245,8 +1245,8 @@ pub mod cache; pub mod cluster; pub mod config; -#[cfg(feature = "crypto")] -#[cfg_attr(docsrs, doc(cfg(feature = "crypto")))] +#[cfg(any(feature = "crypto", feature = "base64"))] +#[cfg_attr(docsrs, doc(cfg(any(feature = "crypto", feature = "base64"))))] pub mod crypto; #[cfg(feature = "reldb-core")] #[cfg_attr(docsrs, doc(cfg(feature = "reldb-core")))] diff --git a/tardis/src/utils/mapper/endecode.rs b/tardis/src/utils/mapper/endecode.rs index 9bedbbcb..ea5ceec6 100644 --- a/tardis/src/utils/mapper/endecode.rs +++ b/tardis/src/utils/mapper/endecode.rs @@ -1,33 +1,2 @@ -use crate::{basic::result::TardisResult, TardisFuns}; - -use super::Mapper; pub struct Base64Encode; pub struct Base64Decode; - -impl Mapper for Base64Encode { - type Output = String; - fn map(value: String) -> String { - TardisFuns::crypto.base64.encode(value) - } -} - -impl<'a> Mapper<&'a str> for Base64Encode { - type Output = String; - fn map(value: &'a str) -> String { - TardisFuns::crypto.base64.encode(value) - } -} - -impl Mapper for Base64Decode { - type Output = TardisResult; - fn map(value: String) -> TardisResult { - TardisFuns::crypto.base64.decode(value) - } -} - -impl<'a> Mapper<&'a str> for Base64Decode { - type Output = TardisResult; - fn map(value: &'a str) -> TardisResult { - TardisFuns::crypto.base64.decode(value) - } -}