use alloc::borrow::Cow;
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
use strum_macros::{Display, EnumString};
use crate::models::{requests::RequestMethod, Model};
use super::{CommonFields, LedgerIndex, LookupByLedgerRequest, Marker, Request};
#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize, Display, EnumString)]
#[strum(serialize_all = "snake_case")]
#[serde(rename_all = "snake_case")]
pub enum AccountObjectType {
Check,
#[serde(rename = "did")]
#[strum(serialize = "did")]
DID,
Credential,
DepositPreauth,
Escrow,
Offer,
Oracle,
PaymentChannel,
SignerList,
State,
Ticket,
MptIssuance,
Mptoken,
Vault,
}
#[skip_serializing_none]
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
pub struct AccountObjects<'a> {
#[serde(flatten)]
pub common_fields: CommonFields<'a>,
pub account: Cow<'a, str>,
#[serde(flatten)]
pub ledger_lookup: Option<LookupByLedgerRequest<'a>>,
pub r#type: Option<AccountObjectType>,
pub deletion_blockers_only: Option<bool>,
pub limit: Option<u16>,
pub marker: Option<Marker<'a>>,
}
impl<'a> Model for AccountObjects<'a> {}
impl<'a> Request<'a> for AccountObjects<'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> AccountObjects<'a> {
pub fn new(
id: Option<Cow<'a, str>>,
account: Cow<'a, str>,
ledger_hash: Option<Cow<'a, str>>,
ledger_index: Option<LedgerIndex<'a>>,
r#type: Option<AccountObjectType>,
deletion_blockers_only: Option<bool>,
limit: Option<u16>,
marker: Option<Marker<'a>>,
) -> Self {
Self {
common_fields: CommonFields {
command: RequestMethod::AccountObjects,
id,
},
account,
ledger_lookup: Some(LookupByLedgerRequest {
ledger_hash,
ledger_index,
}),
r#type,
deletion_blockers_only,
limit,
marker,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::utils::testing::test_constants::*;
#[test]
fn test_serde_round_trip() {
let req = AccountObjects::new(
Some("ao-1".into()),
ACCOUNT_GENESIS.into(),
None,
None,
Some(AccountObjectType::Escrow),
Some(true),
Some(20),
None,
);
let serialized = serde_json::to_string(&req).unwrap();
let deserialized: AccountObjects = serde_json::from_str(&serialized).unwrap();
assert_eq!(req, deserialized);
assert!(serialized.contains("\"command\":\"account_objects\""));
assert!(serialized.contains("\"type\":\"escrow\""));
}
#[test]
fn test_serde_mpt_variants() {
let req_issuance = AccountObjects {
common_fields: CommonFields {
command: RequestMethod::AccountObjects,
id: None,
},
account: ACCOUNT_GENESIS.into(),
ledger_lookup: None,
r#type: Some(AccountObjectType::MptIssuance),
deletion_blockers_only: None,
limit: None,
marker: None,
};
let serialized_issuance = serde_json::to_string(&req_issuance).unwrap();
assert!(serialized_issuance.contains("\"type\":\"mpt_issuance\""));
let req_token = AccountObjects {
common_fields: CommonFields {
command: RequestMethod::AccountObjects,
id: None,
},
account: ACCOUNT_GENESIS.into(),
ledger_lookup: None,
r#type: Some(AccountObjectType::Mptoken),
deletion_blockers_only: None,
limit: None,
marker: None,
};
let serialized_token = serde_json::to_string(&req_token).unwrap();
assert!(serialized_token.contains("\"type\":\"mptoken\""));
}
}