Add chrono_tz scalar

This commit is contained in:
sunli 2020-03-27 09:03:30 +08:00
parent fb1fef022b
commit dd6af84902
3 changed files with 25 additions and 1 deletions

View File

@ -14,7 +14,7 @@ categories = ["network-programming", "asynchronous"]
readme = "README.md"
[features]
default = ["bson", "uuid", "url", "validators"]
default = ["bson", "uuid", "url", "chrono-tz", "validators"]
validators = ["regex", "once_cell"]
[dependencies]
@ -39,6 +39,7 @@ regex = { version = "1.3.5", optional = true }
bson = { version = "0.14.1", optional = true }
uuid = { version = "0.8.1", optional = true }
url = { version = "2.1.1", optional = true }
chrono-tz = { version = "0.5.1", optional = true }
[dev-dependencies]
async-std = { version = "1.5.0", features = ["attributes"] }

22
src/scalars/chrono_tz.rs Normal file
View File

@ -0,0 +1,22 @@
use crate::{impl_scalar_internal, Result, Scalar, Value};
use chrono_tz::Tz;
use std::str::FromStr;
impl Scalar for Tz {
fn type_name() -> &'static str {
"TimeZone"
}
fn parse(value: &Value) -> Option<Self> {
match value {
Value::String(s) => Some(Tz::from_str(&s).ok()?),
_ => None,
}
}
fn to_json(&self) -> Result<serde_json::Value> {
Ok(Tz::name(self).into())
}
}
impl_scalar_internal!(Tz);

View File

@ -1,4 +1,5 @@
mod bool;
mod chrono_tz;
mod datetime;
mod floats;
mod id;