use core::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum BuildError {
LockRequiresSelect,
DistinctOnRequiresPostgres,
EmptyInsert,
EmptyUpdate,
OffsetWithoutLimit,
LockWithUnion,
InvalidHavingOperator(String),
}
impl fmt::Display for BuildError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::LockRequiresSelect => {
f.write_str("for_update()/for_share() is only valid on SELECT")
}
Self::DistinctOnRequiresPostgres => f.write_str("DISTINCT ON requires PostgreSQL"),
Self::EmptyInsert => f.write_str("insert() requires at least one column"),
Self::EmptyUpdate => f.write_str("update() requires at least one column"),
Self::OffsetWithoutLimit => f.write_str("offset(...) requires limit(...)"),
Self::LockWithUnion => {
f.write_str("for_update()/for_share() cannot be combined with UNION")
}
Self::InvalidHavingOperator(op) => write!(
f,
"having() operator {op:?} is not an allowed comparison operator \
(use having_raw() for arbitrary aggregate expressions)"
),
}
}
}
impl std::error::Error for BuildError {}
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
Build(BuildError),
Sqlx(sqlx::Error),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Build(e) => e.fmt(f),
Self::Sqlx(e) => e.fmt(f),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Build(e) => Some(e),
Self::Sqlx(e) => Some(e),
}
}
}
impl From<BuildError> for Error {
fn from(e: BuildError) -> Self {
Self::Build(e)
}
}
impl From<sqlx::Error> for Error {
fn from(e: sqlx::Error) -> Self {
Self::Sqlx(e)
}
}