diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 0f1b7ef8..6543e724 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "zhang" -version = "0.1.0-alpha.4" +version = "0.1.0-alpha.5" authors = ["Kilerd "] description = "a plain text double-accounting tool which is compatible with beancount but more powerful" edition = "2018" diff --git a/core/src/constants.rs b/core/src/constants.rs index 9daee316..db5403f6 100644 --- a/core/src/constants.rs +++ b/core/src/constants.rs @@ -10,6 +10,7 @@ pub const DEFAULT_COMMODITY_PRECISION: i32 = 2; pub const DEFAULT_OPERATING_CURRENCY: &str = "CNY"; pub const DEFAULT_ROUNDING: Rounding = Rounding::RoundDown; pub const DEFAULT_BALANCE_TOLERANCE_PRECISION: i32 = 2; +pub const DEFAULT_TIMEZONE: &str = "Asia/Hong_Kong"; pub const DEFAULT_ROUNDING_PLAIN: &str = "RoundDown"; pub const DEFAULT_COMMODITY_PRECISION_PLAIN: &str = "2"; diff --git a/core/src/options.rs b/core/src/options.rs index 1c9bf349..7ee957a5 100644 --- a/core/src/options.rs +++ b/core/src/options.rs @@ -1,12 +1,14 @@ use itertools::Itertools; -use log::{error, info}; +use log::{error, info, warn}; use sqlx::SqliteConnection; use std::str::FromStr; use std::string::ToString; use strum::{AsRefStr, EnumIter, EnumString, IntoEnumIterator}; use zhang_ast::{Directive, Options, Rounding, SpanInfo, Spanned, ZhangString}; -use crate::constants::{DEFAULT_BALANCE_TOLERANCE_PRECISION_PLAIN, DEFAULT_COMMODITY_PRECISION_PLAIN, DEFAULT_OPERATING_CURRENCY, DEFAULT_ROUNDING_PLAIN}; +use crate::constants::{ + DEFAULT_BALANCE_TOLERANCE_PRECISION_PLAIN, DEFAULT_COMMODITY_PRECISION_PLAIN, DEFAULT_OPERATING_CURRENCY, DEFAULT_ROUNDING_PLAIN, DEFAULT_TIMEZONE, +}; use crate::ZhangResult; use chrono_tz::Tz; @@ -36,9 +38,16 @@ impl BuiltinOption { BuiltinOption::DefaultBalanceTolerancePrecision => DEFAULT_BALANCE_TOLERANCE_PRECISION_PLAIN.to_owned(), BuiltinOption::DefaultCommodityPrecision => DEFAULT_COMMODITY_PRECISION_PLAIN.to_owned(), BuiltinOption::Timezone => { - let system_timezone = iana_time_zone::get_timezone().expect("cannot get the system timezone"); - info!("detect system timezone is {}", system_timezone); - system_timezone + match iana_time_zone::get_timezone() { + Ok(timezone) => { + info!("detect system timezone is {}", timezone); + timezone + } + Err(e) => { + warn!("cannot get timezone, fall back to use GMT+8 as default timezone: {}", e); + DEFAULT_TIMEZONE.to_owned() + } + } } } } diff --git a/core/src/utils/logging.rs b/core/src/utils/logging.rs new file mode 100644 index 00000000..6b68cf37 --- /dev/null +++ b/core/src/utils/logging.rs @@ -0,0 +1,14 @@ +use log::warn; + +pub trait LoggingExit { + fn warn_if_none(self, msg: impl AsRef) -> Self; +} + +impl LoggingExit for Option { + fn warn_if_none(self, msg: impl AsRef) -> Self { + if self.is_none() { + warn!("{}", msg.as_ref()) + } + self + } +} diff --git a/core/src/utils/mod.rs b/core/src/utils/mod.rs index 852d5297..93371900 100644 --- a/core/src/utils/mod.rs +++ b/core/src/utils/mod.rs @@ -7,6 +7,7 @@ pub mod hashmap; pub mod id; pub mod price_grip; pub mod string_; +pub mod logging; pub fn has_path_visited<'a>(visited: impl IntoIterator, path: &Path) -> bool { visited.into_iter().any(|pattern| pattern.matches_path(path))