Skip to content

Commit

Permalink
Merge pull request #80 from 4t145/refactor-config-and-init
Browse files Browse the repository at this point in the history
Fix build error when crypto features disabled
  • Loading branch information
gudaoxuri authored Oct 11, 2023
2 parents 3aee0fa + c70ca3d commit 065c303
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 42 deletions.
3 changes: 2 additions & 1 deletion tardis/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
6 changes: 0 additions & 6 deletions tardis/src/basic/dto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,6 @@ impl TardisContext {
TardisFuns::json.obj_to_string(self)
}

pub fn to_base64(&self) -> TardisResult<String> {
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(())
Expand Down
1 change: 1 addition & 0 deletions tardis/src/basic/tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<C = LogConfig> {
Expand Down
3 changes: 1 addition & 2 deletions tardis/src/config/config_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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");
}
Expand Down
7 changes: 7 additions & 0 deletions tardis/src/crypto.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
39 changes: 39 additions & 0 deletions tardis/src/crypto/crypto_base64.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -24,3 +27,39 @@ impl TardisCryptoBase64 {
general_purpose::STANDARD.encode(data)
}
}

impl TardisContext {
pub fn to_base64(&self) -> TardisResult<String> {
let ctx = TardisContext::default();
let ctx = TardisFuns::json.obj_to_string(&ctx)?;
Ok(TardisCryptoBase64.encode(ctx))
}
}

impl Mapper<String> 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<String> for Base64Decode {
type Output = TardisResult<String>;
fn map(value: String) -> TardisResult<String> {
TardisCryptoBase64.decode(value)
}
}

impl<'a> Mapper<&'a str> for Base64Decode {
type Output = TardisResult<String>;
fn map(value: &'a str) -> TardisResult<String> {
TardisCryptoBase64.decode(value)
}
}
4 changes: 2 additions & 2 deletions tardis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")))]
Expand Down
31 changes: 0 additions & 31 deletions tardis/src/utils/mapper/endecode.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,2 @@
use crate::{basic::result::TardisResult, TardisFuns};

use super::Mapper;
pub struct Base64Encode;
pub struct Base64Decode;

impl Mapper<String> 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<String> for Base64Decode {
type Output = TardisResult<String>;
fn map(value: String) -> TardisResult<String> {
TardisFuns::crypto.base64.decode(value)
}
}

impl<'a> Mapper<&'a str> for Base64Decode {
type Output = TardisResult<String>;
fn map(value: &'a str) -> TardisResult<String> {
TardisFuns::crypto.base64.decode(value)
}
}

0 comments on commit 065c303

Please sign in to comment.