Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. mod captcha {
  2. use std::fmt;
  3.  
  4. pub enum Mode {
  5. NumFirst,
  6. TextFirst,
  7. }
  8.  
  9. pub enum Operator {
  10. Plus,
  11. Minus,
  12. Multiply,
  13. }
  14.  
  15. impl fmt::Display for Operator {
  16. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  17. match self {
  18. Operator::Plus => write!(f, "+"),
  19. Operator::Minus => write!(f, "-"),
  20. Operator::Multiply => write!(f, "*"),
  21. }
  22. }
  23. }
  24.  
  25. type Result = std::result::Result<String, Error>;
  26.  
  27. struct Error;
  28.  
  29. trait Operand {
  30. fn text(&self) -> Result;
  31. }
  32.  
  33. pub struct NumberOperand(i32);
  34.  
  35. impl fmt::Display for NumberOperand {
  36. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  37. match self.text() {
  38. Ok(text) => write!(f, "{}", text),
  39. Err(Error) => panic!("invalid number"),
  40. }
  41. }
  42. }
  43.  
  44. impl Operand for NumberOperand {
  45. fn text(&self) -> Result {
  46. Ok(self.0.to_string())
  47. }
  48. }
  49.  
  50. pub struct TextOperand(i32);
  51.  
  52. impl fmt::Display for TextOperand {
  53. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  54. match self.text() {
  55. Ok(text) => write!(f, "{}", text),
  56. Err(Error) => panic!("invalid number"),
  57. }
  58. }
  59. }
  60.  
  61. impl Operand for TextOperand {
  62. fn text(&self) -> Result {
  63. match self.0 {
  64. 1 => Ok(String::from("One")),
  65. 2 => Ok(String::from("Two")),
  66. 3 => Ok(String::from("Three")),
  67. 4 => Ok(String::from("Four")),
  68. 5 => Ok(String::from("Five")),
  69. 6 => Ok(String::from("Six")),
  70. 7 => Ok(String::from("Seven")),
  71. 8 => Ok(String::from("Eight")),
  72. 9 => Ok(String::from("Nine")),
  73. 0 => Ok(String::from("Zero")),
  74. _ => Err(Error),
  75. }
  76. }
  77. }
  78.  
  79. pub fn captcha(mode: Mode, left: i32, operator: Operator, right: i32) -> String {
  80. match mode {
  81. Mode::NumFirst => format!(
  82. "{} {} {}",
  83. NumberOperand(left),
  84. operator,
  85. TextOperand(right),
  86. ),
  87. Mode::TextFirst => format!(
  88. "{} {} {}",
  89. TextOperand(left),
  90. operator,
  91. NumberOperand(right),
  92. ),
  93. }
  94. }
  95. }
  96.  
  97. mod tests {
  98. use super::captcha::{captcha, Mode, Operator};
  99.  
  100. struct TestCase {
  101. mode: Mode,
  102. left: i32,
  103. right: i32,
  104. operator: Operator,
  105. expected: String,
  106. }
  107.  
  108. #[test]
  109. fn test_captcha() {
  110. let tcs = vec![
  111. // test operand
  112. TestCase {
  113. mode: Mode::NumFirst,
  114. left: 1,
  115. operator: Operator::Plus,
  116. right: 1,
  117. expected: String::from("1 + One"),
  118. },
  119. TestCase {
  120. mode: Mode::NumFirst,
  121. left: 2,
  122. operator: Operator::Plus,
  123. right: 1,
  124. expected: String::from("2 + One"),
  125. },
  126. // end test operand
  127. // test operator
  128. TestCase {
  129. mode: Mode::NumFirst,
  130. left: 2,
  131. operator: Operator::Plus,
  132. right: 2,
  133. expected: String::from("2 + Two"),
  134. },
  135. TestCase {
  136. mode: Mode::NumFirst,
  137. left: 2,
  138. operator: Operator::Minus,
  139. right: 2,
  140. expected: String::from("2 - Two"),
  141. },
  142. TestCase {
  143. mode: Mode::NumFirst,
  144. left: 2,
  145. operator: Operator::Multiply,
  146. right: 2,
  147. expected: String::from("2 * Two"),
  148. },
  149. // end test operator.
  150. // test mode
  151. TestCase {
  152. mode: Mode::TextFirst,
  153. left: 2,
  154. operator: Operator::Minus,
  155. right: 2,
  156. expected: String::from("Two - 2"),
  157. },
  158. // end test mode
  159. ];
  160.  
  161. for tc in tcs {
  162. assert_eq!(
  163. captcha(tc.mode, tc.left, tc.operator, tc.right),
  164. tc.expected
  165. );
  166. }
  167. }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement