pub struct Configuration { /* private fields */ }Expand description
Configuration for runtime-aware TypeScript type remapping.
By default this contains a set of default rules as defined on the module. If you don’t want them use Configuration::empty() instead.
You can add your own rules via Configuration::define(...).
Implementations§
Source§impl Configuration
impl Configuration
Sourcepub fn empty() -> Self
pub fn empty() -> Self
Construct a Configuration without the default rules.
Prefer Configuration::default when possible; the default
rules cover common ecosystem types and may grow over time.
Sourcepub fn rules_mut(&mut self) -> &mut Vec<Rule>
pub fn rules_mut(&mut self) -> &mut Vec<Rule>
Exposes the rules applies to this instance for manual manipulation.
This could be used to filter the default rules if you want to exclude certain ones.
Sourcepub fn define<T: Type>(
self,
dt: impl Fn(DataType) -> DataType + Send + Sync + 'static,
serialize: Option<Transform>,
deserialize: Option<Transform>,
) -> Self
pub fn define<T: Type>( self, dt: impl Fn(DataType) -> DataType + Send + Sync + 'static, serialize: Option<Transform>, deserialize: Option<Transform>, ) -> Self
Define a new rule for a given type T.
dt receives the original DataType for T and must return the
TypeScript-facing DataType that should replace it. serialize
transforms TypeScript runtime values before sending them to Rust.
deserialize transforms values received from Rust into TypeScript
runtime values.
This only works for named types, such as types generated by the
Type derive macro. It does not work for primitives.
use specta::Type;
use specta_typescript::{define, semantic::{Configuration, Transform}};
#[derive(Type)]
struct MyCustomUrl(String);
let mut semantic_types = Configuration::empty();
semantic_types.define::<MyCustomUrl>(
|_| define("URL").into(), // Runtime Specta Type
Some(Transform::new(|value| format!("{value}.toString()"))), // JS -> JSON
Some(Transform::new(|value| format!("new URL({value})"))), // JSON -> JS
);Sourcepub fn enable_lossless_bigints(self) -> Self
pub fn enable_lossless_bigints(self) -> Self
Enable lossless support for large integer types (BigInts).
This remaps usize, isize, u64, i64, u128, and i128 so they
are a BigInt in JavaScript.
This is only safe if your serialization and deserialization layer can losslessly transmit BigInts to the frontend.
Refer to specta-rs/specta#203 for implementation details.
Sourcepub fn enable_lossless_floats(self) -> Self
pub fn enable_lossless_floats(self) -> Self
Enable lossless float support.
By enabling this, you assert that your runtime must preserve NaN,
Infinity, and -Infinity values from JavaScript to Rust and we will flatten number | null into number.
Refer to specta-rs/specta#203 for implementation details.
Sourcepub fn apply_types<'a>(&self, types: &'a Types) -> Cow<'a, Types>
pub fn apply_types<'a>(&self, types: &'a Types) -> Cow<'a, Types>
Transform a Types collection using the configured rules.
This rewrites registered named types so their exported TypeScript shapes match the values produced or consumed by the runtime transforms.
Call this after any format-specific mapping that changes the type graph, and before exporting the final TypeScript definitions.
Sourcepub fn apply_serialize(
&self,
types: &Types,
dt: &DataType,
js_ident: &str,
) -> Option<(Option<DataType>, String)>
pub fn apply_serialize( &self, types: &Types, dt: &DataType, js_ident: &str, ) -> Option<(Option<DataType>, String)>
Scan a DataType tree applying serialize-facing rules.
This assumes Configuration::apply_types has already been applied to the Types.
Therefore the type updates will be shallow (up until references to the Types).
The returned JavaScript expression is built around js_ident and may be
deeply nested for structs, tuples, lists, nullable values, and
intersections.
If no rule or built-in remap matches, None is returned. If a rule
matches but the type shape does not need to change, Some((None, runtime_str)) is returned.
Trait Implementations§
Source§impl Clone for Configuration
impl Clone for Configuration
Source§fn clone(&self) -> Configuration
fn clone(&self) -> Configuration
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more