Skip to content

Conversation

@ziara1
Copy link

@ziara1 ziara1 commented Dec 9, 2025

Motivation

Users often map C-style enums (enums with unit variants and integer discriminants) to integer columns in ScyllaDB (e.g., tinyint, smallint, int). Currently, users must manually implement SerializeValue and DeserializeValue for these enums, which involves tedious boilerplate for matching variants to integers and vice versa.

Solution

This commit extends SerializeValue and DeserializeValue macros to support C-style enums directly.
Users can now annotate their enum with #[scylla(repr = "TYPE")] (e.g., "i8", "i32"), and the macro will generate code that:

  • Serializes the enum variant as its integer discriminant.
  • Deserializes an integer from the DB and matches it back to the corresponding enum variant.

Behavior details:

  • If a discriminant is not specified explicitly, standard Rust rules apply (starts at 0, increments by 1).
  • Deserialization fails with a proper error if the integer value from the DB does not match any known variant.
  • Type checking delegates to the underlying integer type (e.g., i32 checks against Int column).

What's done

Refactored DeserializeValue derive

  • Refactored deserialize_value_derive to handle Data::Enum separately.
  • Added parsing for the #[scylla(repr = "...")] attribute via EnumAttrs.
  • Implemented deserialize_value_derive_enum, which generates a match statement mapping integers from the database back to enum variants.

Refactored SerializeValue derive

  • Refactored serialize_value_derive to handle Data::Enum.
  • Implemented derive_serialize_value_enum, which casts the enum variant to the representation type (e.g., *self as i32) before serialization.

What has been tested

Tests

Added comprehensive tests covering:

  • Integer sizes: Support for i8 (TinyInt), i16 (SmallInt), i32 (Int), and i64 (BigInt).
  • Discriminants: Tested standard auto-incrementing discriminants and manual discriminants with gaps.
  • Error Handling: Verified that deserialization fails with a clear error when the database value doesn't match any enum variant.
  • Type Checking: Verified that using an enum wrapper incorrectly (e.g., against a Text column) produces a type check error.

Fixes: #923

Pre-review checklist

  • I have split my patch into logically separate commits.
  • All commit messages clearly explain what they change and why.
  • I added relevant tests for new features and bug fixes.
  • All commits compile, pass static checks and pass test.
  • PR description sums up the changes and reasons why they should be introduced.
  • I have provided docstrings for the public items that I want to introduce.
  • I have adjusted the documentation in ./docs/source/.
  • I added appropriate Fixes: annotations to PR description.

@github-actions
Copy link

github-actions bot commented Dec 9, 2025

cargo semver-checks found no API-breaking changes in this PR.
Checked commit: d79a4c1

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds support for deriving SerializeValue and DeserializeValue traits for enums, enabling automatic serialization and deserialization of C-style enums to/from integer CQL types. The implementation converts enum variants to their discriminant values, with configurable representation types.

Key Changes:

  • Added enum-specific derive implementations for SerializeValue and DeserializeValue macros
  • Introduced repr attribute to specify the underlying integer representation type (defaults to i32)
  • Made helper functions pub(crate) to support enum deserialization lifetime handling

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 9 comments.

File Description
scylla-macros/src/serialize/value.rs Adds EnumAttrs struct and derive_serialize_value_enum function to generate serialization code for enums, converting them to their integer discriminant values
scylla-macros/src/deserialize/value.rs Adds EnumAttrs struct and deserialize_value_derive_enum function to generate deserialization code for enums, parsing integer values back to enum variants
scylla-macros/src/deserialize/mod.rs Exports lifetime helper functions as pub(crate) to enable reuse in enum deserialization

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@ziara1 ziara1 force-pushed the feature/enum_serialization branch from 04e033e to 6d6796d Compare December 13, 2025 18:09
@ziara1 ziara1 changed the title enum serialization/deserialization macros macros: Add support for enum serialization via #[scylla(repr = "...")] Dec 13, 2025
@ziara1 ziara1 changed the title macros: Add support for enum serialization via #[scylla(repr = "...")] macros: Add support for C-style enum serialization via #[scylla(repr = "...")] Dec 13, 2025
@ziara1 ziara1 marked this pull request as ready for review December 13, 2025 20:25
@wprzytula wprzytula requested a review from Copilot December 13, 2025 20:27
@wprzytula wprzytula added the area/proc-macros Related to procedural macros label Dec 13, 2025
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 13 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@wprzytula
Copy link
Collaborator

@ziara1 Please first address clippy's complaint and Copilot's comments, then ping me.

@ziara1 ziara1 force-pushed the feature/enum_serialization branch 2 times, most recently from 8b54953 to f1633c6 Compare December 20, 2025 10:52
@ziara1
Copy link
Author

ziara1 commented Dec 20, 2025

@wprzytula

@ziara1 ziara1 force-pushed the feature/enum_serialization branch from f1633c6 to f09bbb3 Compare December 21, 2025 08:20
@ziara1 ziara1 requested review from Lorak-mmk and wprzytula January 3, 2026 13:15
@ziara1
Copy link
Author

ziara1 commented Jan 8, 2026

@wprzytula

@wprzytula
Copy link
Collaborator

@ziara1 Please squash fixes into original commits.

@ziara1 ziara1 force-pushed the feature/enum_serialization branch 2 times, most recently from 07a98fa to 3cc1bb1 Compare January 8, 2026 20:23
@ziara1 ziara1 force-pushed the feature/enum_serialization branch from 3cc1bb1 to d79a4c1 Compare January 8, 2026 20:27
@ziara1
Copy link
Author

ziara1 commented Jan 8, 2026

@wprzytula done

@wprzytula wprzytula added this to the 1.5.0 milestone Jan 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/proc-macros Related to procedural macros

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Serialization / Deserialization derive macros should work for enums without data

3 participants