Announcing struct-to-enum: derive enums from your struct fields

Announcing struct-to-enum: derive enums from your struct fields

Hello,
I published struct-to-enum, a crate that generates enums from struct fields at compile time via derive macros.
The primary motivation was enabling compile-time column filter validation for queries, but it's general purpose.

Two macros are provided:

  • FieldName - generates a unit enum where each variant is a field name
  • FieldType - generates a tuple-variant enum where each variant wraps a field's type and value
  #[derive(FieldName, FieldType)]
  struct Point { x: f32, y: f32 }

  // Generates:
  // enum PointFieldName { X, Y }
  // enum PointFieldType { X(f32), Y(f32) }

  let names: [PointFieldName; 2] = Point::field_names();
  let values: [PointFieldType; 2] = Point { x: 1.0, y: 2.0 }.into();

Key features:

  • Nested flattening - inlines a nested struct's variants into the parent enum (one of the reasons for creating this crate)
  • Custom derives/attributes - forward derives/attributes like serde, strum, etc. onto the generated enum
  • Generated enums maintain field definition order

Expected features

  • Field skipping - exclude fields with field attribute
  • Generics - works with lifetime and type parameters

Plans:

  • Improve implementation, read-ability/performance
  • Add renaming
  • allow disabling default traits for FieldName
  • (maybe) reverse-validation: generate sync tests instead of definitions

It is inspired by:

  • Field Types - similar, but less features

  • Strum - get field names

    Your feedback is welcome!

Forgot to add repo...

Also wanted to add that for simple cases where you need to "sync" struct to enum this macro is an overkill. It more about when you have nesting, but still need to "sync" the definitions.