This is an automated email from the ASF dual-hosted git repository. mgrigorov pushed a commit to branch use-bon-builder-style in repository https://gitbox.apache.org/repos/asf/avro-rs.git
commit 58cfb998b3643e03ee3fa052fc3c70e1d781ddc7 Author: Martin Tzvetanov Grigorov <[email protected]> AuthorDate: Thu Feb 19 13:48:34 2026 +0200 feat!: Use bon builder style instead of several methods Instead of having several methods like: Schema::array(Schema) and Schema::array_with_attributes(Schema, BTreeMap) we could use Bon's function builders with start_fn: ``` Schema::array(Schema).call() Schema::array(Schema).attributes(BTreeMap).call() ``` The only annoying part is the `.call()` --- avro/src/decode.rs | 4 ++-- avro/src/encode.rs | 7 +++++-- avro/src/schema/mod.rs | 30 ++++++++++++++---------------- avro/src/schema_equality.rs | 4 ++-- avro/src/serde/derive.rs | 10 +++++----- avro/src/types.rs | 4 ++-- 6 files changed, 30 insertions(+), 29 deletions(-) diff --git a/avro/src/decode.rs b/avro/src/decode.rs index d88ab5f..cfe069d 100644 --- a/avro/src/decode.rs +++ b/avro/src/decode.rs @@ -376,7 +376,7 @@ mod tests { #[test] fn test_decode_array_without_size() -> TestResult { let mut input: &[u8] = &[6, 2, 4, 6, 0]; - let result = decode(&Schema::array(Schema::Int), &mut input); + let result = decode(&Schema::array(Schema::Int).call(), &mut input); assert_eq!(Array(vec!(Int(1), Int(2), Int(3))), result?); Ok(()) @@ -385,7 +385,7 @@ mod tests { #[test] fn test_decode_array_with_size() -> TestResult { let mut input: &[u8] = &[5, 6, 2, 4, 6, 0]; - let result = decode(&Schema::array(Schema::Int), &mut input); + let result = decode(&Schema::array(Schema::Int).call(), &mut input); assert_eq!(Array(vec!(Int(1), Int(2), Int(3))), result?); Ok(()) diff --git a/avro/src/encode.rs b/avro/src/encode.rs index 7efe4a6..a21a326 100644 --- a/avro/src/encode.rs +++ b/avro/src/encode.rs @@ -392,10 +392,13 @@ pub(crate) mod tests { let empty: Vec<Value> = Vec::new(); encode( &Value::Array(empty.clone()), - &Schema::array(Schema::Int), + &Schema::array(Schema::Int).call(), &mut buf, ) - .expect(&success(&Value::Array(empty), &Schema::array(Schema::Int))); + .expect(&success( + &Value::Array(empty), + &Schema::array(Schema::Int).call(), + )); assert_eq!(vec![0u8], buf); } diff --git a/avro/src/schema/mod.rs b/avro/src/schema/mod.rs index 35372ce..01fc253 100644 --- a/avro/src/schema/mod.rs +++ b/avro/src/schema/mod.rs @@ -56,6 +56,7 @@ pub use crate::schema::{ resolve::ResolvedSchema, union::UnionSchema, }; +use bon::bon; /// Represents documentation for complex Avro schemas. pub type Documentation = Option<String>; @@ -383,6 +384,7 @@ type DecimalMetadata = usize; pub(crate) type Precision = DecimalMetadata; pub(crate) type Scale = DecimalMetadata; +#[bon] impl Schema { /// Converts `self` into its [Parsing Canonical Form]. /// @@ -664,16 +666,12 @@ impl Schema { } /// Returns a `Schema::Array` with the given items. - pub fn array(items: Schema) -> Self { - Schema::Array(ArraySchema { - items: Box::new(items), - default: None, - attributes: Default::default(), - }) - } - - /// Returns a `Schema::Array` with the given items and custom attributes. - pub fn array_with_attributes(items: Schema, attributes: BTreeMap<String, JsonValue>) -> Self { + #[builder] + pub fn array( + #[builder(start_fn)] items: Schema, + attributes: Option<BTreeMap<String, JsonValue>>, + ) -> Self { + let attributes = attributes.unwrap_or_default(); Schema::Array(ArraySchema { items: Box::new(items), default: None, @@ -1154,7 +1152,7 @@ mod tests { #[test] fn test_array_schema() -> TestResult { let schema = Schema::parse_str(r#"{"type": "array", "items": "string"}"#)?; - assert_eq!(Schema::array(Schema::String), schema); + assert_eq!(Schema::array(Schema::String).call(), schema); Ok(()) } @@ -1607,7 +1605,8 @@ mod tests { aliases: None, schema: Schema::array(Schema::Ref { name: Name::new("Node")?, - }), + }) + .call(), order: RecordFieldOrder::Ascending, position: 1, custom_attributes: Default::default(), @@ -4682,10 +4681,9 @@ mod tests { #[test] fn test_avro_3927_serialize_array_with_custom_attributes() -> TestResult { - let expected = Schema::array_with_attributes( - Schema::Long, - BTreeMap::from([("field-id".to_string(), "1".into())]), - ); + let expected = Schema::array(Schema::Long) + .attributes(BTreeMap::from([("field-id".to_string(), "1".into())])) + .call(); let value = serde_json::to_value(&expected)?; let serialized = serde_json::to_string(&value)?; diff --git a/avro/src/schema_equality.rs b/avro/src/schema_equality.rs index e65e28c..58b1b01 100644 --- a/avro/src/schema_equality.rs +++ b/avro/src/schema_equality.rs @@ -475,11 +475,11 @@ mod tests { #[test] fn test_avro_3939_compare_array_schemata() { - let schema_one = Schema::array(Schema::Boolean); + let schema_one = Schema::array(Schema::Boolean).call(); assert!(!SPECIFICATION_EQ.compare(&schema_one, &Schema::Boolean)); assert!(!STRUCT_FIELD_EQ.compare(&schema_one, &Schema::Boolean)); - let schema_two = Schema::array(Schema::Boolean); + let schema_two = Schema::array(Schema::Boolean).call(); let specification_eq_res = SPECIFICATION_EQ.compare(&schema_one, &schema_two); let struct_field_eq_res = STRUCT_FIELD_EQ.compare(&schema_one, &schema_two); diff --git a/avro/src/serde/derive.rs b/avro/src/serde/derive.rs index c631db9..b17f259 100644 --- a/avro/src/serde/derive.rs +++ b/avro/src/serde/derive.rs @@ -524,7 +524,7 @@ macro_rules! impl_array_schema ( ($type:ty where T: AvroSchemaComponent) => ( impl<T: AvroSchemaComponent> AvroSchemaComponent for $type { fn get_schema_in_ctxt(named_schemas: &mut Names, enclosing_namespace: &Namespace) -> Schema { - Schema::array(T::get_schema_in_ctxt(named_schemas, enclosing_namespace)) + Schema::array(T::get_schema_in_ctxt(named_schemas, enclosing_namespace)).call() } fn get_record_fields_in_ctxt(_: usize, _: &mut Names, _: &Namespace) -> Option<Vec<RecordField>> { @@ -544,7 +544,7 @@ where T: AvroSchemaComponent, { fn get_schema_in_ctxt(named_schemas: &mut Names, enclosing_namespace: &Namespace) -> Schema { - Schema::array(T::get_schema_in_ctxt(named_schemas, enclosing_namespace)) + Schema::array(T::get_schema_in_ctxt(named_schemas, enclosing_namespace)).call() } fn get_record_fields_in_ctxt( @@ -784,7 +784,7 @@ mod tests { #[test] fn avro_rs_401_slice() -> TestResult { let schema = <[u8]>::get_schema(); - assert_eq!(schema, Schema::array(Schema::Int)); + assert_eq!(schema, Schema::array(Schema::Int).call()); Ok(()) } @@ -792,7 +792,7 @@ mod tests { #[test] fn avro_rs_401_array() -> TestResult { let schema = <[u8; 55]>::get_schema(); - assert_eq!(schema, Schema::array(Schema::Int)); + assert_eq!(schema, Schema::array(Schema::Int).call()); Ok(()) } @@ -804,7 +804,7 @@ mod tests { schema, Schema::union(vec![ Schema::Null, - Schema::array(Schema::array(Schema::Int)) + Schema::array(Schema::array(Schema::Int).call()).call() ])? ); diff --git a/avro/src/types.rs b/avro/src/types.rs index 9c1f05f..5302851 100644 --- a/avro/src/types.rs +++ b/avro/src/types.rs @@ -1351,13 +1351,13 @@ mod tests { ), ( Value::Array(vec![Value::Long(42i64)]), - Schema::array(Schema::Long), + Schema::array(Schema::Long).call(), true, "", ), ( Value::Array(vec![Value::Boolean(true)]), - Schema::array(Schema::Long), + Schema::array(Schema::Long).call(), false, "Invalid value: Array([Boolean(true)]) for schema: Array(ArraySchema { items: Long, default: None, attributes: {} }). Reason: Unsupported value-schema combination! Value: Boolean(true), schema: Long", ),
