use std::{fmt, io};

#[derive(Debug)]
pub enum Error {
  Hello(String),
  IO(String),
  Qsu(String)
}

impl std::error::Error for Error {}

impl Error {
  #[allow(clippy::needless_pass_by_value)]
  pub fn hello(msg: impl ToString) -> Self {
    Self::Hello(msg.to_string())
  }
}

impl fmt::Display for Error {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    match self {
      Self::Hello(s) => write!(f, "Hello error; {s}"),
      Self::IO(s) => write!(f, "I/O error; {s}"),
      Self::Qsu(s) => write!(f, "qsu error; {s}")
    }
  }
}

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

impl From<qsu::CbErr<Self>> for Error {
  fn from(err: qsu::CbErr<Self>) -> Self {
    Self::Qsu(err.to_string())
  }
}

/*
/// Convenience converter used to pass an application-defined errors from the
/// qsu inner runtime back out from the qsu runtime.
impl From<Error> for qsu::Error {
  fn from(err: Error) -> qsu::Error {
    qsu::Error::app(err)
  }
}
*/

impl From<Error> for qsu::CbErr<Error> {
  fn from(err: Error) -> Self {
    Self::App(err)
  }
}

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