1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
use crate::{Error, Result};
use serde::de::{self, Visitor};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::fmt;
use std::result::Result as StdResult;
use std::str::FromStr;

use super::Hex;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TransactionStatus {
    Success,
    Failure,
}

impl TransactionStatus {
    pub fn from_u8(val: u8) -> Result<Self> {
        match val {
            1 => Ok(Self::Success),
            0 => Ok(Self::Failure),
            _ => Err(Error::UnknownTransactionStatus(val.to_string())),
        }
    }

    pub fn to_u8(&self) -> u8 {
        match self {
            Self::Success => 1,
            Self::Failure => 0,
        }
    }
}

impl FromStr for TransactionStatus {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self> {
        match s {
            "0x1" => Ok(Self::Success),
            "0x0" => Ok(Self::Failure),
            _ => Err(Error::UnknownTransactionStatus(s.to_owned())),
        }
    }
}

impl TransactionStatus {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Success => "0x1",
            Self::Failure => "0x0",
        }
    }
}

struct TransactionStatusVisitor;

impl<'de> Visitor<'de> for TransactionStatusVisitor {
    type Value = TransactionStatus;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str("hex string for transaction status")
    }

    fn visit_str<E>(self, value: &str) -> StdResult<Self::Value, E>
    where
        E: de::Error,
    {
        TransactionStatus::from_str(value).map_err(|e| E::custom(e.to_string()))
    }
}

impl<'de> Deserialize<'de> for TransactionStatus {
    fn deserialize<D>(deserializer: D) -> StdResult<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_str(TransactionStatusVisitor)
    }
}

impl Serialize for TransactionStatus {
    fn serialize<S>(&self, serializer: S) -> StdResult<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(self.as_str())
    }
}

impl Hex for TransactionStatus {
    fn encode_hex(&self) -> String {
        self.as_str().to_owned()
    }

    fn decode_hex(hex: &str) -> Result<Self> {
        Self::from_str(hex)
    }
}

#[cfg(test)]
mod tests {
    use super::TransactionStatus;
    use serde_test::{assert_de_tokens, assert_tokens, Token};

    #[test]
    fn test_serde() {
        assert_tokens(&TransactionStatus::Success, &[Token::Str("0x1")]);
        assert_tokens(&TransactionStatus::Failure, &[Token::Str("0x0")]);
    }

    #[test]
    #[should_panic]
    fn test_de_unknown() {
        assert_de_tokens(&TransactionStatus::Success, &[Token::Str("0x3")]);
    }
}