Skip to main content

Module factor_derive

Module factor_derive 

Source
Expand description

§#[derive(RFactor)] - Enum ↔ R Factor Support

This module implements the #[derive(RFactor)] macro which generates the RFactor trait implementation for C-style enums, enabling automatic conversion between Rust enums and R factors.

§Usage

#[derive(Copy, Clone, RFactor)]
enum Color {
    Red,
    Green,
    Blue,
}

// Generates impl RFactor for Color, IntoR for Color, TryFromSexp for Color,
// and Vec/Option variants.

§Attributes

  • #[r_factor(rename = "name")] - Rename a variant’s level string
  • #[r_factor(rename_all = "snake_case")] - Rename all variants (snake_case, kebab-case, lower, upper)
#[derive(Copy, Clone, RFactor)]
#[r_factor(rename_all = "snake_case")]
enum Status {
    InProgress,  // level: "in_progress"
    #[r_factor(rename = "done")]
    Completed,   // level: "done"
}

§Interaction Factors

For enums wrapping another RFactor type (like R’s interaction()):

#[derive(Copy, Clone, RFactor)]
enum Supplement { OJ, VC }

#[derive(Copy, Clone, RFactor)]
#[r_factor(interaction = ["OJ", "VC"])]  // inner type's levels
enum SpeciesSupplement {
    Setosa(Supplement),
    Versicolor(Supplement),
    Virginica(Supplement),
}
// Levels: ["Setosa.OJ", "Setosa.VC", "Versicolor.OJ", ...]

Structs§

RFactorAttrs 🔒
Parsed #[r_factor(...)] attributes from an enum or variant.

Functions§

derive_interaction_factor 🔒
Generate RFactor, MatchArg, IntoR, and TryFromSexp impls for interaction (tuple variant) enums.
derive_r_factor
Main entry point for #[derive(RFactor)].
derive_simple_factor 🔒
Generate RFactor, MatchArg, IntoR, and TryFromSexp impls for simple (unit variant) enums.
parse_r_factor_attrs 🔒
Parse #[r_factor(...)] attributes from a list of syn::Attribute.