Skip to main content

Module condition

Module condition 

Source
Expand description

Condition macros and signal enum for the Rust→R condition pipeline.

This module provides two things:

  1. RCondition enum — the internal panic payload used by error!(), warning!(), message!(), and condition!() macros. Caught by with_r_unwind_protect_error_in_r before the generic panic→error path, then forwarded to R as a structured condition with rust_* class layering.

  2. AsRError struct — wraps any E: std::error::Error and preserves the full error chain (cause/source) when converting to an R error message. Use as the Err type in Result returns.

§Condition macros

The four macros are the user-facing API for raising non-panic conditions from Rust. They all require error_in_r mode (the default for #[miniextendr] functions):

use miniextendr_api::{error, warning, message, condition};

#[miniextendr]
fn demo_error() {
    error!("something went wrong: {}", 42);
}

#[miniextendr]
fn demo_warning() {
    warning!("something looks suspicious");
}

#[miniextendr]
fn demo_message() {
    message!("progress: {} of {}", 1, 10);
}

#[miniextendr]
fn demo_condition() {
    condition!("a signallable condition");
}

Optional class = extension for programmatic catching:

#[miniextendr]
fn typed_error(name: &str) {
    error!(class = "my_error", "missing field: {name}");
}
tryCatch(typed_error("x"), my_error = function(e) "caught!")
# [1] "caught!"

§AsRError

use miniextendr_api::condition::AsRError;

#[miniextendr]
fn parse_config(path: &str) -> Result<i32, AsRError<std::io::Error>> {
    let content = std::fs::read_to_string(path).map_err(AsRError)?;
    Ok(content.len() as i32)
}

Structs§

AsRError
Structured error wrapper that preserves the std::error::Error cause chain.

Enums§

RCondition 👻
Internal panic payload for structured R conditions.

Functions§

repanic_if_rust_error
Inspect a SEXP returned by a trait-ABI vtable shim and, if it is a tagged error value, re-panic with the reconstructed RCondition.