use alloc::borrow::Cow;
use alloc::vec::Vec;
use derive_new::new;
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
use crate::models::{
currency::Currency,
default_false,
requests::{subscribe::StreamParameter, RequestMethod},
Model,
};
use super::{CommonFields, Request};
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone, new)]
#[serde(rename_all(serialize = "PascalCase", deserialize = "snake_case"))]
pub struct UnsubscribeBook<'a> {
pub taker_gets: Currency<'a>,
pub taker_pays: Currency<'a>,
#[serde(default = "default_false")]
pub both: Option<bool>,
}
#[skip_serializing_none]
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
pub struct Unsubscribe<'a> {
#[serde(flatten)]
pub common_fields: CommonFields<'a>,
pub accounts: Option<Vec<Cow<'a, str>>>,
pub accounts_proposed: Option<Vec<Cow<'a, str>>>,
pub books: Option<Vec<UnsubscribeBook<'a>>>,
#[serde(skip_serializing)]
pub broken: Option<Cow<'a, str>>,
pub streams: Option<Vec<StreamParameter>>,
}
impl<'a> Model for Unsubscribe<'a> {}
impl<'a> Request<'a> for Unsubscribe<'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> Unsubscribe<'a> {
pub fn new(
id: Option<Cow<'a, str>>,
accounts: Option<Vec<Cow<'a, str>>>,
accounts_proposed: Option<Vec<Cow<'a, str>>>,
books: Option<Vec<UnsubscribeBook<'a>>>,
broken: Option<Cow<'a, str>>,
streams: Option<Vec<StreamParameter>>,
) -> Self {
Self {
common_fields: CommonFields {
command: RequestMethod::Unsubscribe,
id,
},
books,
streams,
accounts,
accounts_proposed,
broken,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::models::currency::{IssuedCurrency, XRP};
use alloc::vec;
#[test]
fn test_serde_round_trip_no_books() {
let req = Unsubscribe::new(
Some("uns-1".into()),
Some(vec!["rAcc1111111111111111111111111111".into()]),
None,
None,
None,
Some(vec![StreamParameter::Ledger]),
);
let serialized = serde_json::to_string(&req).unwrap();
let deserialized: Unsubscribe = serde_json::from_str(&serialized).unwrap();
assert_eq!(req, deserialized);
assert!(serialized.contains("\"command\":\"unsubscribe\""));
}
#[test]
fn test_unsubscribe_book_serializes() {
let book = UnsubscribeBook::new(
Currency::XRP(XRP::new()),
Currency::IssuedCurrency(IssuedCurrency::new(
"USD".into(),
"rIssuer11111111111111111111111111".into(),
)),
Some(true),
);
let serialized = serde_json::to_string(&book).unwrap();
assert!(serialized.contains("\"TakerGets\""));
assert!(serialized.contains("\"TakerPays\""));
}
}