Skip to main content

Configuration

Struct Configuration 

Source
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

Source

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.

Source

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.

Source

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
);
Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn apply_deserialize( &self, types: &Types, dt: &DataType, js_ident: &str, ) -> Option<(Option<DataType>, String)>

Scan a DataType tree applying deserialize-facing rules.

Use this for values received from Rust before exposing them to TypeScript callers.

Trait Implementations§

Source§

impl Clone for Configuration

Source§

fn clone(&self) -> Configuration

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 Configuration

Source§

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

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

impl Default for Configuration

Source§

fn default() -> Self

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.