use alloc::borrow::Cow;
use alloc::vec::Vec;
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
use serde_with::skip_serializing_none;
use strum_macros::{AsRefStr, Display, EnumIter};
use crate::models::transactions::{CommonFields, Memo, Signer};
use crate::models::{
amount::XRPAmount,
transactions::{Transaction, TransactionType},
Model,
};
use crate::models::{FlagCollection, NoFlags};
#[derive(
Debug, Eq, PartialEq, Clone, Serialize_repr, Deserialize_repr, Display, AsRefStr, EnumIter,
)]
#[repr(u32)]
pub enum UNLModifyDisabling {
Disable = 0,
Enable = 1,
}
#[skip_serializing_none]
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
#[serde(rename_all = "PascalCase")]
pub struct UNLModify<'a> {
#[serde(flatten)]
pub common_fields: CommonFields<'a, NoFlags>,
pub ledger_sequence: u32,
pub unlmodify_disabling: UNLModifyDisabling,
pub unlmodify_validator: Cow<'a, str>,
}
impl<'a> Model for UNLModify<'a> {}
impl<'a> Transaction<'a, NoFlags> for UNLModify<'a> {
fn get_transaction_type(&self) -> &TransactionType {
self.common_fields.get_transaction_type()
}
fn get_common_fields(&self) -> &CommonFields<'_, NoFlags> {
self.common_fields.get_common_fields()
}
fn get_mut_common_fields(&mut self) -> &mut CommonFields<'a, NoFlags> {
self.common_fields.get_mut_common_fields()
}
}
impl<'a> UNLModify<'a> {
pub fn new(
account: Cow<'a, str>,
account_txn_id: Option<Cow<'a, str>>,
fee: Option<XRPAmount<'a>>,
last_ledger_sequence: Option<u32>,
memos: Option<Vec<Memo>>,
sequence: Option<u32>,
signers: Option<Vec<Signer>>,
source_tag: Option<u32>,
ticket_sequence: Option<u32>,
ledger_sequence: u32,
unlmodify_disabling: UNLModifyDisabling,
unlmodify_validator: Cow<'a, str>,
) -> Self {
Self {
common_fields: CommonFields::new(
account,
TransactionType::UNLModify,
account_txn_id,
fee,
Some(FlagCollection::default()),
last_ledger_sequence,
memos,
None,
sequence,
signers,
None,
source_tag,
ticket_sequence,
None,
),
ledger_sequence,
unlmodify_disabling,
unlmodify_validator,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_serde_round_trip() {
let txn = UNLModify::new(
"rrrrrrrrrrrrrrrrrrrrrhoLvTp".into(),
None,
None,
None,
None,
None,
None,
None,
None,
56865245,
UNLModifyDisabling::Enable,
"ED1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF".into(),
);
let serialized = serde_json::to_string(&txn).unwrap();
let deserialized: UNLModify = serde_json::from_str(&serialized).unwrap();
assert_eq!(txn, deserialized);
assert!(serialized.contains("\"TransactionType\":\"UNLModify\""));
assert!(serialized.contains("\"UnlmodifyDisabling\":1"));
assert_eq!(txn.get_transaction_type(), &TransactionType::UNLModify);
}
#[test]
fn test_disabling_disable() {
let txn = UNLModify::new(
"rrrrrrrrrrrrrrrrrrrrrhoLvTp".into(),
None,
None,
None,
None,
None,
None,
None,
None,
1,
UNLModifyDisabling::Disable,
"ED00".into(),
);
let serialized = serde_json::to_string(&txn).unwrap();
assert!(serialized.contains("\"UnlmodifyDisabling\":0"));
}
}