From 629bff2179b3e05d9024849abc5f11d916a82e42 Mon Sep 17 00:00:00 2001 From: Joshua Thayer Date: Thu, 7 Nov 2024 15:45:09 -0800 Subject: [PATCH 1/2] Adds support for currencies in openleadr-wire using iso_currency crate Signed-off-by: Joshua Thayer --- Cargo.lock | 22 +++++++++++++++++++++ Cargo.toml | 1 + openleadr-wire/Cargo.toml | 1 + openleadr-wire/src/event.rs | 39 ++++++++++++++++++++++++++++++------- 4 files changed, 56 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3e737d2..c07aa77 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1134,6 +1134,27 @@ dependencies = [ "nom", ] +[[package]] +name = "iso_country" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20633e788d3948ea7336861fdb09ec247f5dae4267e8f0743fa97de26c28624d" +dependencies = [ + "lazy_static", +] + +[[package]] +name = "iso_currency" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f402cc74ed2fec6f6fed285d45645a8c7c82e1d1fa694898864ef7c0cb047785" +dependencies = [ + "iso_country", + "proc-macro2", + "quote", + "serde", +] + [[package]] name = "itoa" version = "1.0.11" @@ -1437,6 +1458,7 @@ dependencies = [ "chrono", "http", "iso8601-duration", + "iso_currency", "quickcheck", "serde", "serde_json", diff --git a/Cargo.toml b/Cargo.toml index b6293aa..88a97af 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -63,3 +63,4 @@ argon2 = "0.5.3" dotenvy = "0.15.7" serial_test = "3.1.1" + diff --git a/openleadr-wire/Cargo.toml b/openleadr-wire/Cargo.toml index 21c7b9c..e762114 100644 --- a/openleadr-wire/Cargo.toml +++ b/openleadr-wire/Cargo.toml @@ -19,6 +19,7 @@ iso8601-duration.workspace = true thiserror.workspace = true http.workspace = true validator.workspace = true +iso_currency = { version = "0.5.0", features = ["with-serde"] } [dev-dependencies] serde_json.workspace = true diff --git a/openleadr-wire/src/event.rs b/openleadr-wire/src/event.rs index 9b911ab..6365984 100644 --- a/openleadr-wire/src/event.rs +++ b/openleadr-wire/src/event.rs @@ -5,6 +5,7 @@ use crate::{ values_map::Value, Identifier, IdentifierError, Unit, }; use chrono::{DateTime, Utc}; +use iso_currency::Currency; use serde::{Deserialize, Serialize}; use serde_with::skip_serializing_none; use std::{ @@ -208,13 +209,6 @@ impl EventPayloadDescriptor { } } -// TODO: Find a nice ISO 4217 crate -/// A currency described as listed in ISO 4217 -#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] -pub enum Currency { - Todo, -} - /// An object defining a temporal window and a list of valuesMaps. if intervalPeriod present may set /// temporal aspects of interval or override event.intervalPeriod. #[skip_serializing_none] @@ -428,4 +422,35 @@ mod tests { expected ); } + + #[test] + fn test_currency() { + // deserialize + let example = r#"{"payloadType":"SIMPLE","currency":"EUR"}"#; + + let expected = EventPayloadDescriptor { + payload_type: EventType::Simple, + units: None, + currency: Some(Currency::EUR), + }; + + assert_eq!( + serde_json::from_str::(&example).unwrap(), + expected + ); + + // round-trip + let source = EventPayloadDescriptor { + payload_type: EventType::Price, + units: Some(Unit::Volts), + currency: Some(Currency::USD), + }; + + let serialized = serde_json::to_string(&source).unwrap(); + + assert_eq!( + source, + serde_json::from_str::(&serialized).unwrap() + ); + } } From 6fef8e61fa3910217aaf62f5521d281989da1429 Mon Sep 17 00:00:00 2001 From: Joshua Thayer Date: Tue, 12 Nov 2024 11:25:07 -0800 Subject: [PATCH 2/2] Review feedback- move dependency, make clippy happy Signed-off-by: Joshua Thayer --- Cargo.toml | 1 + openleadr-wire/Cargo.toml | 2 +- openleadr-wire/src/event.rs | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 88a97af..4b28503 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -64,3 +64,4 @@ dotenvy = "0.15.7" serial_test = "3.1.1" +iso_currency = { version = "0.5.0", features = ["with-serde"] } diff --git a/openleadr-wire/Cargo.toml b/openleadr-wire/Cargo.toml index e762114..3e2a1d5 100644 --- a/openleadr-wire/Cargo.toml +++ b/openleadr-wire/Cargo.toml @@ -19,7 +19,7 @@ iso8601-duration.workspace = true thiserror.workspace = true http.workspace = true validator.workspace = true -iso_currency = { version = "0.5.0", features = ["with-serde"] } +iso_currency.workspace = true [dev-dependencies] serde_json.workspace = true diff --git a/openleadr-wire/src/event.rs b/openleadr-wire/src/event.rs index 6365984..ba65adb 100644 --- a/openleadr-wire/src/event.rs +++ b/openleadr-wire/src/event.rs @@ -435,7 +435,7 @@ mod tests { }; assert_eq!( - serde_json::from_str::(&example).unwrap(), + serde_json::from_str::(example).unwrap(), expected );