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.
escape_r_string
Escape a Rust &str for embedding inside an R double-quoted string literal.
factor_elt_to_choice 🔒
Extract a single choice from a factor SEXP (INTSXP with levels attribute).
match_arg_from_sexp
Extract a single string from an R SEXP and match it against a MatchArg type.
match_arg_vec_from_sexp
Extract multiple strings from an R SEXP (STRSXP) and match each against the choices of a MatchArg type.
match_arg_vec_into_sexp
Convert a Vec<T: MatchArg> to an R character vector (STRSXP).
match_choice 🔒
Match a string against the choices of a MatchArg type (exact or partial).
sexp_err_to_match_arg_err 🔒
Map a SexpError from a string TryFromSexp conversion into a MatchArgError.