Skip to main content

Remapper

Struct Remapper 

Source
pub struct Remapper { /* private fields */ }
Expand description

Recursively replaces DataTypes within a DataType structure from a set of remap rules.

Remapper is useful when a type should be represented differently for export without changing the original Rust type or derive output. It performs DataType equality checks while walking the DataType structure applying the transformations.

Rules are applied in the order they are registered. For each visited DataType, every matching rule is applied, with each rule seeing the result of the previous matching rule.

WARNING: This is an advanced API!

It needs to be used carefully as it can easily break the safety of the end to end type contract.

You must ensure you have Serde applying the same transformations to the runtime data for it to be sound.

§Examples

Remap u32 to str and i32 to bool:

use specta::{Types, datatype::{DataType, Field, List, NamedDataType, Primitive, Struct}};
use specta_util::Remapper;

let remapper = Remapper::new()
    .rule(Primitive::u32.into(), Primitive::str.into())
    .rule(Primitive::i32.into(), Primitive::bool.into());

// For a single `DataType`
assert_eq!(
    remapper.remap_dt(DataType::List(List::new(Primitive::u32.into()))),
    DataType::List(List::new(Primitive::str.into()))
);

// For a whole collection of types
let types: Types = remapper.remap_types(Types::default());

Implementations§

Source§

impl Remapper

Source

pub fn new() -> Self

Creates a remapper with no rules.

Source

pub fn rule(self, from: DataType, to: DataType) -> Self

Registers a rule that replaces exact matches of from with to.

Rules are checked in the order they are registered.

Source

pub fn remap_dt(&self, dt: DataType) -> DataType

Applies the remap operation to a datatype, returning the remapped datatype.

Source

pub fn remap_types(&self, types: Types) -> Types

Applies the remap operation to every datatype in a Types collection, returning the remapped collection.

Trait Implementations§

Source§

impl Clone for Remapper

Source§

fn clone(&self) -> Remapper

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 Debug for Remapper

Source§

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

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

impl Default for Remapper

Source§

fn default() -> Remapper

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

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> 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> 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.