Skip to main content

Module match_arg

Module match_arg 

Source
Expand description

match.arg-style string conversion for enums.

Provides the MatchArg trait for converting Rust enums to/from R character strings with partial matching, like R’s match.arg(). Use #[derive(MatchArg)] on C-style enums to auto-generate the implementation. match.arg-style enum conversion for R string arguments.

This module provides the MatchArg trait for converting between Rust fieldless enums and R character strings with match.arg semantics (exact match or unique partial matching).

§Usage

use miniextendr_api::MatchArg;

#[derive(Copy, Clone, MatchArg)]
#[match_arg(rename_all = "snake_case")]
enum Mode {
    Fast,
    Safe,
    Debug,
}

#[miniextendr]
fn run(#[miniextendr(match_arg)] mode: Mode) -> String {
    format!("{mode:?}")
}

The generated R wrapper uses base::match.arg() for validation before the main .Call(), giving users familiar R error messages and partial matching.

Enums§

MatchArgError
Error type for MatchArg conversion failures.

Traits§

MatchArg
Trait for enum types that support match.arg-style string conversion.

Functions§

choices_sexp
Build an R character vector (STRSXP) from the choices of a MatchArg type.
match_arg_from_sexp
Extract a string from an R SEXP (STRSXP or factor) and match it against the choices of a MatchArg type.