Skip to main content

Crate field_kinds

Crate field_kinds 

Source
Expand description

Compile-time struct field introspection via derive macro.

This crate provides the FieldKinds derive macro that generates compile-time metadata about struct fields, including their names, types, categories, and custom tags.

§Features

  • Field names: Get field names as &'static str
  • Serialized names: Supports #[serde(rename)] and #[serde(rename_all)]
  • Type categories: Automatic categorization (numeric, text, bool, optional, collection)
  • Custom tags: Add arbitrary tags to fields via #[field_tags("tag1", "tag2")]
  • Static metadata: All field info available as const FIELDS: &'static [FieldMeta]
  • Zero runtime cost: All metadata is computed at compile time

§Example

use field_kinds::{Category, FieldKinds, FieldKindsExt, VisitFields};

#[derive(FieldKinds)]
#[serde(rename_all = "camelCase")]
struct User {
    user_id: u64,
    user_name: String,
    is_active: bool,
    #[field_tags("sensitive", "pii")]
    email: Option<String>,
}

// Get field names
assert_eq!(User::field_names(), vec!["user_id", "user_name", "is_active", "email"]);

// Get serialized names (with rename_all applied)
assert_eq!(User::serialized_names(), vec!["userId", "userName", "isActive", "email"]);

// Filter by category
assert_eq!(User::fields_by_category(Category::NUMERIC), vec!["user_id"]);
assert_eq!(User::fields_by_category(Category::TEXT), vec!["user_name"]);

// Filter by tag
assert_eq!(User::fields_by_tag("sensitive"), vec!["email"]);

// Check field existence
assert!(User::has_field("user_id"));
assert!(!User::has_field("nonexistent"));

// Access static metadata directly
assert_eq!(User::FIELDS.len(), 4);

§Attributes

§Struct-level

  • #[serde(rename_all = "...")] - Apply case conversion to serialized names

§Field-level

  • #[serde(rename = "...")] - Override serialized name for a field
  • #[field_tags("tag1", "tag2")] - Add custom tags to a field
  • #[field_kinds(skip)] - Skip a field from introspection

Structs§

Bool
Marker type for boolean type.
Category
A type-safe wrapper for field category names.
Collection
Marker type for collection types (Vec, HashSet, HashMap, arrays, slices).
FieldMeta
Runtime-accessible metadata for a single field.
Numeric
Marker type for numeric types (i8-i128, u8-u128, f32, f64, isize, usize).
Optional
Marker type for optional types (Option<T>).
Text
Marker type for text types (String, &str, Box<str>, char).
Unknown
Marker type for types that don’t match any known category.

Traits§

Categorized
Trait for mapping Rust types to their categories.
FieldInfo
Trait representing compile-time information about a struct field.
FieldKindsExt
Extension trait providing convenient methods for field introspection.
TypeCategory
Trait for type category markers.
VisitFields
Trait for types that provide static field metadata.

Derive Macros§

FieldKinds