use core::error;
use core::fmt;
#[cfg(feature = "alloc")]
use alloc::format;
#[cfg(feature = "alloc")]
use alloc::string::String;
use crate::Code;
pub type Result<T, E = Error> = core::result::Result<T, E>;
#[derive(PartialEq, Eq)]
pub struct Error {
code: Code,
#[cfg(feature = "alloc")]
message: String,
}
impl Error {
#[inline]
pub fn new(
code: Code,
#[cfg_attr(not(feature = "alloc"), allow(unused_variables))] message: impl fmt::Display,
) -> Self {
Self {
code,
#[cfg(feature = "alloc")]
message: format!("{message}"),
}
}
#[inline]
pub fn custom(message: impl fmt::Display) -> Self {
Self::new(Code::ERROR, message)
}
#[inline]
pub fn code(&self) -> Code {
self.code
}
}
impl fmt::Debug for Error {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut st = f.debug_struct("Error");
st.field("code", &self.code);
#[cfg(feature = "alloc")]
st.field("message", &self.message);
#[cfg(not(feature = "alloc"))]
st.field("message", &self.code.message());
st.finish()
}
}
#[cfg(feature = "alloc")]
impl fmt::Display for Error {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.message.fmt(f)
}
}
#[cfg(not(feature = "alloc"))]
impl fmt::Display for Error {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.code.message().fmt(f)
}
}
impl error::Error for Error {}
#[derive(Debug)]
#[non_exhaustive]
pub struct DatabaseNotFound;
impl fmt::Display for DatabaseNotFound {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "database not found")
}
}
impl core::error::Error for DatabaseNotFound {}
pub struct NotThreadSafe {
kind: NotThreadSafeKind,
}
impl NotThreadSafe {
#[inline]
pub(super) const fn connection() -> Self {
Self {
kind: NotThreadSafeKind::Connection,
}
}
#[inline]
pub(super) const fn statement() -> Self {
Self {
kind: NotThreadSafeKind::Statement,
}
}
}
impl fmt::Debug for NotThreadSafe {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.kind.fmt(f)
}
}
impl fmt::Display for NotThreadSafe {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "a {} object is not thread safe", self.kind)
}
}
impl core::error::Error for NotThreadSafe {}
#[derive(Debug)]
enum NotThreadSafeKind {
Connection,
Statement,
}
impl fmt::Display for NotThreadSafeKind {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
NotThreadSafeKind::Connection => write!(f, "connection"),
NotThreadSafeKind::Statement => write!(f, "statement"),
}
}
}
pub struct CapacityError {
kind: CapacityErrorKind,
}
#[derive(Debug)]
enum CapacityErrorKind {
Capacity { len: usize, max: usize },
}
impl CapacityError {
#[inline]
pub(super) fn capacity(len: usize, max: usize) -> Self {
Self {
kind: CapacityErrorKind::Capacity { len, max },
}
}
}
impl fmt::Display for CapacityError {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.kind {
CapacityErrorKind::Capacity { len, max } => {
write!(f, "size {len} exceeds fixed buffer size {max}")
}
}
}
}
impl fmt::Debug for CapacityError {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.kind.fmt(f)
}
}
impl core::error::Error for CapacityError {}