recstrm 0.0.1

Special purpose flow-controlled channel used to stream records.
Documentation
use std::fmt;

/// Module-specific error codes.
#[derive(Debug)]
pub enum Error<E> {
  /// Application-specific error.
  ///
  /// The `E` type is typically declared as the third generic parameter to
  /// [`channel`](crate::channel()).
  App(E),

  /// Attempted to add a node to queue, but the queue was full.
  //QueueFull(T),
  QueueFull,

  /// The receiver end-point disappeared.
  ReceiverDisappeared,

  /// Fewer records than expected where sent before the sender was dropped.
  RecordsUnderflow
}

impl<E> Error<E> {
  /// Attempt to convert `Error::App(E)` into `E`.
  pub fn into_apperr(self) -> Option<E> {
    match self {
      Error::App(e) => Some(e),
      _ => None
    }
  }

  /// Unwrap application-specific error from an [`Error`].
  ///
  /// # Panic
  /// Panics if `Error` variant is not `Error::App()`.
  pub fn unwrap_apperr(self) -> E {
    match self {
      Error::App(e) => e,
      _ => panic!("Not an Error::App")
    }
  }
}

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

impl<E: fmt::Debug> fmt::Display for Error<E> {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    match self {
      Error::App(err) => write!(f, "Application error; {:?}", err),
      Error::QueueFull => write!(f, "Queue full"),
      Error::ReceiverDisappeared => {
        write!(f, "The receiver disappeared")
      }
      Error::RecordsUnderflow => {
        write!(f, "Sender disappeared before sending all expected records")
      }
    }
  }
}

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