@@ -14,55 +14,88 @@ declare_clippy_lint! {
14
14
/// **Example:**
15
15
/// Before:
16
16
/// ```rust
17
- /// use std::convert::TryFrom ;
17
+ /// use std::fmt ;
18
18
///
19
19
/// #[derive(Debug)]
20
- /// enum Errors {
21
- /// Ignored
20
+ /// enum Error {
21
+ /// Indivisible,
22
+ /// Remainder(u8),
23
+ /// }
24
+ ///
25
+ /// impl fmt::Display for Error {
26
+ /// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27
+ /// match self {
28
+ /// Error::Indivisible => write!(f, "could not divide input by three"),
29
+ /// Error::Remainder(remainder) => write!(
30
+ /// f,
31
+ /// "input is not divisible by three, remainder = {}",
32
+ /// remainder
33
+ /// ),
34
+ /// }
35
+ /// }
22
36
/// }
23
37
///
24
- /// fn divisible_by_3(inp: i32) -> Result<u32, Errors> {
25
- /// let i = u32::try_from(inp).map_err(|_| Errors::Ignored)?;
38
+ /// impl std::error::Error for Error {}
26
39
///
27
- /// Ok(i)
40
+ /// fn divisible_by_3(input: &str) -> Result<(), Error> {
41
+ /// input
42
+ /// .parse::<i32>()
43
+ /// .map_err(|_| Error::Indivisible)
44
+ /// .map(|v| v % 3)
45
+ /// .and_then(|remainder| {
46
+ /// if remainder == 0 {
47
+ /// Ok(())
48
+ /// } else {
49
+ /// Err(Error::Remainder(remainder as u8))
50
+ /// }
51
+ /// })
28
52
/// }
29
53
/// ```
30
54
///
31
55
/// After:
32
56
/// ```rust
33
- /// use std::convert::TryFrom;
34
- /// use std::num::TryFromIntError;
35
- /// use std::fmt;
36
- /// use std::error::Error;
57
+ /// use std::{fmt, num::ParseIntError};
37
58
///
38
59
/// #[derive(Debug)]
39
- /// enum ParseError {
40
- /// Indivisible {
41
- /// source: TryFromIntError,
42
- /// input: String,
43
- /// }
60
+ /// enum Error {
61
+ /// Indivisible(ParseIntError),
62
+ /// Remainder(u8),
44
63
/// }
45
64
///
46
- /// impl fmt::Display for ParseError {
65
+ /// impl fmt::Display for Error {
47
66
/// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48
- /// match &self {
49
- /// ParseError::Indivisible{source: _, input} => write!(f, "Error: {}", input)
67
+ /// match self {
68
+ /// Error::Indivisible(_) => write!(f, "could not divide input by three"),
69
+ /// Error::Remainder(remainder) => write!(
70
+ /// f,
71
+ /// "input is not divisible by three, remainder = {}",
72
+ /// remainder
73
+ /// ),
50
74
/// }
51
75
/// }
52
76
/// }
53
77
///
54
- /// impl Error for ParseError {}
55
- ///
56
- /// impl ParseError {
57
- /// fn new(source: TryFromIntError, input: String) -> ParseError {
58
- /// ParseError::Indivisible{source, input}
78
+ /// impl std::error::Error for Error {
79
+ /// fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
80
+ /// match self {
81
+ /// Error::Indivisible(source) => Some(source),
82
+ /// _ => None,
83
+ /// }
59
84
/// }
60
85
/// }
61
86
///
62
- /// fn divisible_by_3(inp: i32) -> Result<u32, ParseError> {
63
- /// let i = u32::try_from(inp).map_err(|e| ParseError::new(e, e.to_string()))?;
64
- ///
65
- /// Ok(i)
87
+ /// fn divisible_by_3(input: &str) -> Result<(), Error> {
88
+ /// input
89
+ /// .parse::<i32>()
90
+ /// .map_err(Error::Indivisible)
91
+ /// .map(|v| v % 3)
92
+ /// .and_then(|remainder| {
93
+ /// if remainder == 0 {
94
+ /// Ok(())
95
+ /// } else {
96
+ /// Err(Error::Remainder(remainder as u8))
97
+ /// }
98
+ /// })
66
99
/// }
67
100
/// ```
68
101
pub MAP_ERR_IGNORE ,
0 commit comments