Skip to content

Commit

Permalink
Merge pull request #13 from j2inn/granular-features
Browse files Browse the repository at this point in the history
Enable a more granular feature selection
  • Loading branch information
rracariu authored Oct 17, 2022
2 parents 214d0af + 2affea0 commit f12fd47
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 45 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

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

15 changes: 9 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "libhaystack"
version = "1.0.9"
version = "1.0.10"
description = "Rust implementation of the Haystack 4 data types, defs, filter, units, and encodings"
authors = ["J2 Innovations", "Radu Racariu <radur@j2inn.com>"]
edition = "2021"
Expand Down Expand Up @@ -36,11 +36,14 @@ c-api-zinc = ["zinc"]
c-api-json = ["json"]

# Lib features
filter = ["defs", "zinc-decoding"]
defs = []
units = []
timezone = []
encoders = ["json", "zinc"]
value = ["units", "timezone", "encoders"]
filter = ["value", "defs", "zinc-decoding"]
defs = ["value"]
units = ["units-db"]
units-db = []
timezone = ["timezone-db"]
timezone-db = []
encoders = ["value", "json", "zinc"]
json = ["json-encoding", "json-decoding"]
json-encoding = []
json-decoding = []
Expand Down
1 change: 1 addition & 0 deletions src/haystack/defs/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//! Haystack Def namespace

use dashmap::{mapref::one::Ref as MapReadRef, DashMap};
use lazy_static::lazy_static;
use std::collections::{BTreeMap, HashSet};

use super::misc::parse_multi_line_string_to_dicts;
Expand Down
1 change: 1 addition & 0 deletions src/haystack/filter/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use super::nodes::*;
use super::path::Path;
use crate::haystack::encoding::zinc::decode::scanner::Scanner;
use crate::val::{Symbol, Value};
use lazy_static::lazy_static;

use std::io::{Error, Read};

Expand Down
3 changes: 3 additions & 0 deletions src/haystack/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ pub mod defs;
pub mod encoding;
#[cfg(feature = "filter")]
pub mod filter;
#[cfg(feature = "timezone")]
pub mod timezone;
#[cfg(feature = "units")]
pub mod units;
#[cfg(feature = "value")]
pub mod val;
10 changes: 5 additions & 5 deletions src/haystack/timezone/iana.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use chrono_tz::{OffsetName, Tz, UTC};
use crate::timezone::fixed_timezone;

/// DateTime type that supports timezones
pub(crate) type DateTimeType = StdDateTime<Tz>;
pub type DateTimeType = StdDateTime<Tz>;

pub(crate) fn make_date_time(date: StdDateTime<FixedOffset>) -> Result<DateTimeType, String> {
pub fn make_date_time(date: StdDateTime<FixedOffset>) -> Result<DateTimeType, String> {
use chrono::LocalResult;
if let Ok(tz) = find_timezone(&fixed_timezone(&date.offset().to_string())) {
Ok(match tz.from_local_datetime(&date.naive_local()) {
Expand All @@ -37,15 +37,15 @@ pub fn make_date_time_with_tz(
}
}

pub(crate) fn utc_now() -> DateTimeType {
pub fn utc_now() -> DateTimeType {
Utc::now().with_timezone(&UTC)
}

pub(crate) fn is_utc(date: &DateTimeType) -> bool {
pub fn is_utc(date: &DateTimeType) -> bool {
date.timezone() == UTC
}

pub(crate) fn timezone_short_name(date: &DateTimeType) -> String {
pub fn timezone_short_name(date: &DateTimeType) -> String {
let tz_id = date.offset().tz_id();

tz_id[tz_id.find('/').map_or(0, |v| v + 1)..].to_string()
Expand Down
12 changes: 6 additions & 6 deletions src/haystack/timezone/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
//! provided by [chrono_tz](https://crates.io/crates/chrono-tz), or by just using a fixed offset datetime
//! by toggling the `timezone` feature.

#[cfg(feature = "timezone")]
#[cfg(feature = "timezone-db")]
pub mod iana;
#[cfg(feature = "timezone")]
pub(crate) use iana::*;
#[cfg(not(feature = "timezone"))]
#[cfg(feature = "timezone-db")]
pub use iana::*;
#[cfg(not(feature = "timezone-db"))]
pub mod utc;
#[cfg(not(feature = "timezone"))]
pub(crate) use utc::*;
#[cfg(not(feature = "timezone-db"))]
pub use utc::*;

pub(super) fn fixed_timezone(offset: &str) -> String {
let gmt_offset = offset[2..offset.find(':').unwrap_or(3)].to_string();
Expand Down
15 changes: 6 additions & 9 deletions src/haystack/timezone/utc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,32 @@ use crate::timezone::fixed_timezone;
use chrono::{DateTime as StdDateTime, FixedOffset, TimeZone, Utc};

/// DateTime type that works with a fixed offset
pub(crate) type DateTimeType = StdDateTime<FixedOffset>;
pub type DateTimeType = StdDateTime<FixedOffset>;

pub(crate) fn make_date_time(value: DateTimeType) -> Result<DateTimeType, String> {
pub fn make_date_time(value: DateTimeType) -> Result<DateTimeType, String> {
Ok(value)
}

/// Constructs an UTC datetime as the timezones are not available
/// in this configuration.
pub(crate) fn make_date_time_with_tz(
datetime: &DateTimeType,
_tz: &str,
) -> Result<DateTimeType, String> {
pub fn make_date_time_with_tz(datetime: &DateTimeType, _tz: &str) -> Result<DateTimeType, String> {
use chrono::Offset;
Ok(Utc
.from_utc_datetime(&datetime.naive_utc())
.with_timezone(&Utc.fix()))
}

pub(crate) fn utc_now() -> DateTimeType {
pub fn utc_now() -> DateTimeType {
Utc::now().into()
}

pub(crate) fn is_utc(date: &DateTimeType) -> bool {
pub fn is_utc(date: &DateTimeType) -> bool {
use chrono::Offset;

&Utc.fix() == date.offset()
}

pub(crate) fn timezone_short_name(date: &DateTimeType) -> String {
pub fn timezone_short_name(date: &DateTimeType) -> String {
let tz_id = fixed_timezone(&date.offset().to_string());

tz_id[tz_id.find('/').map_or(0, |v| v + 1)..].to_string()
Expand Down
19 changes: 8 additions & 11 deletions src/haystack/units/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,33 @@

pub mod unit;
pub mod unit_dimension;
#[cfg(feature = "units")]
#[cfg(feature = "units-db")]
pub mod units_generated;
use lazy_static::lazy_static;

pub use unit::Unit;
pub use unit_dimension::UnitDimensions;

/// Get unit by name, if it is defined in the units database
#[allow(unused_variables)]
pub fn get_unit(unit: &str) -> Option<&'static Unit> {
#[cfg(feature = "units")]
#[cfg(feature = "units-db")]
{
return units_generated::UNITS.get(unit).copied();
}
#[cfg(not(feature = "units"))]
#[cfg(not(feature = "units-db"))]
return None;
}

/// Tries to get the unit by name, if none is found, return a default unit
pub fn get_unit_or_default(unit: &str) -> &'static Unit {
if let Some(unit) = get_unit(unit) {
unit
} else {
&*DEFAULT_UNIT
}
get_unit(unit).unwrap_or(&*DEFAULT_UNIT)
}

/// Match units for the dimension
#[allow(unused_variables)]
pub fn match_units(dim: UnitDimensions, scale: f64) -> Vec<&'static Unit> {
#[cfg(feature = "units")]
#[cfg(feature = "units-db")]
{
units_generated::UNITS
.iter()
Expand All @@ -46,11 +43,11 @@ pub fn match_units(dim: UnitDimensions, scale: f64) -> Vec<&'static Unit> {
})
.collect()
}
#[cfg(not(feature = "units"))]
#[cfg(not(feature = "units-db"))]
return Vec::default();
}

#[cfg(feature = "units")]
#[cfg(feature = "units-db")]
fn approx_eq(a: f64, b: f64) -> bool {
if a == b {
return true;
Expand Down
1 change: 1 addition & 0 deletions src/haystack/units/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ mod test {
let meters = get_unit("m").expect("Meters");
let foot = get_unit("ft").expect("Foot");
assert_eq!(meters.convert_to(1.0, foot).map(|f| f.round()), Ok(3.0));
assert_eq!(foot.convert_to(1.0, meters), Ok(0.3048));

let fahrenheit = get_unit("°F").expect("Fahrenheit");
let celsius = get_unit("°C").expect("Celsius");
Expand Down
1 change: 1 addition & 0 deletions src/haystack/units/units_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#![allow(clippy::approx_constant)]
use super::{Unit, UnitDimensions};
use lazy_static::lazy_static;
use std::collections::HashMap;

// dimensionless
Expand Down
5 changes: 0 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ pub mod haystack;

pub use haystack::*;

#[cfg(feature = "timezone")]
extern crate chrono_tz;
#[macro_use]
extern crate lazy_static;

#[cfg(target_arch = "wasm32")]
extern crate web_sys;

Expand Down
1 change: 1 addition & 0 deletions unit-gen/src/unit_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ fn gen_file_header() -> String {
"\n",
"#![allow(clippy::approx_constant)]\n",
"use super::{Unit, UnitDimensions};\n",
"use lazy_static::lazy_static;\n",
"use std::collections::HashMap;\n"
)
.to_string()
Expand Down

0 comments on commit f12fd47

Please sign in to comment.