Skip to content

feat!: Use bon builder style instead of several methods - #472

Merged
martin-g merged 15 commits into
mainfrom
use-bon-builder-style
Feb 20, 2026
Merged

martin-g merged 15 commits into
mainfrom
use-bon-builder-style

Conversation

@martin-g

Copy link
Copy Markdown
Member

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()

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()`
@martin-g

Copy link
Copy Markdown
Member Author

@Kriskras99 What do you think about this ?
The same could be used also for Schema::map(), but also for several other places where we have some_method(schema) and some_method_with_schemata(schema, schemata)

@martin-g
martin-g marked this pull request as draft February 19, 2026 11:59
@Kriskras99

Copy link
Copy Markdown
Contributor

I think this would definitely be an improvement, but maybe better as a builder directly on Schema?
To get something like Schema::array(array_schema).attributes(attr).build().

This is also very useful for other functions which take schemata indeed

@martin-g

Copy link
Copy Markdown
Member Author

I think this would definitely be an improvement, but maybe better as a builder directly on Schema?

According to https://bon-rs.com/guide/overview#method-builder only #[builder] on a method named new() will generate a .build() finish function. All others will generate .call().

To get something like Schema::array(array_schema).attributes(attr).build().

But I think I can overwrite the finish_fn and rename .call() to .build()! I agree that it will be more "natural" for a Builder!
Let me try it!

@Kriskras99

Copy link
Copy Markdown
Contributor

Comment thread WARP.md Resolved

@Kriskras99 Kriskras99 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should all documentation to the function arguments so that the generated builder gets documentation: https://bon-rs.com/guide/basics/documenting

Comment thread avro/src/reader.rs Outdated
#[builder(finish_fn = build)]
pub fn builder(
#[builder(start_fn)] reader: R,
schema: Option<&'a Schema>,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe rename this to reader_schema, so it's clearer that this is not the writer schema

Comment thread avro/src/reader.rs Resolved
@martin-g
martin-g marked this pull request as ready for review February 19, 2026 20:38
@martin-g

Copy link
Copy Markdown
Member Author

Ideas what else could use this style ?

…ema::map()

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

Copy link
Copy Markdown
Contributor

The *datum* functions would also be a good fit, although it would be better of those functions are removed in favour of a DatumReader/DatumWriter struct that can actually cache the resolved schemas.

We can do that in a separate PR.

It does not use Bon builders as the other methods because it has just a
single argument (Vec<String>) and we don't expect that more will be ever
added.
@martin-g
martin-g force-pushed the use-bon-builder-style branch from 582b5d6 to 5328dfe Compare February 20, 2026 08:49
@martin-g
martin-g merged commit ec7abd8 into main Feb 20, 2026
21 checks passed
@martin-g
martin-g deleted the use-bon-builder-style branch February 20, 2026 08:54
@martin-g martin-g added this to the 0.22.0 milestone Feb 20, 2026
Kriskras99 pushed a commit that referenced this pull request Feb 22, 2026
* 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()`

* Fix doc tests.

* Use `.build()` as a finish_fn for the bon style function builders

* Use bon function builder for Schema::map() too

* Use Bon function style builder for Reader constructors

* Re-add the docstring for the convenience Reader::new() ctor

Simplify the resolution of `schemata`

* Rename Reader's schema to reader_schema for more clarity

* Add arguments for the new `default` fields in Schema::array() and Schema::map()

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

* Add a Schema::r#enum() builder method

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

* Add Schema::fixed(name, size) builder method

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

* Add Schema::record() builder

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

* Extract the builder methods and their tests into a module.

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

* Fix the issue number in the unit tests' names

* Move Schema::union() to schema/builders.rs

It does not use Bon builders as the other methods because it has just a
single argument (Vec<String>) and we don't expect that more will be ever
added.

---------

Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
@Kriskras99 Kriskras99 mentioned this pull request Jun 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants