Skip to content

Commit

Permalink
AVRO-3904: [Rust] new CompatibilityError created so end users can spe…
Browse files Browse the repository at this point in the history
…cified buisness logic per error type
  • Loading branch information
marcosschroh committed Nov 22, 2023
1 parent a51a30f commit f50c002
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 113 deletions.
34 changes: 32 additions & 2 deletions lang/rust/avro/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,9 +478,39 @@ pub enum Error {

#[error("Invalid Avro data! Cannot read codec type from value that is not Value::Bytes.")]
BadCodecMetadata,
}

#[derive(thiserror::Error, Debug)]
pub enum CompatibilityError {
#[error("Schemas are not compatible. Writer schema is {writer_schema_type}, but reader schema is {reader_schema_type}")]
WrongType { writer_schema_type: String, reader_schema_type: String},

#[error("Schemas are not compatible. The {schema_type} should have been {expected_type}")]
TypeExpected {schema_type: String, expected_type: String},

#[error("Schemas are not compatible. Field '{0}' in reader schema does not match the type in the writer schema")]
FieldTypeMismatch(String),

#[error("Schemas are not compatible. Schemas mismatch")]
SchemaMismatch,

#[error("Schemas are not compatible. Field '{0}' in reader schema must have a default value")]
MissingDefaultValue(String),

#[error("Schemas are not compatible. Reader's symbols must contain all writer's symbols")]
MissingSymbols,

#[error("Schemas are not compatible. All elements in union must match for both schemas")]
MissingUnionElements,

#[error("Schemas are not compatible. Name and size don't match for fixed")]
FixedMismatch,

#[error("Schemas are not compatible. The name must be the same for both schemas. Writer's name {writer_name} and reader's name {reader_name}")]
NameMismatch {writer_name: String, reader_name: String},

#[error("Schemas are not compatible. '{0}'")]
CompatibilityError(String),
#[error("Schemas are not compatible. Unknown type for '{0}'. Make sure that the type is a valid one")]
Inconclusive(String),
}

impl serde::ser::Error for Error {
Expand Down
37 changes: 37 additions & 0 deletions lang/rust/avro/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,43 @@ impl From<&types::Value> for SchemaKind {
}
}

// Implement `Display` for `SchemaKind`.
impl fmt::Display for Schema {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Schema::Null => write!(f, "Null"),
Schema::Boolean => write!(f, "Boolean"),
Schema::Int => write!(f, "Int"),
Schema::Long => write!(f, "Long"),
Schema::Float => write!(f, "Float"),
Schema::Double => write!(f, "Double"),
Schema::Bytes => write!(f, "Bytes"),
Schema::String => write!(f, "String"),
Schema::Array(..)=> write!(f, "Array"),
Schema::Map(..) => write!(f, "Map"),
Schema::Union(..) => write!(f, "Union"),
Schema::Record(..) => write!(f, "Record"),
Schema::Enum(..) => write!(f, "Enum"),
Schema::Fixed(..) => write!(f, "Fixed"),
Schema::Decimal(..) => write!(f, "Decimal"),
Schema::BigDecimal => write!(f, "BigDecimal"),
Schema::Uuid => write!(f, "Uuid"),
Schema::Date => write!(f, "Date"),
Schema::TimeMillis => write!(f, "TimeMillis"),
Schema::TimeMicros => write!(f, "TimeMicros"),
Schema::TimestampMillis => write!(f, "TimestampMillis"),
Schema::TimestampMicros => write!(f, "TimestampMicros"),
Schema::LocalTimestampMillis => write!(f, "LocalTimestampMillis"),
Schema::LocalTimestampMicros => write!(f, "LocalTimestampMicros"),
Schema::Duration => write!(f, "Duration"),
Schema::Ref { name } => {
write!(f, "{}", name.name)
},
}
}
}


/// Represents names for `record`, `enum` and `fixed` Avro schemas.
///
/// Each of these `Schema`s have a `fullname` composed of two parts:
Expand Down
Loading

0 comments on commit f50c002

Please sign in to comment.