summaryrefslogtreecommitdiffstats
path: root/crates/core
diff options
context:
space:
mode:
authorLukasz Woznicki <lukasz.woznicki@softwareag.com>2022-02-09 23:35:36 +0000
committerLukasz Woznicki <lukasz.woznicki@softwareag.com>2022-02-14 08:26:22 +0000
commit03124da93bf8788c0b79ce61219e3c8a2c603768 (patch)
tree8ecf531576a39cc209b597bb4cad3c0267c7dcbc /crates/core
parent379ea3fcdaccf2aec01b24d16c485d4bcabd9a58 (diff)
Use 'time' instead of 'chrono' due to CVE for thin_edge_json and all dependent crates
Signed-off-by: Lukasz Woznicki <lukasz.woznicki@softwareag.com>
Diffstat (limited to 'crates/core')
-rw-r--r--crates/core/c8y_smartrest/src/smartrest_deserializer.rs3
-rw-r--r--crates/core/c8y_translator/Cargo.toml3
-rw-r--r--crates/core/c8y_translator/src/json.rs23
-rw-r--r--crates/core/c8y_translator/src/serializer.rs53
-rw-r--r--crates/core/tedge_mapper/Cargo.toml1
-rw-r--r--crates/core/tedge_mapper/src/az_converter.rs4
-rw-r--r--crates/core/tedge_mapper/src/collectd_mapper/batcher.rs7
-rw-r--r--crates/core/tedge_mapper/src/collectd_mapper/collectd.rs40
-rw-r--r--crates/core/tedge_mapper/src/sm_c8y_mapper/mapper.rs4
-rw-r--r--crates/core/thin_edge_json/Cargo.toml4
-rw-r--r--crates/core/thin_edge_json/benches/parsing.rs4
-rw-r--r--crates/core/thin_edge_json/examples/validate.rs4
-rw-r--r--crates/core/thin_edge_json/src/builder.rs7
-rw-r--r--crates/core/thin_edge_json/src/data.rs6
-rw-r--r--crates/core/thin_edge_json/src/group.rs21
-rw-r--r--crates/core/thin_edge_json/src/measurement.rs14
-rw-r--r--crates/core/thin_edge_json/src/parser.rs121
-rw-r--r--crates/core/thin_edge_json/src/serialize.rs52
-rw-r--r--crates/core/thin_edge_json/tests/fixtures/invalid/reject_invalid_timestamp.expected_error2
-rw-r--r--crates/core/thin_edge_json/tests/fixtures/invalid/reject_partial_timestamp.expected_error2
-rw-r--r--crates/core/thin_edge_json/tests/fixtures/invalid/reject_timestamp_missing_time_separator.expected_error2
21 files changed, 195 insertions, 182 deletions
diff --git a/crates/core/c8y_smartrest/src/smartrest_deserializer.rs b/crates/core/c8y_smartrest/src/smartrest_deserializer.rs
index a5265279..0b736767 100644
--- a/crates/core/c8y_smartrest/src/smartrest_deserializer.rs
+++ b/crates/core/c8y_smartrest/src/smartrest_deserializer.rs
@@ -170,8 +170,7 @@ fn to_datetime<'de, D>(deserializer: D) -> Result<OffsetDateTime, D::Error>
where
D: Deserializer<'de>,
{
- // NOTE `OffsetDateTime` is used here because c8y uses for log requests a date time string which
- // does not exactly equal `chrono::DateTime::parse_from_rfc3339`
+ // NOTE `OffsetDateTime` is used here because c8y uses for log requests a date time string which is not compliant with rfc3339
// c8y result:
// 2021-10-23T19:03:26+0100
// rfc3339 expected:
diff --git a/crates/core/c8y_translator/Cargo.toml b/crates/core/c8y_translator/Cargo.toml
index 52e98e04..d0e77df4 100644
--- a/crates/core/c8y_translator/Cargo.toml
+++ b/crates/core/c8y_translator/Cargo.toml
@@ -6,11 +6,11 @@ edition = "2021"
rust-version = "1.58.1"
[dependencies]
-chrono = "0.4"
clock = { path = "../../common/clock" }
json-writer = { path = "../../common/json_writer" }
thin_edge_json = { path = "../thin_edge_json" }
thiserror = "1.0"
+time = "0.3"
[dev-dependencies]
anyhow = "1.0"
@@ -21,6 +21,7 @@ pretty_assertions = "1.0"
proptest = "1.0"
serde_json = "1.0"
test-case = "1.2"
+time = { version = "0.3", features = ["macros"] }
[features]
# use: #[cfg(feature="integration-test")]
diff --git a/crates/core/c8y_translator/src/json.rs b/crates/core/c8y_translator/src/json.rs
index 8c9a2b5b..0ecacd4f 100644
--- a/crates/core/c8y_translator/src/json.rs
+++ b/crates/core/c8y_translator/src/json.rs
@@ -14,9 +14,9 @@
//! ```
use crate::serializer;
-use chrono::prelude::*;
use clock::{Clock, WallClock};
use thin_edge_json::parser::*;
+use time::{self, OffsetDateTime};
#[derive(thiserror::Error, Debug)]
pub enum CumulocityJsonError {
@@ -46,7 +46,7 @@ pub fn from_thin_edge_json_with_child(
fn from_thin_edge_json_with_timestamp(
input: &str,
- timestamp: DateTime<FixedOffset>,
+ timestamp: OffsetDateTime,
maybe_child_id: Option<&str>,
) -> Result<String, CumulocityJsonError> {
let mut serializer = serializer::C8yJsonSerializer::new(timestamp, maybe_child_id);
@@ -58,8 +58,10 @@ fn from_thin_edge_json_with_timestamp(
mod tests {
use super::*;
use assert_json_diff::*;
+ use proptest::prelude::*;
use serde_json::{json, Value};
use test_case::test_case;
+ use time::{format_description, macros::datetime};
#[test]
fn check_single_value_translation() {
@@ -68,14 +70,17 @@ mod tests {
"pressure": 220.0
}"#;
- let timestamp = FixedOffset::east(5 * 3600).ymd(2021, 4, 8).and_hms(0, 0, 0);
+ let timestamp = datetime!(2021-04-08 0:00:0 +05:00);
let output =
from_thin_edge_json_with_timestamp(single_value_thin_edge_json, timestamp, None);
let expected_output = json!({
"type": "ThinEdgeMeasurement",
- "time": timestamp.to_rfc3339(),
+ "time": timestamp
+ .format(&format_description::well_known::Rfc3339)
+ .unwrap()
+ .as_str(),
"temperature": {
"temperature": {
"value": 23.0
@@ -137,14 +142,17 @@ mod tests {
"pressure": 98.0
}"#;
- let timestamp = FixedOffset::east(5 * 3600).ymd(2021, 4, 8).and_hms(0, 0, 0);
+ let timestamp = datetime!(2021-04-08 0:00:0 +05:00);
let output =
from_thin_edge_json_with_timestamp(multi_value_thin_edge_json, timestamp, None);
let expected_output = json!({
"type": "ThinEdgeMeasurement",
- "time": timestamp.to_rfc3339(),
+ "time": timestamp
+ .format(&format_description::well_known::Rfc3339)
+ .unwrap()
+ .as_str(),
"temperature": {
"temperature": {
"value": 25.0
@@ -200,7 +208,6 @@ mod tests {
actual_output
);
}
- use proptest::prelude::*;
proptest! {
@@ -270,7 +277,7 @@ mod tests {
thin_edge_json: &str,
expected_output: Value,
) {
- let timestamp = FixedOffset::east(5 * 3600).ymd(2021, 4, 8).and_hms(0, 0, 0);
+ let timestamp = datetime!(2021-04-08 0:00:0 +05:00);
let output = from_thin_edge_json_with_timestamp(thin_edge_json, timestamp, Some(child_id));
assert_json_eq!(
serde_json::from_str::<serde_json::Value>(output.unwrap().as_str()).unwrap(),
diff --git a/crates/core/c8y_translator/src/serializer.rs b/crates/core/c8y_translator/src/serializer.rs
index 96a3e507..e268235a 100644
--- a/crates/core/c8y_translator/src/serializer.rs
+++ b/crates/core/c8y_translator/src/serializer.rs
@@ -1,12 +1,12 @@
-use chrono::prelude::*;
use json_writer::{JsonWriter, JsonWriterError};
use thin_edge_json::measurement::MeasurementVisitor;
+use time::{format_description, OffsetDateTime};
pub struct C8yJsonSerializer {
json: JsonWriter,
is_within_group: bool,
timestamp_present: bool,
- default_timestamp: DateTime<FixedOffset>,
+ default_timestamp: OffsetDateTime,
}
#[derive(thiserror::Error, Debug)]
@@ -34,7 +34,7 @@ pub enum MeasurementStreamError {
}
impl C8yJsonSerializer {
- pub fn new(default_timestamp: DateTime<FixedOffset>, maybe_child_id: Option<&str>) -> Self {
+ pub fn new(default_timestamp: OffsetDateTime, maybe_child_id: Option<&str>) -> Self {
let capa = 1024; // XXX: Choose a capacity based on expected JSON length.
let mut json = JsonWriter::with_capacity(capa);
@@ -96,13 +96,18 @@ impl C8yJsonSerializer {
impl MeasurementVisitor for C8yJsonSerializer {
type Error = C8yJsonSerializationError;
- fn visit_timestamp(&mut self, timestamp: DateTime<FixedOffset>) -> Result<(), Self::Error> {
+ fn visit_timestamp(&mut self, timestamp: OffsetDateTime) -> Result<(), Self::Error> {
if self.is_within_group {
return Err(MeasurementStreamError::UnexpectedTimestamp.into());
}
self.json.write_key("time")?;
- self.json.write_str(timestamp.to_rfc3339().as_str())?;
+ self.json.write_str(
+ timestamp
+ .format(&format_description::well_known::Rfc3339)
+ .unwrap()
+ .as_str(),
+ )?;
self.timestamp_present = true;
Ok(())
@@ -146,18 +151,16 @@ impl MeasurementVisitor for C8yJsonSerializer {
#[cfg(test)]
mod tests {
+ use ::time::macros::datetime;
use assert_json_diff::*;
use assert_matches::*;
use serde_json::json;
use super::*;
- use chrono::offset::FixedOffset;
#[test]
fn serialize_single_value_message() -> anyhow::Result<()> {
- let timestamp = FixedOffset::east(5 * 3600)
- .ymd(2021, 6, 22)
- .and_hms_nano(17, 3, 14, 123456789);
+ let timestamp = datetime!(2021-06-22 17:03:14.123456789 +05:00);
let mut serializer = C8yJsonSerializer::new(timestamp, None);
serializer.visit_timestamp(timestamp)?;
@@ -183,9 +186,7 @@ mod tests {
}
#[test]
fn serialize_multi_value_message() -> anyhow::Result<()> {
- let timestamp = FixedOffset::east(5 * 3600)
- .ymd(2021, 6, 22)
- .and_hms_nano(17, 3, 14, 123456789);
+ let timestamp = datetime!(2021-06-22 17:03:14.123456789 +05:00);
let mut serializer = C8yJsonSerializer::new(timestamp, None);
serializer.visit_timestamp(timestamp)?;
@@ -236,9 +237,7 @@ mod tests {
#[test]
fn serialize_empty_message() -> anyhow::Result<()> {
- let timestamp = FixedOffset::east(5 * 3600)
- .ymd(2021, 6, 22)
- .and_hms_nano(17, 3, 14, 123456789);
+ let timestamp = datetime!(2021-06-22 17:03:14.123456789 +05:00);
let mut serializer = C8yJsonSerializer::new(timestamp, None);
@@ -257,9 +256,7 @@ mod tests {
#[test]
fn serialize_timestamp_message() -> anyhow::Result<()> {
- let timestamp = FixedOffset::east(5 * 3600)
- .ymd(2021, 6, 22)
- .and_hms_nano(17, 3, 14, 123456789);
+ let timestamp = datetime!(2021-06-22 17:03:14.123456789 +05:00);
let mut serializer = C8yJsonSerializer::new(timestamp, None);
serializer.visit_timestamp(timestamp)?;
@@ -281,9 +278,7 @@ mod tests {
#[test]
fn serialize_timestamp_within_group() -> anyhow::Result<()> {
- let timestamp = FixedOffset::east(5 * 3600)
- .ymd(2021, 6, 22)
- .and_hms_nano(17, 3, 14, 123456789);
+ let timestamp = datetime!(2021-06-22 17:03:14.123456789 +05:00);
let mut serializer = C8yJsonSerializer::new(timestamp, None);
serializer.visit_start_group("location")?;
@@ -301,9 +296,7 @@ mod tests {
#[test]
fn serialize_unexpected_end_of_group() -> anyhow::Result<()> {
- let timestamp = FixedOffset::east(5 * 3600)
- .ymd(2021, 6, 22)
- .and_hms_nano(17, 3, 14, 123456789);
+ let timestamp = datetime!(2021-06-22 17:03:14.123456789 +05:00);
let mut serializer = C8yJsonSerializer::new(timestamp, None);
serializer.visit_measurement("alti", 2100.4)?;
@@ -323,9 +316,7 @@ mod tests {
#[test]
fn serialize_unexpected_start_of_group() -> anyhow::Result<()> {
- let timestamp = FixedOffset::east(5 * 3600)
- .ymd(2021, 6, 22)
- .and_hms_nano(17, 3, 14, 123456789);
+ let timestamp = datetime!(2021-06-22 17:03:14.123456789 +05:00);
let mut serializer = C8yJsonSerializer::new(timestamp, None);
serializer.visit_start_group("location")?;
@@ -346,9 +337,7 @@ mod tests {
#[test]
fn serialize_unexpected_end_of_message() -> anyhow::Result<()> {
- let timestamp = FixedOffset::east(5 * 3600)
- .ymd(2021, 6, 22)
- .and_hms_nano(17, 3, 14, 123456789);
+ let timestamp = datetime!(2021-06-22 17:03:14.123456789 +05:00);
let mut serializer = C8yJsonSerializer::new(timestamp, None);
serializer.visit_start_group("location")?;
@@ -369,9 +358,7 @@ mod tests {
#[test]
fn serialize_timestamp_child_message() -> anyhow::Result<()> {
- let timestamp = FixedOffset::east(5 * 3600)
- .ymd(2021, 6, 22)
- .and_hms_nano(17, 3, 14, 123456789);
+ let timestamp = datetime!(2021-06-22 17:03:14.123456789 +05:00);
let mut serializer = C8yJsonSerializer::new(timestamp, Some("child1"));
serializer.visit_timestamp(timestamp)?;
diff --git a/crates/core/tedge_mapper/Cargo.toml b/crates/core/tedge_mapper/Cargo.toml
index c1a13215..baf8403f 100644
--- a/crates/core/tedge_mapper/Cargo.toml
+++ b/crates/core/tedge_mapper/Cargo.toml
@@ -32,7 +32,6 @@ async-trait = "0.1"
batcher = { path = "../../common/batcher" }
c8y_smartrest = { path = "../c8y_smartrest" }
c8y_translator = { path = "../c8y_translator" }
-chrono = "0.4"
clock = { path = "../../common/clock" }
csv = "1.1"
download = { path = "../../common/download" }
diff --git a/crates/core/tedge_mapper/src/az_converter.rs b/crates/core/tedge_mapper/src/az_converter.rs
index 27e6c1e6..d6ca77dc 100644
--- a/crates/core/tedge_mapper/src/az_converter.rs
+++ b/crates/core/tedge_mapper/src/az_converter.rs
@@ -53,15 +53,15 @@ mod tests {
use crate::size_threshold::SizeThresholdExceeded;
use assert_json_diff::*;
use assert_matches::*;
- use chrono::{FixedOffset, TimeZone};
use mqtt_channel::Topic;
use serde_json::json;
+ use time::macros::datetime;
struct TestClock;
impl Clock for TestClock {
fn now(&self) -> clock::Timestamp {
- FixedOffset::east(5 * 3600).ymd(2021, 4, 8).and_hms(0, 0, 0)
+ datetime!(2021-04-08 00:00:00 +05:00)
}
}
diff --git a/crates/core/tedge_mapper/src/collectd_mapper/batcher.rs b/crates/core/tedge_mapper/src/collectd_mapper/batcher.rs
index e7ecd830..9844485e 100644
--- a/crates/core/tedge_mapper/src/collectd_mapper/batcher.rs
+++ b/crates/core/tedge_mapper/src/collectd_mapper/batcher.rs
@@ -7,7 +7,6 @@ use thin_edge_json::{
};
use crate::collectd_mapper::{collectd::CollectdMessage, error::DeviceMonitorError};
-use chrono::Local;
use thin_edge_json::group::MeasurementGrouperError;
#[derive(Debug)]
@@ -22,7 +21,7 @@ impl MessageBatch {
let mut messages = messages.into_iter();
if let Some(first_message) = messages.next() {
- let timestamp = first_message.timestamp.with_timezone(Local::now().offset());
+ let timestamp = first_message.timestamp;
let mut batch = MessageBatch::start_batch(first_message, timestamp)?;
for message in messages {
batch.add_to_batch(message)?;
@@ -72,12 +71,12 @@ impl MessageBatch {
mod tests {
use super::*;
use assert_matches::assert_matches;
- use chrono::{TimeZone, Utc};
use clock::{Clock, WallClock};
+ use time::macros::datetime;
#[test]
fn test_message_batch_processor() -> anyhow::Result<()> {
- let timestamp = Utc.ymd(2015, 5, 15).and_hms_milli(0, 0, 1, 444);
+ let timestamp = datetime!(2015-05-15 0:00:01.444 UTC);
let collectd_message = CollectdMessage::new("temperature", "value", 32.5, timestamp);
let mut message_batch = MessageBatch::start_batch(collectd_message, WallClock.now())?;
diff --git a/crates/core/tedge_mapper/src/collectd_mapper/collectd.rs b/crates/core/tedge_mapper/src/collectd_mapper/collectd.rs
index ab98f67d..0bb0fe22 100644
--- a/crates/core/tedge_mapper/src/collectd_mapper/collectd.rs
+++ b/crates/core/tedge_mapper/src/collectd_mapper/collectd.rs
@@ -1,13 +1,13 @@
use batcher::Batchable;
-use chrono::{DateTime, NaiveDateTime, Utc};
use mqtt_channel::Message;
use thin_edge_json::measurement::MeasurementVisitor;
+use time::{Duration, OffsetDateTime};
#[derive(Debug)]
pub struct CollectdMessage {
pub metric_group_key: String,
pub metric_key: String,
- pub timestamp: DateTime<Utc>,
+ pub timestamp: OffsetDateTime,
pub metric_value: f64,
}
@@ -43,7 +43,7 @@ impl CollectdMessage {
metric_group_key: &str,
metric_key: &str,
metric_value: f64,
- timestamp: DateTime<Utc>,
+ timestamp: OffsetDateTime,
) -> Self {
Self {
metric_group_key: metric_group_key.to_string(),
@@ -167,10 +167,11 @@ impl CollectdPayload {
})
}
- pub fn timestamp(&self) -> DateTime<Utc> {
+ pub fn timestamp(&self) -> OffsetDateTime {
let timestamp = self.timestamp.trunc() as i64;
let nanoseconds = (self.timestamp.fract() * 1.0e9) as u32;
- DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(timestamp, nanoseconds), Utc)
+ OffsetDateTime::from_unix_timestamp(timestamp).unwrap()
+ + Duration::nanoseconds(nanoseconds as i64)
}
}
@@ -181,18 +182,17 @@ impl Batchable for CollectdMessage {
format!("{}/{}", &self.metric_group_key, &self.metric_key)
}
- fn event_time(&self) -> DateTime<Utc> {
+ fn event_time(&self) -> OffsetDateTime {
self.timestamp
}
}
#[cfg(test)]
mod tests {
- use std::ops::Index;
-
use assert_matches::assert_matches;
- use chrono::TimeZone;
use mqtt_channel::Topic;
+ use std::ops::Index;
+ use time::macros::datetime;
use super::*;
@@ -212,10 +212,7 @@ mod tests {
assert_eq!(metric_group_key, "temperature");
assert_eq!(metric_key, "value");
- assert_eq!(
- *timestamp,
- Utc.ymd(1973, 11, 29).and_hms_milli(21, 33, 09, 0)
- );
+ assert_eq!(*timestamp, datetime!(1973-11-29 21:33:09.0 UTC));
assert_eq!(*metric_value, 32.5);
}
@@ -230,15 +227,12 @@ mod tests {
metric_group_key,
metric_key,
timestamp,
- metric_value,
+ metric_value: _,
} = collectd_message.index(0);
assert_eq!(metric_group_key, "temperature");
assert_eq!(metric_key, "value_val1");
- assert_eq!(
- *timestamp,
- Utc.ymd(1973, 11, 29).and_hms_milli(21, 33, 09, 0)
- );
+ assert_eq!(*timestamp, datetime!(1973-11-29 21:33:09.0 UTC));
let CollectdMessage {
metric_group_key,
@@ -249,10 +243,7 @@ mod tests {
assert_eq!(metric_group_key, "temperature");
assert_eq!(metric_key, "value_val2");
- assert_eq!(
- *timestamp,
- Utc.ymd(1973, 11, 29).and_hms_milli(21, 33, 09, 0)
- );
+ assert_eq!(*timestamp, datetime!(1973-11-29 21:33:09.0 UTC));
assert_eq!(*metric_value, 45.2);
}
@@ -272,10 +263,7 @@ mod tests {
assert_eq!(metric_group_key, "temperature");
assert_eq!(metric_key, "value");
- assert_eq!(
- *timestamp,
- Utc.ymd(1973, 11, 29).and_hms_milli(21, 33, 09, 125)
- );
+ assert_eq!(*timestamp, datetime!(1973-11-29 21:33:09.125 UTC));
assert_eq!(*metric_value, 32.5);
}
diff --git a/crates/core/tedge_mapper/src/sm_c8y_mapper/mapper.rs b/crates/core/tedge_mapper/src/sm_c8y_mapper/mapper.rs
index 251e0f90..a778f86b 100644
--- a/crates/core/tedge_mapper/src/sm_c8y_mapper/mapper.rs
+++ b/crates/core/tedge_mapper/src/sm_c8y_mapper/mapper.rs
@@ -618,7 +618,7 @@ mod tests {
#[test_case("/path/to/another-variant-2021-10-25T07:45:41Z.log")]
#[test_case("/yet-another-variant-2021-10-25T07:45:41Z.log")]
fn test_datetime_parsing_from_path(file_path: &str) {
- // checking that `get_date_from_file_path` unwraps a `chrono::NaiveDateTime` object.
+ // checking that `get_date_from_file_path` unwraps a `OffsetDateTime` object.
// this should return an Ok Result.
let path_buf = PathBuf::from_str(file_path).unwrap();
let path_buf_datetime = get_datetime_from_file_path(&path_buf);
@@ -630,7 +630,7 @@ mod tests {
#[test_case("/path/to/another-variant-07:45:41Z-2021-10-25T.log")]
#[test_case("/yet-another-variant-2021-10-25T07:45Z.log")]
fn test_datetime_parsing_from_path_fail(file_path: &str) {
- // checking that `get_date_from_file_path` unwraps a `chrono::NaiveDateTime` object.
+ // checking that `get_date_from_file_path` unwraps a `OffsetDateTime` object.
// this should return an err.
let path_buf = PathBuf::from_str(file_path).unwrap();
let path_buf_datetime = get_datetime_from_file_path(&path_buf);
diff --git a/crates/core/thin_edge_json/Cargo.toml b/crates/core/thin_edge_json/Cargo.toml
index 5ce83271..9ed0471a 100644
--- a/crates/core/thin_edge_json/Cargo.toml
+++ b/crates/core/thin_edge_json/Cargo.toml
@@ -8,13 +8,12 @@ rust-version = "1.58.1"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
-time = { version = "0.3", features = ["macros"] }
clock = { path = "../../common/clock" }
-chrono = "0.4"
json-writer = { path = "../../common/json_writer" }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1"
thiserror = "1.0"
+time = { version = "0.3", features = ["formatting", "local-offset", "parsing", "serde"] }
[dev-dependencies]
anyhow = "1.0"
@@ -26,6 +25,7 @@ stats_alloc = "0.1"
walkdir = "2"
assert_matches = "1.5"
test-case = "1.2"
+time = { version = "0.3", features = ["macros"] }
[[bench]]
name = "parsing"
diff --git a/crates/core/thin_edge_json/benches/parsing.rs b/crates/core/thin_edge_json/benches/parsing.rs
index b4294cb6..bdd6871f 100644
--- a/crates/core/thin_edge_json/benches/parsing.rs
+++ b/crates/core/thin_edge_json/benches/parsing.rs
@@ -1,6 +1,6 @@
-use chrono::prelude::*;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use thin_edge_json::measurement::MeasurementVisitor;
+use time::OffsetDateTime;
const INPUT: &str = r#"{
"time" : "2021-04-30T17:03:14.123+02:00",
@@ -26,7 +26,7 @@ struct DummyVisitor;
impl MeasurementVisitor for DummyVisitor {
type Error = DummyError;
- fn visit_timestamp(&mut self, _value: DateTime<FixedOffset>) -> Result<(), Self::Error> {
+ fn visit_timestamp(&mut self, _value: OffsetDateTime) -> Result<(), Self::Error> {
Ok(())
}
fn visit_measurement(&mut self, _name: &str, _value: f64) -> Result<(), Self::Error> {
diff --git a/crates/core/thin_edge_json/examples/validate.rs b/crates/core/thin_edge_json/examples/validate.rs
index 49560051..cbab5138 100644
--- a/crates/core/thin_edge_json/examples/validate.rs
+++ b/crates/core/thin_edge_json/examples/validate.rs
@@ -1,6 +1,6 @@
-use chrono::prelude::*;
use std::env;
use thin_edge_json::measurement::MeasurementVisitor;
+use time::OffsetDateTime;
#[global_allocator]
static GLOBAL: &stats_alloc::StatsAlloc<std::alloc::System> = &stats_alloc::INSTRUMENTED_SYSTEM;
@@ -36,7 +36,7 @@ struct DummyVisitor;
impl MeasurementVisitor for DummyVisitor {
type Error = DummyError;
- fn visit_timestamp(&mut self, _value: DateTime<FixedOffset>) -> Result<(), Self::Error> {
+ fn visit_timestamp(&mut self, _value: OffsetDateTime) -> Result<(), Self::Error> {
Ok(())
}
fn visit_measurement(&mut self, _name: &str, _value: f64) -> Result<(), Self::Error> {
diff --git a/crates/core/thin_edge_json/src/builder.rs b/crates/core/thin_edge_json/src/builder.rs
index 098c7304..cb4b9ee6 100644
--- a/crates/core/thin_edge_json/src/builder.rs
+++ b/crates/core/thin_edge_json/src/builder.rs
@@ -1,9 +1,10 @@
+use time::OffsetDateTime;
+
use crate::{data::*, measurement::*};
-use chrono::prelude::*;
/// A `MeasurementVisitor` that builds up `ThinEdgeJson`.
pub struct ThinEdgeJsonBuilder {
- timestamp: Option<DateTime<FixedOffset>>,
+ timestamp: Option<OffsetDateTime>,
inside_group: Option<MultiValueMeasurement>,
measurements: Vec<ThinEdgeValue>,
}
@@ -36,7 +37,7 @@ impl ThinEdgeJsonBuilder {
impl MeasurementVisitor for ThinEdgeJsonBuilder {
type Error = ThinEdgeJsonBuilderError;
- fn visit_timestamp(&mut self, value: DateTime<FixedOffset>) -> Result<(), Self::Error> {
+ fn visit_timestamp(&mut self, value: OffsetDateTime) -> Result<(), Self::Error> {
match self.timestamp {
None => {
self.timestamp = Some(value);
diff --git a/crates/core/thin_edge_json/src/data.rs b/crates/core/thin_edge_json/src/data.rs
index 4fa25cf4..ef3245e3 100644
--- a/crates/core/thin_edge_json/src/data.rs
+++ b/crates/core/thin_edge_json/src/data.rs
@@ -1,11 +1,11 @@
//! The in-memory data model representing ThinEdge JSON.
-use chrono::prelude::*;
+use time::OffsetDateTime;
/// In-memory representation of parsed ThinEdge JSON.
#[derive(Debug)]
pub struct ThinEdgeJson {
- pub timestamp: Option<DateTime<FixedOffset>>,
+ pub timestamp: Option<OffsetDateTime>,
pub values: Vec<ThinEdgeValue>,
}
@@ -14,7 +14,7 @@ impl ThinEdgeJson {
self.timestamp.is_some()
}
- pub fn set_timestamp(&mut self, timestamp: DateTime<FixedOffset>) {
+ pub fn set_timestamp(&mut self, timestamp: OffsetDateTime) {
self.timestamp = Some(timestamp)
}
}
diff --git a/crates/core/thin_edge_json/src/group.rs b/crates/core/thin_edge_json/src/group.rs
index 17488a3a..706812dc 100644
--- a/crates/core/thin_edge_json/src/group.rs
+++ b/crates/core/thin_edge_json/src/group.rs
@@ -1,12 +1,11 @@
-use chrono::offset::FixedOffset;
-use chrono::DateTime;
use std::collections::HashMap;
+use time::OffsetDateTime;
use crate::measurement::MeasurementVisitor;
#[derive(Debug)]
pub struct MeasurementGroup {
- timestamp: Option<DateTime<FixedOffset>>,
+ timestamp: Option<OffsetDateTime>,
values: HashMap<String, Measurement>,
}
@@ -18,7 +17,7 @@ impl MeasurementGroup {
}
}
- pub fn timestamp(&self) -> Option<DateTime<FixedOffset>> {
+ pub fn timestamp(&self) -> Option<OffsetDateTime> {
self.timestamp
}
@@ -138,7 +137,7 @@ impl Default for MeasurementGrouper {
impl MeasurementVisitor for MeasurementGrouper {
type Error = MeasurementGrouperError;
- fn visit_timestamp(&mut self, time: DateTime<FixedOffset>) -> Result<(), Self::Error> {
+ fn visit_timestamp(&mut self, time: OffsetDateTime) -> Result<(), Self::Error> {
self.measurement_group.timestamp = Some(time);
Ok(())
}
@@ -192,9 +191,9 @@ impl MeasurementVisitor for MeasurementGrouper {
#[cfg(test)]
mod tests {
use super::*;
- use chrono::prelude::*;
use mockall::predicate::*;
use mockall::*;
+ use time::{macros::datetime, Duration};
#[derive(thiserror::Error, Debug, Clone)]
pub enum TestError {
@@ -209,7 +208,7 @@ mod tests {
impl MeasurementVisitor for GroupedVisitor {
type Error = TestError;
- fn visit_timestamp(&mut self, value: DateTime<FixedOffset>) -> Result<(), TestError>;
+ fn visit_timestamp(&mut self,