use crate::models::FlagCollection;
use crate::models::Model;
use crate::models::{ledger::objects::LedgerEntryType, NoFlags};
use alloc::borrow::Cow;
use alloc::string::String;
use alloc::vec::Vec;
use derive_new::new;
use serde::{ser::SerializeMap, Deserialize, Serialize};
use crate::serde_with_tag;
use serde_with::skip_serializing_none;
use super::{CommonFields, LedgerObject};
serde_with_tag! {
#[derive(Debug, PartialEq, Eq, Clone, new, Default)]
pub struct DisabledValidator {
pub first_ledger_sequence: u32,
pub public_key: String,
}
}
#[skip_serializing_none]
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
#[serde(rename_all = "PascalCase")]
pub struct NegativeUNL<'a> {
#[serde(flatten)]
pub common_fields: CommonFields<'a, NoFlags>,
pub disabled_validators: Option<Vec<DisabledValidator>>,
pub validator_to_disable: Option<Cow<'a, str>>,
pub validator_to_re_enable: Option<Cow<'a, str>>,
}
impl<'a> Model for NegativeUNL<'a> {}
impl<'a> LedgerObject<NoFlags> for NegativeUNL<'a> {
fn get_ledger_entry_type(&self) -> LedgerEntryType {
self.common_fields.get_ledger_entry_type()
}
}
impl<'a> NegativeUNL<'a> {
pub fn new(
index: Option<Cow<'a, str>>,
ledger_index: Option<Cow<'a, str>>,
disabled_validators: Option<Vec<DisabledValidator>>,
validator_to_disable: Option<Cow<'a, str>>,
validator_to_re_enable: Option<Cow<'a, str>>,
) -> Self {
Self {
common_fields: CommonFields {
flags: FlagCollection::default(),
ledger_entry_type: LedgerEntryType::NegativeUNL,
index,
ledger_index,
},
disabled_validators,
validator_to_disable,
validator_to_re_enable,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use alloc::string::ToString;
use alloc::vec;
#[test]
fn test_serde() {
let negative_unl = NegativeUNL::new(
Some(Cow::from(
"2E8A59AA9D3B5B186B0B9E0F62E6C02587CA74A4D778938E957B6357D364B244",
)),
None,
Some(vec![DisabledValidator::new(
1609728,
"ED6629D456285AE3613B285F65BBFF168D695BA3921F309949AFCD2CA7AFEC16FE".to_string(),
)]),
None,
None,
);
let serialized = serde_json::to_string(&negative_unl).unwrap();
let deserialized: NegativeUNL = serde_json::from_str(&serialized).unwrap();
assert_eq!(negative_unl, deserialized);
}
}