Skip to content

Commit

Permalink
Support currencies in openleadr-wire (#54) (#69)
Browse files Browse the repository at this point in the history
* Adds support for currencies in openleadr-wire using iso_currency crate

Signed-off-by: Joshua Thayer <joshuamilesthayer@gmail.com>

* Review feedback- move dependency, make clippy happy

Signed-off-by: Joshua Thayer <joshuamilesthayer@gmail.com>

---------

Signed-off-by: Joshua Thayer <joshuamilesthayer@gmail.com>
  • Loading branch information
joshuathayer authored Nov 13, 2024
1 parent 5c3d1ca commit a0d0128
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 7 deletions.
22 changes: 22 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,5 @@ argon2 = "0.5.3"
dotenvy = "0.15.7"

serial_test = "3.1.1"

iso_currency = { version = "0.5.0", features = ["with-serde"] }
1 change: 1 addition & 0 deletions openleadr-wire/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ iso8601-duration.workspace = true
thiserror.workspace = true
http.workspace = true
validator.workspace = true
iso_currency.workspace = true

[dev-dependencies]
serde_json.workspace = true
Expand Down
39 changes: 32 additions & 7 deletions openleadr-wire/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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::<EventPayloadDescriptor>(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::<EventPayloadDescriptor>(&serialized).unwrap()
);
}
}

0 comments on commit a0d0128

Please sign in to comment.