Fix formatting of validation errors

This commit is contained in:
Yuriy Dupyn 2024-01-27 17:26:00 +01:00
parent f30d749962
commit 4e5959a53a

View file

@ -11,29 +11,20 @@ pub enum ValidationError {
TableDoesNotExist(TableName), TableDoesNotExist(TableName),
#[error("table {0} already exists")] #[error("table {0} already exists")]
TableAlreadyExists(TableName), TableAlreadyExists(TableName),
// TODO #[error("columns {0:?} do not exist")]
// #[error("columns {0} do not exist")]
#[error("columns do not exist")]
ColumnsDoNotExist(Vec<ColumnName>), ColumnsDoNotExist(Vec<ColumnName>),
#[error("duplicate column {0}")] #[error("duplicate column {0}")]
DuplicateColumn(ColumnName), DuplicateColumn(ColumnName),
// TODO: You need to actually print the error message #[error("type mismatch at column `{column_name:?}` (expected {expected_type:?}, found {received_type:?})")]
#[error("type mismatch")] TypeMismatch {
TypeMismatch(TypeMismatch), column_name: ColumnName,
// TODO received_type: DbType,
// #[error("values for required columns {0} are missing")] expected_type: DbType,
#[error("values for required columns are missing")] },
#[error("values for required columns {0:?} are missing")]
RequiredColumnsAreMissing(Vec<ColumnName>) RequiredColumnsAreMissing(Vec<ColumnName>)
} }
// TODO: Add derive(Error)
#[derive(Debug)]
pub struct TypeMismatch {
pub column_name: ColumnName,
pub received_type: DbType,
pub expected_type: DbType,
}
pub type DbSchema<'a> = Vec<(TableName, &'a TableSchema)>; pub type DbSchema<'a> = Vec<(TableName, &'a TableSchema)>;
/// Validates the operation based on db_metadata /// Validates the operation based on db_metadata
@ -178,7 +169,7 @@ pub fn validate_insert(table_name: &TableName, insertion_values: &InsertionValue
let expected_type = schema.get_type_at(column_name).ok_or(ValidationError::ColumnsDoNotExist(vec![column_name.to_string()]))?; // By the previous validation steps this is never gonna trigger an error. let expected_type = schema.get_type_at(column_name).ok_or(ValidationError::ColumnsDoNotExist(vec![column_name.to_string()]))?; // By the previous validation steps this is never gonna trigger an error.
let value_type = value.to_type(); let value_type = value.to_type();
if value_type != expected_type { if value_type != expected_type {
return Err(ValidationError::TypeMismatch(TypeMismatch { column_name: column_name.to_string(), received_type: value_type, expected_type })); return Err(ValidationError::TypeMismatch { column_name: column_name.to_string(), received_type: value_type, expected_type });
} }
} }
@ -199,7 +190,7 @@ fn validate_condition(condition: &Option<Condition>, schema: &TableSchema) -> Re
let expected_type: DbType = schema.get_type_at(column_name).ok_or(ValidationError::ColumnsDoNotExist(vec![column_name.to_string()]))?; let expected_type: DbType = schema.get_type_at(column_name).ok_or(ValidationError::ColumnsDoNotExist(vec![column_name.to_string()]))?;
let value_type: DbType = value.to_type(); let value_type: DbType = value.to_type();
if !expected_type.eq(&value_type) { if !expected_type.eq(&value_type) {
return Err(ValidationError::TypeMismatch(TypeMismatch { column_name: column_name.to_string(), received_type: value_type, expected_type })); return Err(ValidationError::TypeMismatch { column_name: column_name.to_string(), received_type: value_type, expected_type });
} }
} }
} }