use crate::models::FlagCollection;
use crate::models::Model;
use crate::models::{ledger::objects::LedgerEntryType, NoFlags};
use alloc::borrow::Cow;
use alloc::vec::Vec;
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
use super::{CommonFields, LedgerObject};
#[skip_serializing_none]
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
#[serde(rename_all = "PascalCase")]
pub struct DirectoryNode<'a> {
#[serde(flatten)]
pub common_fields: CommonFields<'a, NoFlags>,
pub exchange_rate: Option<Cow<'a, str>>,
pub indexes: Vec<Cow<'a, str>>,
pub root_index: Cow<'a, str>,
pub index_next: Option<u64>,
pub index_previous: Option<u64>,
pub owner: Option<Cow<'a, str>>,
pub taker_gets_currency: Option<Cow<'a, str>>,
pub taker_gets_issuer: Option<Cow<'a, str>>,
pub taker_pays_currency: Option<Cow<'a, str>>,
pub taker_pays_issuer: Option<Cow<'a, str>>,
}
impl<'a> Model for DirectoryNode<'a> {}
impl<'a> LedgerObject<NoFlags> for DirectoryNode<'a> {
fn get_ledger_entry_type(&self) -> LedgerEntryType {
self.common_fields.get_ledger_entry_type()
}
}
impl<'a> DirectoryNode<'a> {
pub fn new(
index: Option<Cow<'a, str>>,
ledger_index: Option<Cow<'a, str>>,
indexes: Vec<Cow<'a, str>>,
root_index: Cow<'a, str>,
exchange_rate: Option<Cow<'a, str>>,
index_next: Option<u64>,
index_previous: Option<u64>,
owner: Option<Cow<'a, str>>,
taker_gets_currency: Option<Cow<'a, str>>,
taker_gets_issuer: Option<Cow<'a, str>>,
taker_pays_currency: Option<Cow<'a, str>>,
taker_pays_issuer: Option<Cow<'a, str>>,
) -> Self {
Self {
common_fields: CommonFields {
flags: FlagCollection::default(),
ledger_entry_type: LedgerEntryType::DirectoryNode,
index,
ledger_index,
},
exchange_rate,
indexes,
root_index,
index_next,
index_previous,
owner,
taker_gets_currency,
taker_gets_issuer,
taker_pays_currency,
taker_pays_issuer,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use alloc::vec;
#[test]
fn test_serde() {
let directory_node = DirectoryNode::new(
Some(Cow::from(
"1BBEF97EDE88D40CEE2ADE6FEF121166AFE80D99EBADB01A4F069BA8FF484000",
)),
None,
vec![Cow::from(
"AD7EAE148287EF12D213A251015F86E6D4BD34B3C4A0A1ED9A17198373F908AD",
)],
Cow::from("1BBEF97EDE88D40CEE2ADE6FEF121166AFE80D99EBADB01A4F069BA8FF484000"),
Some(Cow::from("4F069BA8FF484000")),
None,
None,
None,
Some(Cow::from("0000000000000000000000000000000000000000")),
Some(Cow::from("0000000000000000000000000000000000000000")),
Some(Cow::from("0000000000000000000000004A50590000000000")),
Some(Cow::from("5BBC0F22F61D9224A110650CFE21CC0C4BE13098")),
);
let serialized = serde_json::to_string(&directory_node).unwrap();
let deserialized: DirectoryNode = serde_json::from_str(&serialized).unwrap();
assert_eq!(directory_node, deserialized);
}
}