update example to be more idiomatic

This commit is contained in:
Jane Lusby 2020-09-04 15:55:13 -07:00
parent 2387f68e43
commit c31d4730b0

View File

@ -14,55 +14,88 @@ declare_clippy_lint! {
/// **Example:** /// **Example:**
/// Before: /// Before:
/// ```rust /// ```rust
/// use std::convert::TryFrom; /// use std::fmt;
/// ///
/// #[derive(Debug)] /// #[derive(Debug)]
/// enum Errors { /// enum Error {
/// Ignored /// Indivisible,
/// Remainder(u8),
/// } /// }
/// ///
/// fn divisible_by_3(inp: i32) -> Result<u32, Errors> { /// impl fmt::Display for Error {
/// let i = u32::try_from(inp).map_err(|_| Errors::Ignored)?; /// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
/// match self {
/// Error::Indivisible => write!(f, "could not divide input by three"),
/// Error::Remainder(remainder) => write!(
/// f,
/// "input is not divisible by three, remainder = {}",
/// remainder
/// ),
/// }
/// }
/// }
/// ///
/// Ok(i) /// impl std::error::Error for Error {}
///
/// fn divisible_by_3(input: &str) -> Result<(), Error> {
/// input
/// .parse::<i32>()
/// .map_err(|_| Error::Indivisible)
/// .map(|v| v % 3)
/// .and_then(|remainder| {
/// if remainder == 0 {
/// Ok(())
/// } else {
/// Err(Error::Remainder(remainder as u8))
/// }
/// })
/// } /// }
/// ``` /// ```
/// ///
/// After: /// After:
/// ```rust /// ```rust
/// use std::convert::TryFrom; /// use std::{fmt, num::ParseIntError};
/// use std::num::TryFromIntError;
/// use std::fmt;
/// use std::error::Error;
/// ///
/// #[derive(Debug)] /// #[derive(Debug)]
/// enum ParseError { /// enum Error {
/// Indivisible { /// Indivisible(ParseIntError),
/// source: TryFromIntError, /// Remainder(u8),
/// input: String,
/// }
/// } /// }
/// ///
/// impl fmt::Display for ParseError { /// impl fmt::Display for Error {
/// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { /// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
/// match &self { /// match self {
/// ParseError::Indivisible{source: _, input} => write!(f, "Error: {}", input) /// Error::Indivisible(_) => write!(f, "could not divide input by three"),
/// Error::Remainder(remainder) => write!(
/// f,
/// "input is not divisible by three, remainder = {}",
/// remainder
/// ),
/// } /// }
/// } /// }
/// } /// }
/// ///
/// impl Error for ParseError {} /// impl std::error::Error for Error {
/// /// fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
/// impl ParseError { /// match self {
/// fn new(source: TryFromIntError, input: String) -> ParseError { /// Error::Indivisible(source) => Some(source),
/// ParseError::Indivisible{source, input} /// _ => None,
/// }
/// } /// }
/// } /// }
/// ///
/// fn divisible_by_3(inp: i32) -> Result<u32, ParseError> { /// fn divisible_by_3(input: &str) -> Result<(), Error> {
/// let i = u32::try_from(inp).map_err(|e| ParseError::new(e, e.to_string()))?; /// input
/// /// .parse::<i32>()
/// Ok(i) /// .map_err(Error::Indivisible)
/// .map(|v| v % 3)
/// .and_then(|remainder| {
/// if remainder == 0 {
/// Ok(())
/// } else {
/// Err(Error::Remainder(remainder as u8))
/// }
/// })
/// } /// }
/// ``` /// ```
pub MAP_ERR_IGNORE, pub MAP_ERR_IGNORE,