use std::error::Error as StdError; use std::fmt::{self, Display, Formatter}; use crate::database::HasOutput; use crate::{Database, Type, TypeInfo}; /// A type that can be encoded into a SQL value. pub trait Encode: Send + Sync { /// Encode this value into the specified SQL type. fn encode(&self, ty: &Db::TypeInfo, out: &mut >::Output) -> Result<()>; } impl, Db: Database> Encode for &T { #[inline] fn encode(&self, ty: &Db::TypeInfo, out: &mut >::Output) -> Result<()> { (*self).encode(ty, out) } } /// Errors which can occur while encoding a SQL value. #[derive(Debug)] #[non_exhaustive] pub enum Error { TypeNotCompatible { rust_type_name: &'static str, sql_type_name: &'static str, }, /// A general error raised while encoding a value. Custom(Box), } impl Error { #[doc(hidden)] pub fn msg(msg: D) -> Self { Self::Custom(msg.to_string().into()) } } impl Display for Error { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { match self { Self::TypeNotCompatible { rust_type_name, sql_type_name } => { write!( f, "Rust type `{}` is not compatible with SQL type `{}`", rust_type_name, sql_type_name ) } Self::Custom(error) => { write!(f, "{}", error) } } } } // noinspection DuplicatedCode impl From for Error { fn from(error: E) -> Self { Self::Custom(Box::new(error)) } } /// A specialized result type representing the result of encoding a SQL value. pub type Result = std::result::Result;