Skip to content

Commit a0d0128

Browse files
authored
Support currencies in openleadr-wire (#54) (#69)
* Adds support for currencies in openleadr-wire using iso_currency crate Signed-off-by: Joshua Thayer <[email protected]> * Review feedback- move dependency, make clippy happy Signed-off-by: Joshua Thayer <[email protected]> --------- Signed-off-by: Joshua Thayer <[email protected]>
1 parent 5c3d1ca commit a0d0128

File tree

4 files changed

+57
-7
lines changed

4 files changed

+57
-7
lines changed

Cargo.lock

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,5 @@ argon2 = "0.5.3"
6363
dotenvy = "0.15.7"
6464

6565
serial_test = "3.1.1"
66+
67+
iso_currency = { version = "0.5.0", features = ["with-serde"] }

openleadr-wire/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ iso8601-duration.workspace = true
1919
thiserror.workspace = true
2020
http.workspace = true
2121
validator.workspace = true
22+
iso_currency.workspace = true
2223

2324
[dev-dependencies]
2425
serde_json.workspace = true

openleadr-wire/src/event.rs

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::{
55
values_map::Value, Identifier, IdentifierError, Unit,
66
};
77
use chrono::{DateTime, Utc};
8+
use iso_currency::Currency;
89
use serde::{Deserialize, Serialize};
910
use serde_with::skip_serializing_none;
1011
use std::{
@@ -208,13 +209,6 @@ impl EventPayloadDescriptor {
208209
}
209210
}
210211

211-
// TODO: Find a nice ISO 4217 crate
212-
/// A currency described as listed in ISO 4217
213-
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
214-
pub enum Currency {
215-
Todo,
216-
}
217-
218212
/// An object defining a temporal window and a list of valuesMaps. if intervalPeriod present may set
219213
/// temporal aspects of interval or override event.intervalPeriod.
220214
#[skip_serializing_none]
@@ -428,4 +422,35 @@ mod tests {
428422
expected
429423
);
430424
}
425+
426+
#[test]
427+
fn test_currency() {
428+
// deserialize
429+
let example = r#"{"payloadType":"SIMPLE","currency":"EUR"}"#;
430+
431+
let expected = EventPayloadDescriptor {
432+
payload_type: EventType::Simple,
433+
units: None,
434+
currency: Some(Currency::EUR),
435+
};
436+
437+
assert_eq!(
438+
serde_json::from_str::<EventPayloadDescriptor>(example).unwrap(),
439+
expected
440+
);
441+
442+
// round-trip
443+
let source = EventPayloadDescriptor {
444+
payload_type: EventType::Price,
445+
units: Some(Unit::Volts),
446+
currency: Some(Currency::USD),
447+
};
448+
449+
let serialized = serde_json::to_string(&source).unwrap();
450+
451+
assert_eq!(
452+
source,
453+
serde_json::from_str::<EventPayloadDescriptor>(&serialized).unwrap()
454+
);
455+
}
431456
}

0 commit comments

Comments
 (0)