use thiserror::Error;
#[derive(Error, Debug)]
pub enum Error {
#[error("Database error: {0}")]
Database(#[from] sqlx::Error),
#[error("SQL generation error: {message}")]
SqlGeneration { message: String },
#[error("Invalid query: {message}")]
InvalidQuery { message: String },
#[error("Serialization error: {0}")]
Serialization(#[from] serde_json::Error),
#[error("Column '{column}' not found in table '{table}'")]
ColumnNotFound { table: String, column: String },
#[error("Table '{table}' not found")]
TableNotFound { table: String },
}
pub type Result<T> = std::result::Result<T, Error>;
impl Error {
pub fn sql_generation(message: impl Into<String>) -> Self {
Self::SqlGeneration {
message: message.into(),
}
}
pub fn invalid_query(message: impl Into<String>) -> Self {
Self::InvalidQuery {
message: message.into(),
}
}
pub fn column_not_found(table: impl Into<String>, column: impl Into<String>) -> Self {
Self::ColumnNotFound {
table: table.into(),
column: column.into(),
}
}
pub fn table_not_found(table: impl Into<String>) -> Self {
Self::TableNotFound {
table: table.into(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_creation() {
let err = Error::sql_generation("Invalid SELECT");
assert!(matches!(err, Error::SqlGeneration { .. }));
assert_eq!(err.to_string(), "SQL generation error: Invalid SELECT");
}
#[test]
fn test_invalid_query_error() {
let err = Error::invalid_query("Missing WHERE clause");
assert!(matches!(err, Error::InvalidQuery { .. }));
assert_eq!(err.to_string(), "Invalid query: Missing WHERE clause");
}
#[test]
fn test_column_not_found_error() {
let err = Error::column_not_found("users", "invalid_column");
assert!(matches!(err, Error::ColumnNotFound { .. }));
assert_eq!(
err.to_string(),
"Column 'invalid_column' not found in table 'users'"
);
}
#[test]
fn test_table_not_found_error() {
let err = Error::table_not_found("non_existent_table");
assert!(matches!(err, Error::TableNotFound { .. }));
assert_eq!(err.to_string(), "Table 'non_existent_table' not found");
}
}