Skip to main content

match_arg_from_sexp

Function match_arg_from_sexp 

Source
pub fn match_arg_from_sexp<T: MatchArg>(sexp: SEXP) -> Result<T, MatchArgError>
Expand description

Match one choice argument with the semantics of the generated wrapper’s match_arg check (.miniextendr_match_arg), which are those of base::match.arg(arg, choices):

  • NULL is the first choice;
  • a factor is read as its labels;
  • any other value that is not a character vector is MatchArgError::InvalidType;
  • the full choices vector, T::CHOICES in order and without attributes (what an omitted argument evaluates to when the choices are its formal default), is the first choice;
  • otherwise the value must have length 1 (MatchArgError::InvalidLength) and match a choice exactly, an alias exactly (MatchArg::ALIASES), or a choice as a unique prefix (MatchArgError::NoMatch). The empty string matches nothing, and NA is matched as the string "NA", as pmatch() does (MatchArgError::IsNa when that matches nothing).

Used by the generated TryFromSexp for T implementation, and by the generated C wrapper of a #[miniextendr(match_arg)] parameter, which only sees the single choice the R wrapper already matched. A body matching a raw SEXP argument wants match_arg_param, which words its errors as the wrapper does.