use std::any::Any; use std::error::Error; use std::fmt::{Debug, Display}; // Autoderef specialization similar to `clap::value_parser!()`. pub struct SpecErrorWrapper(pub E); pub trait SpecError: Sized { fn __sqlx_spec_error( &self, ) -> fn(SpecErrorWrapper) -> Box; } impl SpecError for &&&&SpecErrorWrapper where E: Error + Send + Sync + 'static, { fn __sqlx_spec_error( &self, ) -> fn(SpecErrorWrapper) -> Box { |e| Box::new(e.0) } } impl SpecError for &&&SpecErrorWrapper where E: Display, { fn __sqlx_spec_error( &self, ) -> fn(SpecErrorWrapper) -> Box { |e| e.0.to_string().into() } } impl SpecError for &&SpecErrorWrapper where E: Debug, { fn __sqlx_spec_error( &self, ) -> fn(SpecErrorWrapper) -> Box { |e| format!("{:?}", e.0).into() } } impl SpecError for &SpecErrorWrapper where E: Any, { fn __sqlx_spec_error( &self, ) -> fn(SpecErrorWrapper) -> Box { |_e| format!("unprintable error: {}", std::any::type_name::()).into() } } impl SpecError for SpecErrorWrapper { fn __sqlx_spec_error( &self, ) -> fn(SpecErrorWrapper) -> Box { |_e| "unprintable error: (unprintable type)".into() } } #[doc(hidden)] #[macro_export] macro_rules! __spec_error { ($e:expr) => {{ use $crate::spec_error::{SpecError, SpecErrorWrapper}; let wrapper = SpecErrorWrapper($e); let wrap_err = wrapper.__sqlx_spec_error(); wrap_err(wrapper) }}; } #[test] fn test_spec_error() { #[derive(Debug)] struct DebugError; struct AnyError; let _e: Box = __spec_error!(std::io::Error::from(std::io::ErrorKind::Unsupported)); let _e: Box = __spec_error!("displayable error"); let _e: Box = __spec_error!(DebugError); let _e: Box = __spec_error!(AnyError); let _e: Box = __spec_error!(&1i32); }