Skip to content

Commit

Permalink
AVRO-3904: [Rust] Minor improvements to the new schema compatibility …
Browse files Browse the repository at this point in the history
…changes (#2600)

* AVRO-3904: [Rust] Minor improvements to the new schema compatibility changes

Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>

* AVRO-3904: [Rust] Use `Debug` instead of `Display` when printing schemata in CompatibilityError

Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>

---------

Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
  • Loading branch information
martin-g authored Dec 4, 2023
1 parent 9c4103c commit efd3b2a
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 167 deletions.
41 changes: 25 additions & 16 deletions lang/rust/avro/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ pub enum Error {
DeflateCompress(#[source] std::io::Error),

#[error("Failed to finish flate compressor")]
DeflateCompressFinish(std::io::Error),
DeflateCompressFinish(#[source] std::io::Error),

#[error("Failed to decompress with flate")]
DeflateDecompress(#[source] std::io::Error),
Expand Down Expand Up @@ -480,45 +480,44 @@ pub enum Error {
BadCodecMetadata,
}

#[derive(thiserror::Error, Debug)]
#[derive(thiserror::Error, PartialEq)]
pub enum CompatibilityError {
#[error("Schemas are not compatible. Writer schema is {writer_schema_type}, but reader schema is {reader_schema_type}")]
#[error("Incompatible schema types! 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}")]
#[error("Incompatible schema types! The {schema_type} should have been {expected_type:?}")]
TypeExpected {
schema_type: String,
expected_type: String,
expected_type: &'static [SchemaKind],
},

#[error("Schemas are not compatible. Field '{0}' in reader schema does not match the type in the writer schema")]
FieldTypeMismatch(String),
#[error("Incompatible schemata! Field '{0}' in reader schema does not match the type in the writer schema")]
FieldTypeMismatch(String, #[source] Box<CompatibilityError>),

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

#[error("Schemas are not compatible. Field '{0}' in reader schema must have a default value")]
#[error("Incompatible schemata! 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")]
#[error("Incompatible schemata! Reader's symbols must contain all writer's symbols")]
MissingSymbols,

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

#[error("Schemas are not compatible. Name and size don't match for fixed")]
#[error("Incompatible schemata! 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}")]
#[error("Incompatible schemata! 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. Unknown type for '{0}'. Make sure that the type is a valid one")]
#[error(
"Incompatible schemata! Unknown type for '{0}'. Make sure that the type is a valid one"
)]
Inconclusive(String),
}

Expand All @@ -543,3 +542,13 @@ impl fmt::Debug for Error {
write!(f, "{}", msg)
}
}

impl fmt::Debug for CompatibilityError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut msg = self.to_string();
if let Some(e) = self.source() {
msg.extend([": ", &e.to_string()]);
}
write!(f, "{}", msg)
}
}
36 changes: 0 additions & 36 deletions lang/rust/avro/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,42 +206,6 @@ 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 efd3b2a

Please sign in to comment.