use alloc::borrow::Cow;
use alloc::vec::Vec;
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
use crate::models::{requests::RequestMethod, Model};
use super::{CommonFields, LedgerIndex, LookupByLedgerRequest, Request};
#[skip_serializing_none]
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
pub struct GatewayBalances<'a> {
#[serde(flatten)]
pub common_fields: CommonFields<'a>,
pub account: Cow<'a, str>,
pub hotwallet: Option<Vec<Cow<'a, str>>>,
#[serde(flatten)]
pub ledger_lookup: Option<LookupByLedgerRequest<'a>>,
pub strict: Option<bool>,
}
impl<'a> Model for GatewayBalances<'a> {}
impl<'a> Request<'a> for GatewayBalances<'a> {
fn get_common_fields(&self) -> &CommonFields<'a> {
&self.common_fields
}
fn get_common_fields_mut(&mut self) -> &mut CommonFields<'a> {
&mut self.common_fields
}
}
impl<'a> GatewayBalances<'a> {
pub fn new(
id: Option<Cow<'a, str>>,
account: Cow<'a, str>,
hotwallet: Option<Vec<Cow<'a, str>>>,
ledger_hash: Option<Cow<'a, str>>,
ledger_index: Option<LedgerIndex<'a>>,
strict: Option<bool>,
) -> Self {
Self {
common_fields: CommonFields {
command: RequestMethod::GatewayBalances,
id,
},
account,
strict,
ledger_lookup: Some(LookupByLedgerRequest {
ledger_hash,
ledger_index,
}),
hotwallet,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use alloc::vec;
#[test]
fn test_serde_round_trip() {
let req = GatewayBalances::new(
Some("gb-1".into()),
"rIssuer11111111111111111111111111".into(),
Some(vec!["rHot1111111111111111111111111111".into()]),
None,
Some(LedgerIndex::Str("validated".into())),
Some(true),
);
let serialized = serde_json::to_string(&req).unwrap();
let deserialized: GatewayBalances = serde_json::from_str(&serialized).unwrap();
assert_eq!(req, deserialized);
assert!(serialized.contains("\"command\":\"gateway_balances\""));
assert!(serialized.contains("\"hotwallet\""));
}
}