use crate::models::transactions::mptoken_issuance_set::validate_mptoken_issuance_id;
use crate::models::{Model, XRPLModelResult};
use alloc::borrow::Cow;
use serde::{Deserialize, Serialize};
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, Default)]
pub struct MPTCurrency<'a> {
pub mpt_issuance_id: Cow<'a, str>,
}
impl<'a> Model for MPTCurrency<'a> {
fn get_errors(&self) -> XRPLModelResult<()> {
validate_mptoken_issuance_id(self.mpt_issuance_id.as_ref())?;
Ok(())
}
}
impl<'a> MPTCurrency<'a> {
pub fn new(mpt_issuance_id: Cow<'a, str>) -> Self {
Self { mpt_issuance_id }
}
}
#[cfg(test)]
mod tests {
use crate::models::Model;
use super::*;
const VALID_ID: &str = "00000001A407AF5856CEFBF81F3D4A0000000000A407AF58";
#[test]
fn test_mpt_currency_serde_roundtrip() {
let cur = MPTCurrency::new(VALID_ID.into());
let json = serde_json::to_string(&cur).unwrap();
let decoded: MPTCurrency = serde_json::from_str(&json).unwrap();
assert_eq!(cur, decoded);
}
#[test]
fn test_mpt_currency_get_errors_valid() {
assert!(MPTCurrency::new(VALID_ID.into()).get_errors().is_ok());
}
#[test]
fn test_mpt_currency_get_errors_bad_id_too_short() {
assert!(MPTCurrency::new("XYZ".into()).get_errors().is_err());
}
#[test]
fn test_mpt_currency_get_errors_bad_id_non_hex() {
let bad_id = "Z".repeat(48);
assert!(MPTCurrency::new(bad_id.as_str().into())
.get_errors()
.is_err());
}
}