Expand description
§#[derive(MatchArg)] - Enum ↔ R String with match.arg Support
This module implements the #[derive(MatchArg)] macro which generates
the MatchArg trait implementation for C-style enums, enabling automatic
conversion between Rust enums and R character strings with partial matching.
§Tradeoff
Without #[derive(MatchArg)], an enum-shaped argument lands in the Rust
function as a raw String that you match on by hand — invalid values
surface as Rust Err deep in the body, with no R-side defaulting and no
partial matching. #[derive(MatchArg)] shifts that work to the wrapper
boundary: the generated R wrapper picks up match.arg-style partial
string matching against the enum’s variant list, fills in the first
variant as the R-visible default, and rejects unknown values before
the Rust function is even called. The variant list reaches the R side
via a write-time placeholder (MX_MATCH_ARG_CHOICES) rather than a
string baked into the proc macro, so adding a variant only requires
re-running the build.
§Usage
#[derive(Copy, Clone, MatchArg)]
enum Mode {
Fast,
Safe,
Debug,
}
// Generates impl MatchArg for Mode, TryFromSexp for Mode, IntoR for Mode.§Attributes
#[match_arg(rename = "name")]- Rename a variant’s choice string#[match_arg(rename_all = "snake_case")]- Rename all variants (snake_case, kebab-case, lower, upper)#[match_arg(alias = "name")]- Another spelling of a variant’s choice, matched only when typed in full (MatchArg::ALIASES, #1843). Repeatable, variants only, never renamed byrename_all. An alias that is empty,"NA", a choice, given twice, or a prefix of another variant’s choice is a compile error. Every error that lists the choices names it after them ((or "name" for "choice"), #2148); the formal, the usage and the@paramline don’t.#[match_arg(alias = "name", listed = false)]- Keep the aliases of this attribute out of every error.listedapplies to the aliases of its own#[match_arg(...)]only; one without an alias, a repeated one, or a value other thantrue/falseis a compile error.
Structs§
- Match
ArgAttrs 🔒 - Parsed
#[match_arg(...)]attributes from an enum or variant.
Functions§
- check_
aliases 🔒 - Refuse the aliases
#[match_arg(alias = "...")]can’t have, all at once (one error per bad alias, spanned on its literal): an alias that is empty (pmatch()matches""to nothing, while the R check’smatch()would map it),"NA"(it would read as a missing value), a choice, given twice (on any variant), or a prefix of another variant’s choice, since the alias would change what typing it selects. A prefix of its own choice only is harmless. The wrappers writer applies the same rules to a hand-writtenMatchArg::ALIASES(miniextendr_api::registry::match_arg_aliases_arg). - derive_
match_ arg - Main entry point for
#[derive(MatchArg)]. - parse_
match_ 🔒arg_ attrs - Parse
#[match_arg(...)]attributes from a list ofsyn::Attribute.