1use error::{Error, Result};
2
3const MAX_MEMO_TEXT_LEN: usize = 28;
4
5#[derive(Debug, Clone, PartialEq, Eq)]
7pub enum Memo {
8 None,
10 Text(String),
12 Id(u64),
14 Hash([u8; 32]),
16 Return([u8; 32]),
18}
19
20impl Memo {
21 pub fn none() -> Memo {
23 Memo::None
24 }
25
26 pub fn id(id: u64) -> Memo {
28 Memo::Id(id)
29 }
30
31 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 pub fn hash(h: [u8; 32]) -> Memo {
43 Memo::Hash(h)
44 }
45
46 pub fn return_(r: [u8; 32]) -> Memo {
48 Memo::Return(r)
49 }
50
51 pub fn is_none(&self) -> bool {
53 match *self {
54 Memo::None => true,
55 _ => false,
56 }
57 }
58
59 pub fn is_id(&self) -> bool {
61 match *self {
62 Memo::Id(_) => true,
63 _ => false,
64 }
65 }
66
67 pub fn is_text(&self) -> bool {
69 match *self {
70 Memo::Text(_) => true,
71 _ => false,
72 }
73 }
74
75 pub fn is_hash(&self) -> bool {
77 match *self {
78 Memo::Hash(_) => true,
79 _ => false,
80 }
81 }
82
83 pub fn is_return(&self) -> bool {
85 match *self {
86 Memo::Return(_) => true,
87 _ => false,
88 }
89 }
90}