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; fieldfromreaches R ase$to.as_character,as_integer,as_double,as_logical: the field’s usual serialization as one vector of that R type, as theminiextendr_api::serde::as_character& co. field helpers give it from the error type’s own#[serde(serialize_with = ..)]:character(0)for an empty sequence,NAfor aNoneelement. A value that doesn’t fit (a string underas_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
renametarget 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
impl SerdeErrorShape
Sourcepub const fn tag(self, tag: &'static str) -> Self
pub const fn tag(self, tag: &'static str) -> Self
The internally tagged discriminator field (#[serde(tag = "...")]),
consumed as the variant.
Sourcepub const fn prefix(self, prefix: &'static str) -> Self
pub const fn prefix(self, prefix: &'static str) -> Self
The family class: conditions get c("<prefix>_<variant>", "<prefix>").
Sourcepub const fn rename(
self,
pairs: &'static [(&'static str, &'static str)],
) -> Self
pub const fn rename( self, pairs: &'static [(&'static str, &'static str)], ) -> Self
Splice field from as to, for each (from, to) pair.
Sourcepub const fn as_logical(self, fields: &'static [&'static str]) -> Self
pub const fn as_logical(self, fields: &'static [&'static str]) -> Self
Give these fields an R logical vector.
Sourcepub const fn as_integer(self, fields: &'static [&'static str]) -> Self
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.
Sourcepub const fn as_double(self, fields: &'static [&'static str]) -> Self
pub const fn as_double(self, fields: &'static [&'static str]) -> Self
Give these fields an R double vector; integers widen.
Sourcepub const fn as_character(self, fields: &'static [&'static str]) -> Self
pub const fn as_character(self, fields: &'static [&'static str]) -> Self
Give these fields an R character vector.
const fn with_type(self, index: usize, fields: &'static [&'static str]) -> Self
Sourceconst fn type_option(&self, name: &str) -> Option<&'static str>
const fn type_option(&self, name: &str) -> Option<&'static str>
The as_* option that lists name.
Trait Implementations§
Source§impl Clone for SerdeErrorShape
impl Clone for SerdeErrorShape
impl Copy for SerdeErrorShape
Source§impl Debug for SerdeErrorShape
impl Debug for SerdeErrorShape
Source§impl Default for SerdeErrorShape
impl Default for SerdeErrorShape
Source§impl Eq for SerdeErrorShape
impl Eq for SerdeErrorShape
Source§#[doc(hidden)]fn assert_fields_are_eq(&self)
#[doc(hidden)]fn assert_fields_are_eq(&self)
derive_eq_internals)1.0.0 (const: unstable) · Source§#[doc(hidden)]fn assert_receiver_is_total_eq(&self)
#[doc(hidden)]fn assert_receiver_is_total_eq(&self)
implementation detail of #[derive(Eq)]
Source§impl PartialEq for SerdeErrorShape
impl PartialEq for SerdeErrorShape
impl StructuralPartialEq for SerdeErrorShape
impl TrivialClone for SerdeErrorShape
Auto Trait Implementations§
impl Freeze for SerdeErrorShape
impl RefUnwindSafe for SerdeErrorShape
impl Send for SerdeErrorShape
impl Sync for SerdeErrorShape
impl Unpin for SerdeErrorShape
impl UnsafeUnpin for SerdeErrorShape
impl UnwindSafe for SerdeErrorShape
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> ConsumingFallible for Twhere
T: Clone,
impl<T> Printable for T
Source§impl<T> RCopy for Twhere
T: Copy,
impl<T> RCopy for Twhere
T: Copy,
Source§impl<T> RDebug for Twhere
T: Debug,
impl<T> RDebug for Twhere
T: Debug,
Source§impl<T> SizeHint for Twhere
T: ?Sized,
impl<T> SizeHint for Twhere
T: ?Sized,
Source§default fn lower_bound(&self) -> usize
default fn lower_bound(&self) -> usize
core_io_internals)[u8; 12] could return any value between 0 and
12 inclusively as a correct implementation. Read moreSource§impl<T> SizedTypeProperties for T
impl<T> SizedTypeProperties for T
Source§#[doc(hidden)]const SIZE: usize = _
#[doc(hidden)]const SIZE: usize = _
sized_type_properties)Source§#[doc(hidden)]const ALIGN: usize = _
#[doc(hidden)]const ALIGN: usize = _
sized_type_properties)Source§#[doc(hidden)]const ALIGNMENT: Alignment = _
#[doc(hidden)]const ALIGNMENT: Alignment = _
ptr_alignment_type)Source§#[doc(hidden)]const IS_ZST: bool = _
#[doc(hidden)]const IS_ZST: bool = _
sized_type_properties)Source§#[doc(hidden)]const LAYOUT: Layout = _
#[doc(hidden)]const LAYOUT: Layout = _
sized_type_properties)Source§#[doc(hidden)]const MAX_SLICE_LEN: usize = _
#[doc(hidden)]const MAX_SLICE_LEN: usize = _
sized_type_properties)[Self]. Read moreLayout§
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