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

fixup/payout: various fixes for payout functionality #32

Merged
merged 4 commits into from
Aug 24, 2023
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ssp"
version = "0.5.1"
version = "0.5.2"
edition = "2021"
authors = ["SSP Rust Developers"]
description = "Messages and related types for implementing the SSP/eSSP serial communication protocol"
Expand Down
31 changes: 30 additions & 1 deletion src/jsonrpc/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ use crate::{Error, Event, EventPayload, Method};

impl From<&Request> for Event {
fn from(val: &Request) -> Self {
let method = Method::from(val.method().unwrap_or(""));
let method_str = val.method().unwrap_or("");
let method = Method::from(method_str.to_lowercase().as_str());

log::trace!("Method: {method}, method string: {method_str}");

let payload = match method {
Method::Fail => {
Expand Down Expand Up @@ -127,3 +130,29 @@ impl From<Event> for Request {
(&val).into()
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::{CountryCode, PayoutDenomination, PayoutDenominationList, PayoutVec, Result};

#[test]
fn test_deserialize_dispense_request() -> Result<()> {
let req_str = r#"{"jsonrpc":"2.0","id":1,"method":"DENOMINATION_DISPENSE","params":{"denominations":[{"value":50,"number":1,"currency":"CAD"}]}}"#;

let request = serde_json::from_str::<Request>(req_str)?;

let payout_denom = PayoutDenomination::create(1, 50, CountryCode::CAD);
let payout_list =
PayoutDenominationList::create(PayoutVec::from_slice([payout_denom].as_ref())?);
let exp_event = DispenseEvent::create(payout_list);

assert_eq!(request.params::<DispenseEvent>()?, exp_event);
assert_eq!(
Event::from(&request),
Event::new(Method::Dispense, EventPayload::DispenseEvent(exp_event))
);

Ok(())
}
}
1 change: 1 addition & 0 deletions src/payout_by_denomination/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ impl PayoutByDenominationCommand {
pub fn set_number_of_payouts(&mut self, num: u8) {
if (0..=MAX_PAYOUTS).contains(&(num as usize)) {
self.buf[index::NUMBER_BLOCKS] = num;
self.set_data_len(num.saturating_mul(PAYOUT_BLOCK as u8).saturating_add(2));
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/types/payout_denomination/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ impl PayoutDenominationList {
}
}

/// Creates a new [PayoutDenominationList] from the provided [PayoutDenomination]s.
pub const fn create(denominations: PayoutVec) -> Self {
Self { denominations }
}

/// Gets an iterator over the list.
pub fn iter(&self) -> slice::Iter<PayoutDenomination> {
self.denominations.iter()
Expand Down
Loading