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
impl Remapper
Sourcepub fn rule(self, from: DataType, to: DataType) -> Self
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.
Sourcepub fn remap_dt(&self, dt: DataType) -> DataType
pub fn remap_dt(&self, dt: DataType) -> DataType
Applies the remap operation to a datatype, returning the remapped datatype.
Sourcepub fn remap_types(&self, types: Types) -> Types
pub fn remap_types(&self, types: Types) -> Types
Applies the remap operation to every datatype in a Types collection, returning the remapped collection.