use std::fmt;
use serde::{Deserialize, Deserializer, Serialize};
use crate::{
domain::xrpl::{Network, PaymentRequest, PreparedPayment},
error::Error,
};
use super::Preparation;
#[derive(Clone, Eq, PartialEq)]
pub struct XrplPaymentPreparation {
intent: PaymentRequest,
unsigned: PreparedPayment,
}
impl XrplPaymentPreparation {
#[must_use]
pub fn new(intent: PaymentRequest) -> Self {
let unsigned = intent.clone().prepare();
Self { intent, unsigned }
}
}
impl Preparation for XrplPaymentPreparation {
type Network = Network;
type Intent = PaymentRequest;
type UnsignedPayload = PreparedPayment;
fn network(&self) -> &Network {
self.intent.network()
}
fn intent(&self) -> &PaymentRequest {
&self.intent
}
fn unsigned_payload(&self) -> &PreparedPayment {
&self.unsigned
}
fn validate(&self) -> Result<(), Error> {
Ok(())
}
}
impl fmt::Debug for XrplPaymentPreparation {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("XrplPaymentPreparation")
.finish_non_exhaustive()
}
}
impl Serialize for XrplPaymentPreparation {
fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
self.intent.serialize(s)
}
}
impl<'de> Deserialize<'de> for XrplPaymentPreparation {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
PaymentRequest::deserialize(d).map(Self::new)
}
}