xrpl/models/currency/
mpt_currency.rs1use crate::models::transactions::mptoken_issuance_set::validate_mptoken_issuance_id;
2use crate::models::{Model, XRPLModelResult};
3use alloc::borrow::Cow;
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, Default)]
17pub struct MPTCurrency<'a> {
18 pub mpt_issuance_id: Cow<'a, str>,
21}
22
23impl<'a> Model for MPTCurrency<'a> {
24 fn get_errors(&self) -> XRPLModelResult<()> {
25 validate_mptoken_issuance_id(self.mpt_issuance_id.as_ref())?;
26 Ok(())
27 }
28}
29
30impl<'a> MPTCurrency<'a> {
31 pub fn new(mpt_issuance_id: Cow<'a, str>) -> Self {
32 Self { mpt_issuance_id }
33 }
34}
35
36#[cfg(test)]
37mod tests {
38 use crate::models::Model;
39
40 use super::*;
41
42 const VALID_ID: &str = "00000001A407AF5856CEFBF81F3D4A0000000000A407AF58";
43
44 #[test]
45 fn test_mpt_currency_serde_roundtrip() {
46 let cur = MPTCurrency::new(VALID_ID.into());
47 let json = serde_json::to_string(&cur).unwrap();
48 let decoded: MPTCurrency = serde_json::from_str(&json).unwrap();
49 assert_eq!(cur, decoded);
50 }
51
52 #[test]
53 fn test_mpt_currency_get_errors_valid() {
54 assert!(MPTCurrency::new(VALID_ID.into()).get_errors().is_ok());
55 }
56
57 #[test]
58 fn test_mpt_currency_get_errors_bad_id_too_short() {
59 assert!(MPTCurrency::new("XYZ".into()).get_errors().is_err());
60 }
61
62 #[test]
63 fn test_mpt_currency_get_errors_bad_id_non_hex() {
64 let bad_id = "Z".repeat(48);
65 assert!(MPTCurrency::new(bad_id.as_str().into())
66 .get_errors()
67 .is_err());
68 }
69}