Advertisement
Guest User

Untitled

a guest
May 24th, 2015
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. use std::convert::From;
  2. use std::error::Error;
  3. use std::fmt::{Display, Formatter, Result};
  4.  
  5. #[derive(Debug)]
  6. pub enum ErrorKind {
  7. InvalidOption(InvalidOptionError),
  8. MissingOptArg(MissingOptArgError),
  9. MissingArgument(MissingArgumentError),
  10. }
  11.  
  12. impl From<InvalidOptionError> for ErrorKind {
  13. fn from(err: InvalidOptionError) -> ErrorKind {
  14. ErrorKind::InvalidOption(err)
  15. }
  16. }
  17.  
  18. impl From<MissingOptArgError> for ErrorKind {
  19. fn from(err: MissingOptArgError) -> ErrorKind {
  20. ErrorKind::MissingOptArg(err)
  21. }
  22. }
  23.  
  24. impl From<MissingArgumentError> for ErrorKind {
  25. fn from(err: MissingArgumentError) -> ErrorKind {
  26. ErrorKind::MissingArgument(err)
  27. }
  28. }
  29.  
  30. impl Error for ErrorKind {
  31. fn description(&self) -> &str {
  32. match *self {
  33. ErrorKind::InvalidOption(ref err) => err.description(),
  34. ErrorKind::MissingOptArg(ref err) => err.description(),
  35. ErrorKind::MissingArgument(ref err) => err.description(),
  36. }
  37. }
  38.  
  39. fn cause(&self) -> Option<&Error> {
  40. Some(match *self {
  41. ErrorKind::InvalidOption(ref err) => err as &Error,
  42. ErrorKind::MissingOptArg(ref err) => err as &Error,
  43. ErrorKind::MissingArgument(ref err) => err as &Error,
  44. })
  45. }
  46. }
  47.  
  48. impl Display for ErrorKind {
  49. fn fmt(&self, f: &mut Formatter) -> Result {
  50. match *self {
  51. ErrorKind::InvalidOption(ref err) => Display::fmt(err, f),
  52. ErrorKind::MissingOptArg(ref err) => Display::fmt(err, f),
  53. ErrorKind::MissingArgument(ref err) => Display::fmt(err, f),
  54. }
  55. }
  56. }
  57.  
  58. #[derive(Debug)]
  59. pub struct InvalidOptionError {
  60. desc: String,
  61. }
  62.  
  63. impl InvalidOptionError {
  64. pub fn new(opt: &str) -> InvalidOptionError {
  65. InvalidOptionError {
  66. desc: format!("An invalid option was passed to the program: {}", opt)
  67. }
  68. }
  69. }
  70.  
  71. impl Error for InvalidOptionError {
  72. fn description(&self) -> &str {
  73. &self.desc
  74. }
  75. }
  76.  
  77. impl Display for InvalidOptionError {
  78. fn fmt(&self, f: &mut Formatter) -> Result {
  79. write!(f, "{}", &self.desc)
  80. }
  81. }
  82.  
  83. #[derive(Debug)]
  84. pub struct MissingOptArgError {
  85. desc: String,
  86. }
  87.  
  88. impl MissingOptArgError {
  89. pub fn new(opt: &str) -> MissingOptArgError {
  90. MissingOptArgError {
  91. desc: format!("A required argument is missing after the option '{}'", opt)
  92. }
  93. }
  94. }
  95.  
  96. impl Error for MissingOptArgError {
  97. fn description(&self) -> &str {
  98. &self.desc
  99. }
  100. }
  101.  
  102.  
  103. impl Display for MissingOptArgError {
  104. fn fmt(&self, f: &mut Formatter) -> Result {
  105. write!(f, "{}", &self.desc)
  106. }
  107. }
  108.  
  109. #[derive(Debug)]
  110. pub struct MissingArgumentError {
  111. desc: String,
  112. }
  113.  
  114. impl MissingArgumentError {
  115. pub fn new() -> MissingArgumentError {
  116. MissingArgumentError {
  117. desc: format!("A required program argument is missing")
  118. }
  119. }
  120. }
  121.  
  122. impl Error for MissingArgumentError {
  123. fn description(&self) -> &str {
  124. &self.desc
  125. }
  126. }
  127.  
  128. impl Display for MissingArgumentError {
  129. fn fmt(&self, f: &mut Formatter) -> Result {
  130. write!(f, "{}", &self.desc)
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement