ethrecv 0.0.1

Receive ethernet packets at a high rate.
Documentation
use std::{fmt, io};

#[derive(Debug)]
pub enum Error<E> {
  /// Application-specific error returned by application callbacks.
  App(E),

  /// An argument validation failed.
  BadArg(String),

  /// An I/O error.
  IO(std::io::Error),

  /// Internal error.
  Internal(String),

  /// Network interface error.
  NetIface(String)
}

impl<E> Error<E> {
  pub fn bad_arg<S: ToString>(s: S) -> Self {
    Error::BadArg(s.to_string())
  }

  pub fn internal<S: ToString>(s: S) -> Self {
    Error::Internal(s.to_string())
  }
}

impl<E> std::error::Error for Error<E> where E: std::fmt::Debug {}

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

impl<E> fmt::Display for Error<E> {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    match self {
      Error::App(_) => {
        write!(f, "Application-specific error")
      }
      Error::BadArg(s) => {
        write!(f, "Bad argument; {}", s)
      }
      Error::Internal(e) => {
        write!(f, "Internal error; {}", e)
      }
      Error::IO(e) => {
        write!(f, "I/O error; {}", e)
      }
      Error::NetIface(s) => {
        write!(f, "Bad format error; {}", s)
      }
    }
  }
}

// vim: set ft=rust et sw=2 ts=2 sts=2 cinoptions=2 tw=79 :