Skip to main content

SerdeErrorShape

Struct SerdeErrorShape 

Source
pub struct SerdeErrorShape {
    tag: Option<&'static str>,
    prefix: Option<&'static str>,
    skip: &'static [&'static str],
    rename: &'static [(&'static str, &'static str)],
    types: [&'static [&'static str]; 4],
}
Expand description

The options of #[miniextendr(serde_error(..))] as a value: declared once, shared by every function that returns one error type.

use miniextendr_api::condition::SerdeErrorShape;

pub const ENGINE: SerdeErrorShape = SerdeErrorShape::new()
    .prefix("engine")
    .skip(&["message"])
    .as_character(&["rule", "column"])
    .as_integer(&["n"]);

Declare it for the error type with serde_error_shape!, and every #[miniextendr] function or method of the crate that returns that type uses it, without naming it:

ⓘ
miniextendr_api::serde_error_shape!(EngineError = ENGINE);

#[miniextendr]
pub fn check(x: f64) -> Result<f64, EngineError> { /* ... */ }

A function can override it for itself: with options of its own, or with another shape named by path, which applies after the declared one:

ⓘ
#[miniextendr(serde_error(as_double("n")))]
pub fn rate(x: f64) -> Result<f64, EngineError> { /* ... */ }

#[miniextendr(serde_error(shape = STRICT, as_double("n")))]
pub fn strict_rate(x: f64) -> Result<f64, EngineError> { /* ... */ }

The path resolves where the function is defined (STRICT, crate::errors::STRICT, or Self::SHAPE on a method for an associated const of its type), so a misspelt one is a compile error. The options act on the serde-classed Err arm, which needs the serde feature.

§Options

One method per serde_error option. Fields are named as the error serializes them, before any rename:

  • tag: the internally tagged discriminator field consumed as the variant; "kind" when unset.
  • prefix: the family class; <crate>_error, from the function’s crate, when unset.
  • skip: fields to drop.
  • rename: (from, to) pairs; field from reaches R as e$to.
  • as_character, as_integer, as_double, as_logical: the field’s usual serialization as one vector of that R type, as the miniextendr_api::serde::as_character & co. field helpers give it from the error type’s own #[serde(serialize_with = ..)]: character(0) for an empty sequence, NA for a None element. A value that doesn’t fit (a string under as_integer) raises an R error naming the field and the option.

A field that a variant doesn’t carry is a no-op for that variant, so one shape covers an enum whose variants differ.

§Precedence

Shapes apply in order: the one declared for the error type, then serde_error(shape = ..), then the function’s own options; for a field set by more than one, the later wins. A later skip drops a field an earlier shape types or renames, and a later as_* or rename keeps a field an earlier shape skips. A type and a new name compose: the function’s rename(n = "count") keeps the shape’s as_integer(&["n"]). A later tag or prefix replaces an earlier one.

§Contradictions don’t compile

Each method checks its fields against the options already set and panics during const evaluation, so a const holding a contradictory shape is a compile error (E0080) naming the field:

  • a field in two as_* lists;
  • a field skipped and also typed or renamed;
  • a field listed twice, renamed twice, or two renames onto one name;
  • a rename target that is one of the condition’s own slots (message, call, kind);
  • an empty list or name, and an option set twice.
ⓘ
use miniextendr_api::condition::SerdeErrorShape;

const ENGINE: SerdeErrorShape = SerdeErrorShape::new()
    .as_integer(&["n"])
    .as_double(&["n"]);

Fields§

§tag: Option<&'static str>§prefix: Option<&'static str>§skip: &'static [&'static str]§rename: &'static [(&'static str, &'static str)]§types: [&'static [&'static str]; 4]

The as_* lists, in SHAPE_TYPE_OPTIONS order.

Implementations§

Source§

impl SerdeErrorShape

Source

pub const fn new() -> Self

A shape that sets nothing.

Source

pub const fn tag(self, tag: &'static str) -> Self

The internally tagged discriminator field (#[serde(tag = "...")]), consumed as the variant.

Source

pub const fn prefix(self, prefix: &'static str) -> Self

The family class: conditions get c("<prefix>_<variant>", "<prefix>").

Source

pub const fn skip(self, fields: &'static [&'static str]) -> Self

Drop these payload fields.

Source

pub const fn rename( self, pairs: &'static [(&'static str, &'static str)], ) -> Self

Splice field from as to, for each (from, to) pair.

Source

pub const fn as_logical(self, fields: &'static [&'static str]) -> Self

Give these fields an R logical vector.

Source

pub const fn as_integer(self, fields: &'static [&'static str]) -> Self

Give these fields an R integer vector; a value outside R’s integer range is an error, not a double.

Source

pub const fn as_double(self, fields: &'static [&'static str]) -> Self

Give these fields an R double vector; integers widen.

Source

pub const fn as_character(self, fields: &'static [&'static str]) -> Self

Give these fields an R character vector.

Source

const fn with_type(self, index: usize, fields: &'static [&'static str]) -> Self

Source

const fn type_option(&self, name: &str) -> Option<&'static str>

The as_* option that lists name.

Source

const fn renames(&self, name: &str) -> bool

Whether a rename pair renames name.

Trait Implementations§

Source§

impl Clone for SerdeErrorShape

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for SerdeErrorShape

Source§

impl Debug for SerdeErrorShape

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for SerdeErrorShape

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Eq for SerdeErrorShape

Source§

#[doc(hidden)]
fn assert_fields_are_eq(&self)

🔬This is a nightly-only experimental API. (derive_eq_internals)
1.0.0 (const: unstable) · Source§

#[doc(hidden)]
fn assert_receiver_is_total_eq(&self)

👎Deprecated since 1.95.0:

implementation detail of #[derive(Eq)]

Source§

impl PartialEq for SerdeErrorShape

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for SerdeErrorShape

Source§

impl TrivialClone for SerdeErrorShape

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> ConsumingFallible for T
where T: Clone,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Printable for T
where T: Copy + Debug,

Source§

impl<T> RClone for T
where T: Clone,

Source§

fn r_clone(&self) -> T

Create a deep copy of this value.
Source§

#[doc(hidden)]
unsafe extern "C" fn __RClone_r_clone_shim( data: *mut c_void, argc: i32, argv: *const SEXP, ) -> SEXP
where Self: Sized + RClone + IntoR,

Method shim for RClone::r_clone. Read more
Source§

impl<T> RCopy for T
where T: Copy,

Source§

fn copy(&self) -> T

Create a bitwise copy of this value. Read more
Source§

fn is_copy(&self) -> bool

Check if this type implements Copy. Read more
Source§

#[doc(hidden)]
unsafe extern "C" fn __RCopy_copy_shim( data: *mut c_void, argc: i32, argv: *const SEXP, ) -> SEXP
where Self: Sized + RCopy + IntoR,

Method shim for RCopy::copy. Read more
Source§

#[doc(hidden)]
unsafe extern "C" fn __RCopy_is_copy_shim( data: *mut c_void, argc: i32, argv: *const SEXP, ) -> SEXP
where Self: Sized + RCopy + IntoR,

Method shim for RCopy::is_copy. Read more
Source§

impl<T> RDebug for T
where T: Debug,

Source§

fn debug_str(&self) -> String

Get a compact debug string representation.
Source§

fn debug_str_pretty(&self) -> String

Get a pretty-printed debug string with indentation.
Source§

#[doc(hidden)]
unsafe extern "C" fn __RDebug_debug_str_shim( data: *mut c_void, argc: i32, argv: *const SEXP, ) -> SEXP
where Self: Sized + RDebug,

Method shim for RDebug::debug_str. Read more
Source§

#[doc(hidden)]
unsafe extern "C" fn __RDebug_debug_str_pretty_shim( data: *mut c_void, argc: i32, argv: *const SEXP, ) -> SEXP
where Self: Sized + RDebug,

Method shim for RDebug::debug_str_pretty. Read more
Source§

impl<T> RDefault for T
where T: Default,

Source§

fn r_default() -> T

Create a new instance with default values.
Source§

impl<T> SizeHint for T
where T: ?Sized,

Source§

default fn lower_bound(&self) -> usize

🔬This is a nightly-only experimental API. (core_io_internals)
Returns a lower bound on the number of elements this container-like item contains. For example, an array [u8; 12] could return any value between 0 and 12 inclusively as a correct implementation. Read more
Source§

default fn upper_bound(&self) -> Option<usize>

🔬This is a nightly-only experimental API. (core_io_internals)
Returns an upper bound on the number of elements this container-like item contains if it can be determined, otherwise None. Read more
Source§

final fn size_hint(&self) -> (usize, Option<usize>)

🔬This is a nightly-only experimental API. (core_io_internals)
Returns an estimate for the number of elements this container like type contains. Read more
Source§

impl<T> SizedTypeProperties for T

Source§

#[doc(hidden)]
const SIZE: usize = _

🔬This is a nightly-only experimental API. (sized_type_properties)
Source§

#[doc(hidden)]
const ALIGN: usize = _

🔬This is a nightly-only experimental API. (sized_type_properties)
Source§

#[doc(hidden)]
const ALIGNMENT: Alignment = _

🔬This is a nightly-only experimental API. (ptr_alignment_type)
Source§

#[doc(hidden)]
const IS_ZST: bool = _

🔬This is a nightly-only experimental API. (sized_type_properties)
true if this type requires no storage. false if its size is greater than zero. Read more
Source§

#[doc(hidden)]
const LAYOUT: Layout = _

🔬This is a nightly-only experimental API. (sized_type_properties)
Source§

#[doc(hidden)]
const MAX_SLICE_LEN: usize = _

🔬This is a nightly-only experimental API. (sized_type_properties)
The largest safe length for a [Self]. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.

Layout§

Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...) attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.

Size: 128 bytes