Skip to content

Commit

Permalink
Move under tedge_api::measurement all related structs
Browse files Browse the repository at this point in the history
Signed-off-by: Didier Wenzek <[email protected]>
  • Loading branch information
didier-wenzek committed Apr 9, 2024
1 parent 310e0d1 commit 4a31a93
Show file tree
Hide file tree
Showing 14 changed files with 39 additions and 32 deletions.
6 changes: 0 additions & 6 deletions crates/core/tedge_api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
pub mod alarm;
pub mod builder;
pub mod data;
pub mod entity_store;
pub mod error;
pub mod event;
pub mod group;
pub mod health;
pub mod measurement;
pub mod messages;
pub mod mqtt_topics;
pub mod parser;
pub mod path;
pub mod serialize;
mod software;
mod store;
pub mod utils;
pub mod workflow;

pub use download::*;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use time::OffsetDateTime;

use crate::data::*;
use crate::measurement::data::*;
use crate::measurement::*;

/// A `MeasurementVisitor` that builds up `ThinEdgeJson`.
Expand Down
File renamed without changes.
File renamed without changes.
13 changes: 13 additions & 0 deletions crates/core/tedge_api/src/measurement/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
use time::OffsetDateTime;

#[cfg(test)]
pub mod builder;
#[cfg(test)]
mod data;
mod group;
mod parser;
mod serialize;
pub(crate) mod utils;

pub use group::*;
pub use parser::*;
pub use serialize::*;

/// The `MeasurementVisitor` trait represents the capability to visit a series of measurements, possibly grouped.
///
/// Here is an implementation of the `MeasurementVisitor` trait that prints the measurements:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ fn invalid_empty_measurement(key: &str) -> String {
fn map_error(error: serde_json::Error, input: &str) -> ThinEdgeJsonParserError {
const MAX_INPUT_EXCERPT: usize = 80;
let input_excerpt =
crate::utils::excerpt(input, error.line(), error.column(), MAX_INPUT_EXCERPT);
crate::measurement::utils::excerpt(input, error.line(), error.column(), MAX_INPUT_EXCERPT);
ThinEdgeJsonParserError {
error,
input_excerpt,
Expand All @@ -287,11 +287,11 @@ mod tests {
use time::macros::datetime;
use time::OffsetDateTime;

use crate::parser::parse_str;
use super::parse_str;

#[test]
fn it_deserializes_thin_edge_json() -> anyhow::Result<()> {
use crate::builder::ThinEdgeJsonBuilder;
use crate::measurement::builder::ThinEdgeJsonBuilder;
let input = r#"{
"time" : "2021-04-30T17:03:14.123+02:00",
"pressure": 123.4,
Expand Down Expand Up @@ -333,7 +333,7 @@ mod tests {

#[test]
fn it_shows_input_excerpt_on_error() {
use crate::builder::ThinEdgeJsonBuilder;
use crate::measurement::builder::ThinEdgeJsonBuilder;

let input = "{\n\"time\" : null\n}";

Expand All @@ -349,7 +349,7 @@ mod tests {

#[test]
fn it_accepts_unix_timestamps_in_place_of_iso_8601() {
use crate::builder::ThinEdgeJsonBuilder;
use crate::measurement::builder::ThinEdgeJsonBuilder;

let input = "{\n\"time\" : 1701949168,\n\"test\": 1023}";
let expected_timestamp = OffsetDateTime::parse(
Expand All @@ -367,7 +367,7 @@ mod tests {

#[test]
fn it_accepts_decimals_as_unix_timestamps() {
use crate::builder::ThinEdgeJsonBuilder;
use crate::measurement::builder::ThinEdgeJsonBuilder;

let input = "{\n\"time\" : 1701949168.001,\n\"test\": 1023}";
let expected_timestamp = OffsetDateTime::parse(
Expand All @@ -385,7 +385,7 @@ mod tests {

#[test]
fn it_produces_a_clear_error_message_when_unix_timestamp_is_out_of_range() {
use crate::builder::ThinEdgeJsonBuilder;
use crate::measurement::builder::ThinEdgeJsonBuilder;

let input = "{\n\"time\" : -377705116801,\n\"test\": 1023}";
let mut builder = ThinEdgeJsonBuilder::default();
Expand All @@ -400,7 +400,7 @@ mod tests {

#[test]
fn parse_type_as_measurement() {
use crate::builder::ThinEdgeJsonBuilder;
use crate::measurement::builder::ThinEdgeJsonBuilder;

let input = r#"{
"time" : "2021-04-30T17:03:14.123+02:00",
Expand Down
File renamed without changes.
File renamed without changes.
10 changes: 5 additions & 5 deletions crates/core/tedge_api/tests/test_suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ fn it_rejects_invalid_thin_edge_json() -> anyhow::Result<()> {
println!("Fixture: {:?}", fixture.path());

let res: anyhow::Result<_> = {
let mut builder = tedge_api::builder::ThinEdgeJsonBuilder::default();
tedge_api::parser::parse_str(&input, &mut builder)
let mut builder = tedge_api::measurement::ThinEdgeJsonSerializer::new();
tedge_api::measurement::parse_str(&input, &mut builder)
.map_err(Into::into)
.and_then(|_| builder.done().map_err(Into::into))
.and_then(|_| builder.into_string().map_err(Into::into))
};

let err_msg = res.unwrap_err().to_string();
Expand Down Expand Up @@ -43,8 +43,8 @@ fn it_transforms_valid_thin_edge_json() -> anyhow::Result<()> {
let input = std::fs::read_to_string(fixture.path())?;

let output = {
let mut builder = tedge_api::serialize::ThinEdgeJsonSerializer::new();
let res = tedge_api::parser::parse_str(&input, &mut builder);
let mut builder = tedge_api::measurement::ThinEdgeJsonSerializer::new();
let res = tedge_api::measurement::parse_str(&input, &mut builder);
assert!(res.is_ok());
builder.into_string()?
};
Expand Down
4 changes: 2 additions & 2 deletions crates/extensions/aws_mapper_ext/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use tedge_api::serialize::ThinEdgeJsonSerializationError;
use tedge_api::measurement::ThinEdgeJsonSerializationError;
use tedge_mqtt_ext::MqttError;

#[derive(Debug, thiserror::Error)]
Expand All @@ -7,7 +7,7 @@ pub enum ConversionError {
FromThinEdgeJsonSerialization(#[from] ThinEdgeJsonSerializationError),

#[error(transparent)]
FromThinEdgeJsonParser(#[from] tedge_api::parser::ThinEdgeJsonParserError),
FromThinEdgeJsonParser(#[from] tedge_api::measurement::ThinEdgeJsonParserError),

#[error("The size of the message received on {topic} is {actual_size} which is greater than the threshold size of {threshold}.")]
SizeThresholdExceeded {
Expand Down
4 changes: 2 additions & 2 deletions crates/extensions/c8y_mapper_ext/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use c8y_http_proxy::messages::C8YRestError;
use plugin_sm::operation_logs::OperationLogsError;
use std::path::PathBuf;
use tedge_api::entity_store::InvalidExternalIdError;
use tedge_api::serialize::ThinEdgeJsonSerializationError;
use tedge_api::measurement::ThinEdgeJsonSerializationError;
use tedge_config::TEdgeConfigError;
use tedge_mqtt_ext::MqttError;
use tedge_utils::file::FileError;
Expand Down Expand Up @@ -59,7 +59,7 @@ pub enum ConversionError {
),

#[error(transparent)]
FromThinEdgeJsonParser(#[from] tedge_api::parser::ThinEdgeJsonParserError),
FromThinEdgeJsonParser(#[from] tedge_api::measurement::ThinEdgeJsonParserError),

#[error(transparent)]
SizeThresholdExceeded(#[from] SizeThresholdExceededError),
Expand Down
2 changes: 1 addition & 1 deletion crates/extensions/c8y_mapper_ext/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::serializer;
use clock::Clock;
use clock::WallClock;
use tedge_api::entity_store::EntityMetadata;
use tedge_api::parser::*;
use tedge_api::measurement::*;
use time::OffsetDateTime;
use time::{self};

Expand Down
8 changes: 4 additions & 4 deletions crates/extensions/collectd_ext/src/batcher.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use clock::Timestamp;
use tedge_api::group::MeasurementGroup;
use tedge_api::group::MeasurementGrouper;
use tedge_api::group::MeasurementGrouperError;
use tedge_api::measurement::MeasurementGroup;
use tedge_api::measurement::MeasurementGrouper;
use tedge_api::measurement::MeasurementGrouperError;
use tedge_api::measurement::MeasurementVisitor;
use tedge_api::serialize::ThinEdgeJsonSerializer;
use tedge_api::measurement::ThinEdgeJsonSerializer;
use tedge_mqtt_ext::MqttMessage;
use tedge_mqtt_ext::Topic;

Expand Down
6 changes: 3 additions & 3 deletions crates/extensions/collectd_ext/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ pub enum DeviceMonitorError {
FromInvalidCollectdMeasurement(#[from] crate::collectd::CollectdError),

#[error(transparent)]
FromInvalidThinEdgeJson(#[from] tedge_api::group::MeasurementGrouperError),
FromInvalidThinEdgeJson(#[from] tedge_api::measurement::MeasurementGrouperError),

#[error(transparent)]
FromThinEdgeJsonSerializationError(
#[from] tedge_api::serialize::ThinEdgeJsonSerializationError,
#[from] tedge_api::measurement::ThinEdgeJsonSerializationError,
),

#[error(transparent)]
FromBatchingError(#[from] SendError<tedge_api::group::MeasurementGrouper>),
FromBatchingError(#[from] SendError<tedge_api::measurement::MeasurementGrouper>),
}

impl From<DeviceMonitorError> for RuntimeError {
Expand Down

0 comments on commit 4a31a93

Please sign in to comment.