Skip to content

refactor: Centralize generating the actual impl in the Implementation struct - #561

Merged
Kriskras99 merged 3 commits into
mainfrom
refactor/derive_implementation
Jun 26, 2026
Merged

Kriskras99 merged 3 commits into
mainfrom
refactor/derive_implementation

Conversation

@Kriskras99

Copy link
Copy Markdown
Contributor

This makes it clearer how everything fits together, and allows extending shared logic further (for example, adding generated tests). And because it is not a TokenStream, accidentally returning only a schema expression instead of the entire implementation results in a compilation error.

This also fixes an issue where you couldn't override the field_default implementation for a transparent struct via a field attribute.

This should be the last refactor PR before the actual enum PRs

@martin-g

Copy link
Copy Markdown
Member

@Kriskras99 This needs resolving of conflicts.

Base automatically changed from refactor/derive to main June 23, 2026 19:07
@Kriskras99
Kriskras99 force-pushed the refactor/derive_implementation branch from c601ca9 to 8bf4969 Compare June 23, 2026 19:15
@Kriskras99

Copy link
Copy Markdown
Contributor Author

@martin-g Thanks for the headsup, is fixed now!

Comment thread avro_derive/src/attributes/mod.rs Resolved
Comment thread avro_derive/src/enums/plain.rs Outdated
let default_value = default_enum_variant(data_enum, ident_span)?;
if data.variants.iter().all(|v| Fields::Unit == v.fields) {
let default_value = default_enum_variant(&data, input_span)?;
let default = preserve_optional(default_value);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I wonder should container_attrs.rename_all be used also for the default value ?!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes it should! This did cause a kind of snowball effect as to fix this:

  • I needed the serde_json::Value instead of a TokenStream
  • Needed to restructure the plain enum code
  • Decided to also implement a Value to TokenStream function so we don't need to parse at runtime
  • I also made the serializer less struct (but keeping a fast path!), allowing skip to work on enum variants

Comment thread avro_derive/src/structs.rs Outdated
Ok((
field_to_schema_expr(&field, attrs.with.clone())?,
field_to_record_fields_expr(&field, attrs.with)?,
let field_default_expr = if container_attrs.default.is_none() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hm. If there is no container default then use the field's default. Shouldn't the field's default overwrite the container's one ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think you're right, and even stricter: I don't think default should be allowed as a container attribute on a transparent struct, so I've added it to the deny list.

Comment thread avro_derive/src/structs.rs Resolved
…ion` struct

This makes it clearer how everything fits together, and allows extending shared
logic further (for example, adding generated tests). And because it is not
a `TokenStream`, accidentally returning only a schema expression instead of the
entire implementation results in a compilation error.
- Only allow the default attribute on the field of a transparent struct
- Improve the plain enum schema generation
  - Skipped variants are now actually skipped
  - The container default attribute is used first, then comes the variant default attribute
- `serde_json::Value`s are now constructed instead of parsed at runtime
@Kriskras99
Kriskras99 force-pushed the refactor/derive_implementation branch from d5b14dc to c626294 Compare June 26, 2026 06:19
Comment thread avro_derive/src/enums/plain.rs Resolved
Comment thread avro_derive/src/enums/mod.rs Resolved
Comment thread avro_derive/tests/derive.rs Resolved
Comment thread avro_derive/src/utils.rs Resolved
@Kriskras99
Kriskras99 merged commit 791e77f into main Jun 26, 2026
13 checks passed
@Kriskras99
Kriskras99 deleted the refactor/derive_implementation branch June 26, 2026 08:45
@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