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

events: add DispenseEvent #23

Merged
merged 2 commits into from
Aug 8, 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
4 changes: 4 additions & 0 deletions src/jsonrpc/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ impl From<&Request> for Event {
val.params::<StatusEvent>()
.unwrap_or(StatusEvent::default()),
),
Method::Dispense => EventPayload::DispenseEvent(
val.params::<DispenseEvent>()
.unwrap_or(DispenseEvent::new()),
),
Method::CashboxRemoved => EventPayload::CashboxRemovedEvent(
val.params::<CashboxRemovedEvent>()
.unwrap_or(CashboxRemovedEvent::new()),
Expand Down
10 changes: 7 additions & 3 deletions src/types/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod cashbox_removed;
mod cashbox_replaced;
mod disable;
mod disabled;
mod dispense;
mod enable;
mod fraud_attempt;
mod method;
Expand All @@ -28,6 +29,7 @@ pub use cashbox_removed::*;
pub use cashbox_replaced::*;
pub use disable::*;
pub use disabled::*;
pub use dispense::*;
pub use enable::*;
pub use fraud_attempt::*;
pub use method::*;
Expand All @@ -46,15 +48,13 @@ pub use stacking::*;
pub use status::*;
pub use unsafe_jam::*;

pub const OPEN_BRACE: &str = "{";
pub const CLOSE_BRACE: &str = "}";

/// JSON-RPC payloads for request parameters and response results.
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum EventPayload {
Error(Error),
// Command event payloads
DisableEvent(DisableEvent),
DispenseEvent(DispenseEvent),
EnableEvent(EnableEvent),
RejectEvent(RejectEvent),
StackEvent(StackEvent),
Expand Down Expand Up @@ -83,6 +83,7 @@ impl EventPayload {
match self {
Self::Error(_) => Method::Fail,
Self::DisableEvent(_) => DisableEvent::method(),
Self::DispenseEvent(_) => DispenseEvent::method(),
Self::EnableEvent(_) => EnableEvent::method(),
Self::RejectEvent(_) => RejectEvent::method(),
Self::StackEvent(_) => StackEvent::method(),
Expand Down Expand Up @@ -112,6 +113,7 @@ impl EventPayload {
match self {
Self::Error(evt) => json!(evt),
Self::DisableEvent(evt) => json!(evt),
Self::DispenseEvent(evt) => json!(evt),
Self::EnableEvent(evt) => json!(evt),
Self::RejectEvent(evt) => json!(evt),
Self::StackEvent(evt) => json!(evt),
Expand Down Expand Up @@ -140,6 +142,7 @@ impl fmt::Display for EventPayload {
match self {
Self::Error(err) => write!(f, r#"{{"error": "{err}"}}"#),
Self::DisableEvent(evt) => write!(f, "{evt}"),
Self::DispenseEvent(evt) => write!(f, "{evt}"),
Self::EnableEvent(evt) => write!(f, "{evt}"),
Self::RejectEvent(evt) => write!(f, "{evt}"),
Self::StackEvent(evt) => write!(f, "{evt}"),
Expand Down Expand Up @@ -270,6 +273,7 @@ impl From<Method> for Event {
Method::Disable | Method::Stop | Method::Shutdown => {
EventPayload::DisableEvent(DisableEvent::new())
}
Method::Dispense => EventPayload::DispenseEvent(DispenseEvent::default()),
Method::Enable | Method::Accept => EventPayload::EnableEvent(EnableEvent::default()),
Method::Reject => EventPayload::RejectEvent(RejectEvent::new()),
Method::Stack => EventPayload::StackEvent(StackEvent::default()),
Expand Down
25 changes: 19 additions & 6 deletions src/types/events/cashbox_removed.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{impl_default, std::fmt, Error, ResponseStatus, Result};

use super::{Method, CLOSE_BRACE, OPEN_BRACE};
use super::Method;

/// Represents a [CashboxRemoved](crate::ResponseStatus::CashboxRemoved) event.
#[derive(Clone, Copy, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
Expand All @@ -17,6 +17,11 @@ impl CashboxRemovedEvent {
Method::CashboxRemoved
}

/// Converts the [CashboxRemovedEvent] to a string.
pub const fn to_str(&self) -> &'static str {
Self::method().to_str()
}

/// Gets the length of the event in a [PollResponse](crate::PollResponse).
pub const fn len() -> usize {
1
Expand Down Expand Up @@ -54,13 +59,21 @@ impl<const N: usize> TryFrom<&[u8; N]> for CashboxRemovedEvent {
}
}

impl From<&CashboxRemovedEvent> for &'static str {
fn from(val: &CashboxRemovedEvent) -> Self {
val.to_str()
}
}

impl From<CashboxRemovedEvent> for &'static str {
fn from(val: CashboxRemovedEvent) -> Self {
(&val).into()
}
}

impl fmt::Display for CashboxRemovedEvent {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{OPEN_BRACE}\"{}\"{CLOSE_BRACE}",
Self::method().to_str()
)
write!(f, r#"{{"{}"}}"#, self.to_str())
}
}

Expand Down
25 changes: 19 additions & 6 deletions src/types/events/cashbox_replaced.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{impl_default, std::fmt, Error, ResponseStatus, Result};

use super::{Method, CLOSE_BRACE, OPEN_BRACE};
use super::Method;

/// Represents a [CashboxReplaced](crate::ResponseStatus::CashboxReplaced) event.
#[derive(Clone, Copy, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
Expand All @@ -17,6 +17,11 @@ impl CashboxReplacedEvent {
Method::CashboxReplaced
}

/// Converts the [CashboxReplacedEvent] to a string.
pub const fn to_str(&self) -> &'static str {
Self::method().to_str()
}

/// Gets the length of the event in a [PollResponse](crate::PollResponse).
pub const fn len() -> usize {
1
Expand Down Expand Up @@ -57,13 +62,21 @@ impl<const N: usize> TryFrom<&[u8; N]> for CashboxReplacedEvent {
}
}

impl From<&CashboxReplacedEvent> for &'static str {
fn from(val: &CashboxReplacedEvent) -> Self {
val.to_str()
}
}

impl From<CashboxReplacedEvent> for &'static str {
fn from(val: CashboxReplacedEvent) -> Self {
(&val).into()
}
}

impl fmt::Display for CashboxReplacedEvent {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{OPEN_BRACE}\"{}\"{CLOSE_BRACE}",
Self::method().to_str()
)
write!(f, r#"{{"{}"}}"#, self.to_str())
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/types/events/disable.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{impl_default, std::fmt};

use super::{Method, CLOSE_BRACE, OPEN_BRACE};
use super::Method;

/// Represents a [Disable](crate::ResponseDisable::Disable) event.
#[derive(Clone, Copy, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
Expand Down Expand Up @@ -42,7 +42,7 @@ impl From<DisableEvent> for &'static str {

impl fmt::Display for DisableEvent {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{OPEN_BRACE}\"{}\"{CLOSE_BRACE}", self.to_str())
write!(f, r#"{{"{}"}}"#, self.to_str())
}
}

Expand Down
13 changes: 7 additions & 6 deletions src/types/events/disabled.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{impl_default, std::fmt, Error, ResponseStatus, Result};

use super::{Method, CLOSE_BRACE, OPEN_BRACE};
use super::Method;

/// Represents a [Disabled](crate::ResponseStatus::Disabled) event.
#[derive(Clone, Copy, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
Expand All @@ -17,6 +17,11 @@ impl DisabledEvent {
Method::Disabled
}

/// Converts the [DisabledEvent] to a string.
pub const fn to_str(&self) -> &'static str {
Self::method().to_str()
}

/// Gets the length of the event in a [PollResponse](crate::PollResponse).
pub const fn len() -> usize {
1
Expand Down Expand Up @@ -56,11 +61,7 @@ impl<const N: usize> TryFrom<&[u8; N]> for DisabledEvent {

impl fmt::Display for DisabledEvent {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{OPEN_BRACE}\"{}\"{CLOSE_BRACE}",
Self::method().to_str()
)
write!(f, r#"{{"{}"}}"#, self.to_str())
}
}

Expand Down
69 changes: 69 additions & 0 deletions src/types/events/dispense.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use crate::{impl_default, std::fmt, PayoutDenominationList};

use super::Method;

/// Represents a [Dispense](crate::PayoutDenominationCommand) event.
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct DispenseEvent(PayoutDenominationList);

impl DispenseEvent {
/// Creates a new [DispenseEvent].
pub const fn new() -> Self {
Self(PayoutDenominationList::new())
}

/// Creates a new [DispenseEvent] with the provided parameter.
pub const fn create(list: PayoutDenominationList) -> Self {
Self(list)
}

/// Gets the [Method] for the [DispenseEvent].
pub const fn method() -> Method {
Method::Dispense
}

/// Converts the [DispenseEvent] to a string.
pub const fn to_str(&self) -> &'static str {
Self::method().to_str()
}

/// Gets the length of the event in a [PollResponse](crate::PollResponse).
pub const fn len() -> usize {
1
}

/// Gets a reference to the inner representation of the [DispenseEvent].
pub const fn as_inner(&self) -> &PayoutDenominationList {
&self.0
}

/// Gets a mutable reference to the inner representation of the [DispenseEvent].
pub fn as_inner_mut(&mut self) -> &mut PayoutDenominationList {
&mut self.0
}

/// Consumes the [DispenseEvent], returning its inner repreesentation.
pub fn to_inner(self) -> PayoutDenominationList {
self.0
}
}

impl From<&DispenseEvent> for &'static str {
fn from(val: &DispenseEvent) -> Self {
val.to_str()
}
}

impl From<DispenseEvent> for &'static str {
fn from(val: DispenseEvent) -> Self {
(&val).into()
}
}

impl fmt::Display for DispenseEvent {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, r#"{{"{}"}}"#, self.to_str())
}
}

impl_default!(DispenseEvent);
6 changes: 2 additions & 4 deletions src/types/events/enable.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{std::fmt, ProtocolVersion};

use super::{Method, CLOSE_BRACE, OPEN_BRACE};
use super::Method;

/// Represents a [Enable](crate::ResponseEnable::Enable) event.
#[derive(Clone, Copy, Debug, Default, PartialEq, serde::Serialize, serde::Deserialize)]
Expand Down Expand Up @@ -66,11 +66,9 @@ impl From<&ProtocolVersion> for EnableEvent {

impl fmt::Display for EnableEvent {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (o, c) = (OPEN_BRACE, CLOSE_BRACE);

let method = self.to_str();
let protocol = self.protocol_version();

write!(f, "{o}\"{method}\": {o}\"protocol_version\": {protocol}{c}")
write!(f, r#"{{"{method}": {{"protocol_version": {protocol}}}"#)
}
}
25 changes: 19 additions & 6 deletions src/types/events/fraud_attempt.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{channel_value, std::fmt, ChannelValue, Error, ResponseStatus, Result};

use super::{Method, CLOSE_BRACE, OPEN_BRACE};
use super::Method;

/// Represents a [FraudAttempt](crate::ResponseStatus::FraudAttempt) event.
#[derive(Clone, Copy, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
Expand All @@ -19,6 +19,11 @@ impl FraudAttemptEvent {
Method::FraudAttempt
}

/// Converts the [FraudAttemptEvent] to a string.
pub const fn to_str(&self) -> &'static str {
Self::method().to_str()
}

/// Gets the [ChannelValue].
pub fn value(&self) -> &ChannelValue {
&self.value
Expand Down Expand Up @@ -77,13 +82,21 @@ impl From<&ChannelValue> for FraudAttemptEvent {
}
}

impl From<&FraudAttemptEvent> for &'static str {
fn from(val: &FraudAttemptEvent) -> Self {
val.to_str()
}
}

impl From<FraudAttemptEvent> for &'static str {
fn from(val: FraudAttemptEvent) -> Self {
(&val).into()
}
}

impl fmt::Display for FraudAttemptEvent {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{OPEN_BRACE}\"{}\"{CLOSE_BRACE}",
Self::method().to_str()
)
write!(f, r#"{{"{}"}}"#, self.to_str())
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/types/events/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pub enum Method {
Status,
/// Shutdown the server.
Shutdown,
/// Dispense notes from the device.
Dispense,
/// Cashbox removed from device.
CashboxRemoved = ResponseStatus::CashboxRemoved.to_u8(),
/// Cashbox replaced into device.
Expand Down Expand Up @@ -78,6 +80,7 @@ impl Method {
Self::Reject => "reject",
Self::Stack => "stack",
Self::Shutdown => "shutdown",
Self::Dispense => "dispense",
Self::CashboxRemoved => "cashbox_removed",
Self::CashboxReplaced => "cashbox_replaced",
Self::Disabled => "disabled",
Expand Down Expand Up @@ -171,6 +174,7 @@ impl FromStr for Method {
"reject" => Self::Reject,
"stack" => Self::Stack,
"shutdown" => Self::Shutdown,
"dispense" => Self::Dispense,
"cashbox_removed" => Self::CashboxRemoved,
"cashbox_replaced" => Self::CashboxReplaced,
"disabled" => Self::Disabled,
Expand Down Expand Up @@ -280,6 +284,7 @@ impl serde::Serialize for Method {
Self::Stack => serializer.serialize_unit_variant("Method", 5, "stack"),
Self::Status => serializer.serialize_unit_variant("Method", 6, "status"),
Self::Shutdown => serializer.serialize_unit_variant("Method", 7, "shutdown"),
Self::Dispense => serializer.serialize_unit_variant("Method", 7, "dispense"),
Self::CashboxRemoved => {
serializer.serialize_unit_variant("Method", 8, "cashbox_removed")
}
Expand Down
Loading
Loading