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
116
117
118
119
120
121
122
123
use std::error;
use std::fmt;
use std::result::Result;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub enum Obfuscation {
None,
UseClipboard,
}
impl Obfuscation {
pub fn from_i32(id: i32) -> Result<Obfuscation, ObfuscationError> {
match id {
0 => Ok(Obfuscation::None),
1 => Ok(Obfuscation::UseClipboard),
_ => Err(ObfuscationError::InvalidValue),
}
}
pub fn to_i32(self) -> i32 {
self as i32
}
}
impl Default for Obfuscation {
fn default() -> Obfuscation {
Obfuscation::None
}
}
#[derive(Debug, PartialEq)]
pub enum ObfuscationError {
InvalidValue,
}
impl ObfuscationError {
fn msg(&self) -> &str {
match *self {
ObfuscationError::InvalidValue => "invalid value",
}
}
}
impl fmt::Display for ObfuscationError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
ObfuscationError::InvalidValue => write!(f, "Obfuscation error: {}", self.msg()),
}
}
}
impl error::Error for ObfuscationError {
fn description(&self) -> &str {
self.msg()
}
fn cause(&self) -> Option<&dyn error::Error> {
None
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_from_i32_with_valid_i32_returns_obfuscation() {
assert_eq!(Obfuscation::from_i32(0), Ok(Obfuscation::None));
assert_eq!(Obfuscation::from_i32(1), Ok(Obfuscation::UseClipboard));
}
#[test]
fn test_from_i32_with_invalid_i32_returns_error() {
assert_eq!(Obfuscation::from_i32(-1), Err(ObfuscationError::InvalidValue));
assert_eq!(Obfuscation::from_i32(2), Err(ObfuscationError::InvalidValue));
}
#[test]
fn test_to_i32_returns_correct_i32() {
assert_eq!(Obfuscation::None.to_i32(), 0);
assert_eq!(Obfuscation::UseClipboard.to_i32(), 1);
}
#[test]
fn test_default_returns_correct_value() {
assert_eq!(Obfuscation::default(), Obfuscation::None);
}
}