rsprocmaps 0.3.2

A Rust library for handling memory maps in procfs
Documentation
//! Error types for `rsprocmaps`.

use std::error;
use std::fmt;
use std::io;
use std::num;

use pest::error::Error as PestError;

use crate::Rule;

/// An enumeration of possible error states for `rsprocmaps`.
#[derive(Debug)]
pub enum Error {
    /// An I/O error.
    Io(io::Error),
    // NOTE(ww): PestError<Rule> is pretty big, so we box it to keep
    // the surrounding error type small.
    /// A general parsing error.
    ParseError(Box<PestError<Rule>>),
    // NOTE(ww): ParseIntError is more general than just numbers that don't
    // fit into a particular width, but we handle all of its other parsing issues
    // at the pest/actual parsing level.
    /// An integer-width parsing error.
    WidthError(num::ParseIntError),
}

impl From<io::Error> for Error {
    fn from(err: io::Error) -> Error {
        Error::Io(err)
    }
}

impl From<PestError<Rule>> for Error {
    fn from(err: PestError<Rule>) -> Error {
        Error::ParseError(Box::new(err))
    }
}

impl From<num::ParseIntError> for Error {
    fn from(err: num::ParseIntError) -> Error {
        Error::WidthError(err)
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Error::Io(ref e) => e.fmt(f),
            Error::ParseError(ref e) => e.fmt(f),
            Error::WidthError(ref e) => e.fmt(f),
        }
    }
}

impl error::Error for Error {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match *self {
            Error::Io(ref e) => Some(e),
            Error::ParseError(ref e) => Some(e),
            Error::WidthError(ref e) => Some(e),
        }
    }
}