use thiserror::Error;
#[derive(Error, Debug)]
pub enum SqlRuntimeError {
#[error("{0}")]
CustomError(String),
}
impl SqlRuntimeError {
pub fn from_string(message: String) -> Box<Self> {
Box::new(SqlRuntimeError::CustomError(message))
}
pub fn from_strng(message: &str) -> Box<Self> {
Box::new(SqlRuntimeError::CustomError(message.to_string()))
}
}
pub type SqlResult<T> = Result<T, Box<SqlRuntimeError>>;
#[doc(hidden)]
pub(crate) fn r2o<T>(result: SqlResult<T>) -> SqlResult<Option<T>> {
match result {
Err(e) => Err(e),
Ok(value) => Ok(Some(value)),
}
}
#[doc(hidden)]
pub(crate) fn convert_error<T, E>(x: Result<T, E>) -> SqlResult<T>
where
E: std::fmt::Display,
{
match x {
Ok(value) => Ok(value),
Err(e) => Err(SqlRuntimeError::from_string(e.to_string())),
}
}
#[doc(hidden)]
pub fn unwrap_sql_result<T>(data: SqlResult<Option<T>>) -> SqlResult<T> {
match data {
Err(e) => Err(e),
Ok(None) => Err(SqlRuntimeError::from_strng("NULL result produced")),
Ok(Some(data)) => Ok(data),
}
}
#[doc(hidden)]
pub fn wrap_sql_result<T>(data: T) -> SqlResult<T> {
Ok(data)
}