specta_typescript/
typescript.rs1use std::{borrow::Cow, path::Path};
2
3use specta::TypeCollection;
4use specta_serde::SerdeMode;
5
6use crate::{BigIntExportBehavior, Branded, BrandedTypeExporter, Error, Exporter, Layout};
7
8#[derive(Debug, Clone)]
10#[non_exhaustive]
11pub struct Typescript(Exporter);
12
13impl Default for Typescript {
14 fn default() -> Self {
15 Exporter::default().into()
16 }
17}
18
19impl From<Typescript> for Exporter {
20 fn from(value: Typescript) -> Self {
21 value.0
22 }
23}
24
25impl From<Exporter> for Typescript {
26 fn from(mut value: Exporter) -> Self {
27 value.jsdoc = false;
28 Self(value)
29 }
30}
31
32impl Typescript {
33 pub fn new() -> Self {
35 Default::default()
36 }
37
38 pub fn header(self, header: impl Into<Cow<'static, str>>) -> Self {
42 Self(self.0.header(header))
43 }
44
45 pub fn bigint(self, bigint: BigIntExportBehavior) -> Self {
47 Self(self.0.bigint(bigint))
48 }
49
50 pub fn layout(self, layout: Layout) -> Self {
52 Self(self.0.layout(layout))
53 }
54
55 pub fn branded_type_impl(
59 self,
60 builder: impl for<'a> Fn(BrandedTypeExporter<'a>, &Branded) -> Result<Cow<'static, str>, Error>
61 + Send
62 + Sync
63 + 'static,
64 ) -> Self {
65 Self(self.0.branded_type_impl(builder))
66 }
67
68 pub fn with_serde(self, mode: SerdeMode) -> Self {
70 Self(self.0.with_serde(mode))
71 }
72
73 pub fn with_serde_serialize(self) -> Self {
75 Self(self.0.with_serde_serialize())
76 }
77
78 pub fn with_serde_deserialize(self) -> Self {
80 Self(self.0.with_serde_deserialize())
81 }
82
83 pub fn export(&self, types: &TypeCollection) -> Result<String, Error> {
87 self.0.export(types)
88 }
89
90 pub fn export_to(&self, path: impl AsRef<Path>, types: &TypeCollection) -> Result<(), Error> {
96 self.0.export_to(path, types)
97 }
98}
99
100impl AsRef<Exporter> for Typescript {
101 fn as_ref(&self) -> &Exporter {
102 &self.0
103 }
104}
105
106impl AsMut<Exporter> for Typescript {
107 fn as_mut(&mut self) -> &mut Exporter {
108 &mut self.0
109 }
110}