shuttle_core/
memo.rs

1use error::{Error, Result};
2
3const MAX_MEMO_TEXT_LEN: usize = 28;
4
5/// Memo attached to transactions.
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub enum Memo {
8    /// No memo
9    None,
10    /// Text Memo
11    Text(String),
12    /// Id Memo
13    Id(u64),
14    /// Hash Memo
15    Hash([u8; 32]),
16    /// Return Memo
17    Return([u8; 32]),
18}
19
20impl Memo {
21    /// Create new empty memo.
22    pub fn none() -> Memo {
23        Memo::None
24    }
25
26    /// Create new id memo.
27    pub fn id(id: u64) -> Memo {
28        Memo::Id(id)
29    }
30
31    /// Create new text memo. `text` must be shorter than 28 bytes.
32    pub fn text<S: Into<String>>(text: S) -> Result<Memo> {
33        let text = text.into();
34        if text.len() > MAX_MEMO_TEXT_LEN {
35            Err(Error::InvalidMemoText)
36        } else {
37            Ok(Memo::Text(text))
38        }
39    }
40
41    /// Create new hash memo.
42    pub fn hash(h: [u8; 32]) -> Memo {
43        Memo::Hash(h)
44    }
45
46    /// Create new return memo.
47    pub fn return_(r: [u8; 32]) -> Memo {
48        Memo::Return(r)
49    }
50
51    /// Return `true` if memo is `None`.
52    pub fn is_none(&self) -> bool {
53        match *self {
54            Memo::None => true,
55            _ => false,
56        }
57    }
58
59    /// Return `true` if memo is `Id`.
60    pub fn is_id(&self) -> bool {
61        match *self {
62            Memo::Id(_) => true,
63            _ => false,
64        }
65    }
66
67    /// Return `true` if memo is `Text`.
68    pub fn is_text(&self) -> bool {
69        match *self {
70            Memo::Text(_) => true,
71            _ => false,
72        }
73    }
74
75    /// Return `true` if memo is `Hash`.
76    pub fn is_hash(&self) -> bool {
77        match *self {
78            Memo::Hash(_) => true,
79            _ => false,
80        }
81    }
82
83    /// Return `true` if memo is `Return`.
84    pub fn is_return(&self) -> bool {
85        match *self {
86            Memo::Return(_) => true,
87            _ => false,
88        }
89    }
90}