Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support currencies in openleadr-wire (#54) #69

Merged
merged 2 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
);
}
}