Guest User

Untitled

a guest
Dec 10th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. use std::fmt;
  2.  
  3. #[derive(Debug, Clone)]
  4. pub enum Discounting {
  5. MinusFifteen,
  6. MinusTen,
  7. MinusFive,
  8. Zero,
  9. Five,
  10. Ten,
  11. Fifteen,
  12. Twenty,
  13. TwentyFive,
  14. Thirty,
  15. ThirtyFive,
  16. Forty,
  17. FortyFive,
  18. Fifty,
  19. FiftyFive,
  20. Sixty,
  21. SixtyFive,
  22. Seventy,
  23. SeventyFive,
  24. Eighty,
  25. EightyFive,
  26. Ninety,
  27. NinetyFive,
  28. NinetyNine,
  29. }
  30.  
  31.  
  32. impl Discounting {
  33.  
  34. fn new(x:i64)->Discounting {
  35. match x {
  36. // !! shouldn't you have your MinusXX here?
  37. 0 => Discounting::Zero,
  38. 5 => Discounting::Five,
  39. 10 => Discounting::Ten,
  40. 15 => Discounting::Fifteen,
  41. 20 => Discounting::Twenty,
  42. 25 => Discounting::TwentyFive,
  43. 30 => Discounting::Thirty,
  44. 35 => Discounting::ThirtyFive,
  45. 40 => Discounting::Forty,
  46. 45 => Discounting::FortyFive,
  47. 50 => Discounting::Fifty,
  48. 55 => Discounting::FiftyFive,
  49. 60 => Discounting::Sixty,
  50. 65 => Discounting::SixtyFive,
  51. 70 => Discounting::Seventy,
  52. 75 => Discounting::SeventyFive,
  53. 80 => Discounting::Eighty,
  54. _ => Discounting::SixtyFive,
  55. }
  56. }
  57.  
  58. fn to_i64(&self)->i64 {
  59. match *self {
  60. Discounting::MinusFifteen => -15,
  61. Discounting::MinusTen => -10,
  62. Discounting::MinusFive => -5,
  63. Discounting::Zero => 0,
  64. Discounting::Five => 5,
  65. Discounting::Ten => 10,
  66. Discounting::Fifteen => 15,
  67. Discounting::Twenty => 20,
  68. Discounting::TwentyFive => 25,
  69. Discounting::Thirty => 30,
  70. Discounting::ThirtyFive => 35,
  71. Discounting::Forty => 40,
  72. Discounting::FortyFive => 45,
  73. Discounting::Fifty => 50,
  74. Discounting::FiftyFive => 55,
  75. Discounting::Sixty => 60,
  76. Discounting::SixtyFive => 65,
  77. Discounting::Seventy => 70,
  78. Discounting::SeventyFive => 75,
  79. Discounting::Eighty => 80,
  80. _ => 65
  81. }
  82. }
  83. }
  84.  
  85.  
  86. impl<'de> serde::Deserialize<'de> for Discounting {
  87. fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
  88. where
  89. D: serde::Deserializer<'de>,
  90. {
  91. struct DiscountingVisitor;
  92. impl<'de> serde::de::Visitor<'de> for DiscountingVisitor {
  93. type Value = Discounting;
  94. fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
  95. println!("here1");
  96. formatter.write_str(
  97. r#"an integer with one of these values: -15, -10, -5, 0,
  98. 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80"#,
  99. )
  100. }
  101. fn visit_i64<E>(self, value: i64) -> Result<Self::Value, E>
  102. where
  103. E: serde::de::Error,
  104. {
  105. match value {
  106. -15 => Ok(Discounting::MinusFifteen),
  107. -10 => Ok(Discounting::MinusTen),
  108. -5 => Ok(Discounting::MinusFive),
  109. _ => Ok(Discounting::SixtyFive),
  110. }
  111. }
  112. fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
  113. where
  114. E: serde::de::Error,
  115. {
  116. Ok(Discounting::new(value as i64))
  117. }
  118. }
  119. deserializer.deserialize_i64(DiscountingVisitor)
  120. }
  121. }
  122.  
  123. impl serde::Serialize for Discounting {
  124. fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
  125. where
  126. S: serde::Serializer,
  127. {
  128. serializer.serialize_i64(self.to_i64())
  129. }
  130. }
Add Comment
Please, Sign In to add comment