Skip to content

Commit

Permalink
refact: use cfg_if to control feature related code
Browse files Browse the repository at this point in the history
Signed-off-by: Kilerd Chan <[email protected]>
  • Loading branch information
Kilerd committed Jul 29, 2024
1 parent 4644c52 commit 2ccdff3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 27 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions zhang-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ snailquote = "0.3"
once_cell = "1.19"
extism = { version = "1.0", optional = true }
semver = "1.0.22"
cfg-if = "1.0.0"

[dev-dependencies]
indoc = "2"
Expand Down
20 changes: 10 additions & 10 deletions zhang-core/src/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::path::PathBuf;
use std::sync::atomic::AtomicI32;
use std::sync::{Arc, RwLock};

use cfg_if::cfg_if;
use itertools::Itertools;
use log::{error, info};
use zhang_ast::{Directive, DirectiveType, Options, Plugin, SpanInfo, Spanned};
Expand Down Expand Up @@ -324,18 +325,15 @@ impl Ledger {

fn handle_plugin_execution(&mut self, other_directives: Vec<Spanned<Directive>>) -> ZhangResult<Vec<Spanned<Directive>>> {
let other_directives = Ledger::sort_directives_datetime(other_directives);
let d = feature_enable!(
self.options.features.plugins,
{
#[cfg(feature = "plugin_runtime")]
{
let d = if self.options.features.plugins {
cfg_if! {
if #[cfg(feature = "plugin_runtime")] {
let mut directives = other_directives;
let options = self.operations().options()?;
// execute the plugins of processor type
for plugin in self.plugins.processors.iter() {
directives = plugin.execute_as_processor(directives, &options)?;
}

directives = Ledger::sort_directives_datetime(directives);

// execute the plugins of mapper type
Expand All @@ -345,12 +343,14 @@ impl Ledger {
directives = plugin_ret?.into_iter().flatten().collect_vec();
}
Ledger::sort_directives_datetime(directives)

}else {
other_directives
}
#[cfg(not(feature = "plugin_runtime"))]
other_directives
},
}
} else {
other_directives
);
};
Ok(d)
}
}
Expand Down
36 changes: 19 additions & 17 deletions zhang-core/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::borrow::Cow;
use std::collections::{HashMap, HashSet};
use std::str::FromStr;

use cfg_if::cfg_if;
use chrono_tz::Tz;
use itertools::Itertools;
use log::error;
Expand Down Expand Up @@ -39,24 +40,25 @@ pub enum BuiltinOption {
}

fn detect_timezone() -> String {
#[cfg(feature = "iana-time-zone")]
{
static DETECTED_TZ: OnceCell<String> = OnceCell::new();
DETECTED_TZ
.get_or_init(|| match iana_time_zone::get_timezone() {
Ok(timezone) => {
log::info!("detect system timezone is {}", timezone);
timezone
}
Err(e) => {
log::warn!("cannot get timezone, fall back to use GMT+8 as default timezone: {}", e);
DEFAULT_TIMEZONE.to_owned()
}
})
.to_string()
cfg_if! {
if #[cfg(feature = "iana-time-zone")] {
static DETECTED_TZ: OnceCell<String> = OnceCell::new();
DETECTED_TZ
.get_or_init(|| match iana_time_zone::get_timezone() {
Ok(timezone) => {
log::info!("detect system timezone is {}", timezone);
timezone
}
Err(e) => {
log::warn!("cannot get timezone, fall back to use GMT+8 as default timezone: {}", e);
DEFAULT_TIMEZONE.to_owned()
}
})
.to_string()
}else {
crate::constants::DEFAULT_TIMEZONE.to_owned()
}
}
#[cfg(not(feature = "iana-time-zone"))]
crate::constants::DEFAULT_TIMEZONE.to_owned()
}

impl BuiltinOption {
Expand Down

0 comments on commit 2ccdff3

Please sign in to comment.