kpdb/types/
obfuscation.rs

1// Copyright (c) 2016-2017 Martijn Rijkeboer <mrr@sru-systems.com>
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9use std::error;
10use std::fmt;
11use std::result::Result;
12
13/// The type of obfuscation to use.
14#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
15pub enum Obfuscation {
16    None,
17    UseClipboard,
18}
19
20impl Obfuscation {
21    /// Attempts to convert an integer to an obfuscation type.
22    ///
23    /// # Examples
24    ///
25    /// ```rust
26    /// use kpdb::Obfuscation;
27    /// # use kpdb::ObfuscationError;
28    ///
29    /// # fn from_i32_example() -> Result<Obfuscation, ObfuscationError> {
30    /// let obfuscation = Obfuscation::from_i32(0)?;
31    /// # Ok(obfuscation)
32    /// # }
33    /// ```
34    pub fn from_i32(id: i32) -> Result<Obfuscation, ObfuscationError> {
35        match id {
36            0 => Ok(Obfuscation::None),
37            1 => Ok(Obfuscation::UseClipboard),
38            _ => Err(ObfuscationError::InvalidValue),
39        }
40    }
41
42    /// Gets the integer value of the obfuscation type.
43    ///
44    /// # Examples
45    ///
46    /// ```rust
47    /// use kpdb::Obfuscation;
48    ///
49    /// let obfuscation = Obfuscation::None;
50    /// let integer = obfuscation.to_i32();
51    /// ```
52    pub fn to_i32(self) -> i32 {
53        self as i32
54    }
55}
56
57impl Default for Obfuscation {
58    fn default() -> Obfuscation {
59        Obfuscation::None
60    }
61}
62
63/// Error type for obfuscation conversion errors.
64#[derive(Debug, PartialEq)]
65pub enum ObfuscationError {
66    /// Invalid value.
67    InvalidValue,
68}
69
70impl ObfuscationError {
71    fn msg(&self) -> &str {
72        match *self {
73            ObfuscationError::InvalidValue => "invalid value",
74        }
75    }
76}
77
78impl fmt::Display for ObfuscationError {
79    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
80        match *self {
81            ObfuscationError::InvalidValue => write!(f, "Obfuscation error: {}", self.msg()),
82        }
83    }
84}
85
86impl error::Error for ObfuscationError {
87    fn description(&self) -> &str {
88        self.msg()
89    }
90
91    fn cause(&self) -> Option<&dyn error::Error> {
92        None
93    }
94}
95
96#[cfg(test)]
97mod tests {
98
99    use super::*;
100
101    #[test]
102    fn test_from_i32_with_valid_i32_returns_obfuscation() {
103        assert_eq!(Obfuscation::from_i32(0), Ok(Obfuscation::None));
104        assert_eq!(Obfuscation::from_i32(1), Ok(Obfuscation::UseClipboard));
105    }
106
107    #[test]
108    fn test_from_i32_with_invalid_i32_returns_error() {
109        assert_eq!(Obfuscation::from_i32(-1), Err(ObfuscationError::InvalidValue));
110        assert_eq!(Obfuscation::from_i32(2), Err(ObfuscationError::InvalidValue));
111    }
112
113    #[test]
114    fn test_to_i32_returns_correct_i32() {
115        assert_eq!(Obfuscation::None.to_i32(), 0);
116        assert_eq!(Obfuscation::UseClipboard.to_i32(), 1);
117    }
118
119    #[test]
120    fn test_default_returns_correct_value() {
121        assert_eq!(Obfuscation::default(), Obfuscation::None);
122    }
123}