Skip to main content

match_arg_param

Function match_arg_param 

Source
pub fn match_arg_param<T: MatchArg>(
    sexp: SEXP,
    param: &str,
) -> Result<T, ArgError>
Expand description

Match a choice argument a body received as a raw SEXP exactly as the generated wrapper matches a #[miniextendr(match_arg)] parameter, and with the same error.

The result is match_arg_from_sexp’s (NULL and the full choices vector select the first choice, a factor is read as its labels, one string matches a choice exactly, an alias exactly, or a choice as a unique prefix). The error is the argument error the wrapper raises for parameter param (its R name), word for word:

  • 'mode' must be NULL or a character vector
  • 'mode' must be of length 1
  • 'mode' should be one of "fast", "safe"

ArgError::raise raises it as that condition (kind = "conversion", the crate’s conversion_error_class, e$param; see crate::arg_error!):

ⓘ
use miniextendr_api::{SEXP, match_arg_param, miniextendr};

#[miniextendr]
pub fn run(data: SEXP, mode: SEXP) -> String {
    if is_data_frame(data) {
        // Checked on this path only.
        let mode: Mode = match_arg_param(mode, "mode").unwrap_or_else(|e| e.raise());
        return format!("{mode:?}");
    }
    String::new()
}

The choices are T::CHOICES in declaration order, the formal of a #[miniextendr(match_arg)] parameter. A value forwarded from a parameter with default = "..." wants match_arg_param_with_default.