#[non_exhaustive]pub struct Transform(/* private fields */);Expand description
A JavaScript expression that converts between a semantic TypeScript runtime value and its JSON-compatible representation.
The closure receives the JavaScript identifier/expression being transformed and must return a JavaScript expression using that value.
§Examples
Convert a JSON string into a TypeScript Date:
use specta_typescript::semantic::Transform;
let transform = Transform::new(|value| format!("new Date({value})"));Convert a Uint8Array into a JSON array of numbers:
use specta_typescript::semantic::Transform;
let transform = Transform::new(|value| format!("[...{value}]"));Use Transform::identity when the TypeScript runtime value already has
the same representation as the value crossing the wire or when JSON.stringify/JSON.parse
is already able to handle the transformation for you.
Implementations§
Source§impl Transform
impl Transform
Sourcepub fn new(runtime: impl Fn(&str) -> String + Send + Sync + 'static) -> Self
pub fn new(runtime: impl Fn(&str) -> String + Send + Sync + 'static) -> Self
Construct a runtime transform from a JavaScript identifier mapper.
The mapper should return a JavaScript expression, not a statement.
use specta_typescript::semantic::Transform;
let transform = Transform::new(|ident| format!("new URL({ident})"));Trait Implementations§
Auto Trait Implementations§
impl Freeze for Transform
impl !RefUnwindSafe for Transform
impl Send for Transform
impl Sync for Transform
impl Unpin for Transform
impl UnsafeUnpin for Transform
impl !UnwindSafe for Transform
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
Mutably borrows from an owned value. Read more