Skip to main content

try_from_sexp_via_str_parse

Macro try_from_sexp_via_str_parse 

Source
macro_rules! try_from_sexp_via_str_parse {
    ($ty:ty, $label:literal, |$s:ident| $parse:expr) => { ... };
}
Expand description

Implement the four string-parse TryFromSexp impls (T, Option<T>, Vec<T>, Vec<Option<T>>) for a type parsed from an R character vector.

Sibling of into_r_infallible! for the reverse direction: every “parse a scalar type out of an R string” integration (uuid, url, regex, num-bigint) used to hand-write these four impls — some reinventing the STRSXP validation String’s own TryFromSexp already performs. This macro delegates to Option<String> / Vec<Option<String>>, so type, length, and NA checks live in exactly one place.

Semantics:

  • T: NA_character_ / NULLSexpError::Na; parse failure → InvalidValue("invalid <label>: <err>").
  • Option<T>: NA_character_ / NULLNone.
  • Vec<T>: NA elements and parse failures are collected across the whole vector into one batched InvalidValue (see batch_conversion_errors). Per-element entries keep the "NA at index <i> not allowed for Vec<T>" and "invalid <label> at index <i>: <err>" shapes; the first 10 are listed and the remainder is summarized as "and N more".
  • Vec<Option<T>>: NA elements → None; parse failures batch as above.

The parse body is a closure-style |s| expr where s: &str, returning Result<T, E> with E: Display.

try_from_sexp_via_str_parse!(Uuid, "UUID", |s| Uuid::parse_str(s));