sqlsrv 0.14.0

Utility functions for managing SQLite connections in a server application.
Documentation
use std::{fmt, io};

/// Errors that are returned by sqlsrv.
#[derive(Debug)]
pub enum Error {
  /// A bad parameter was passed.
  BadParam(String),

  /// I/O error.
  IO(String),

  /// A value is out of bounds.
  OutOfBound(String),

  /// Error occurred in [`r2d2`].
  R2D2(r2d2::Error),

  /// Error occurred in [`rusqlite`]
  Sqlite(rusqlite::Error)
}

impl Error {
  pub fn bad_param(s: impl Into<String>) -> Self {
    Self::BadParam(s.into())
  }

  pub fn oob(s: impl Into<String>) -> Self {
    Self::OutOfBound(s.into())
  }
}

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

impl fmt::Display for Error {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    match self {
      Self::BadParam(s) => {
        write!(f, "Bad parameter; {s}")
      }
      Self::IO(s) => {
        write!(f, "I/O; {s}")
      }
      Self::OutOfBound(s) => {
        write!(f, "Out of bound; {s}")
      }
      Self::R2D2(err) => {
        write!(f, "r2d2; {err}")
      }
      Self::Sqlite(err) => {
        write!(f, "Sqlite; {err}")
      }
    }
  }
}

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

impl From<r2d2::Error> for Error {
  fn from(err: r2d2::Error) -> Self {
    Self::R2D2(err)
  }
}

impl From<rusqlite::Error> for Error {
  fn from(err: rusqlite::Error) -> Self {
    Self::Sqlite(err)
  }
}

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