Skip to main content

specta_typescript/
typescript.rs

1use std::{borrow::Cow, path::Path};
2
3use specta::TypeCollection;
4use specta_serde::SerdeMode;
5
6use crate::{BigIntExportBehavior, Branded, BrandedTypeExporter, Error, Exporter, Layout};
7
8/// JSDoc language exporter.
9#[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    /// Construct a new JSDoc exporter with the default options configured.
34    pub fn new() -> Self {
35        Default::default()
36    }
37
38    /// Configure a header for the file.
39    ///
40    /// This is perfect for configuring lint ignore rules or other file-level comments.
41    pub fn header(self, header: impl Into<Cow<'static, str>>) -> Self {
42        Self(self.0.header(header))
43    }
44
45    /// Configure the BigInt handling behaviour
46    pub fn bigint(self, bigint: BigIntExportBehavior) -> Self {
47        Self(self.0.bigint(bigint))
48    }
49
50    /// Configure the layout of the generated file
51    pub fn layout(self, layout: Layout) -> Self {
52        Self(self.0.layout(layout))
53    }
54
55    /// Configure how `specta_typescript::branded!` types are rendered.
56    ///
57    /// See [`Exporter::branded_type_impl`] for details.
58    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    /// Configure the exporter to use specta-serde with the specified mode
69    pub fn with_serde(self, mode: SerdeMode) -> Self {
70        Self(self.0.with_serde(mode))
71    }
72
73    /// Configure the exporter to export the types for `#[derive(serde::Serialize)]`
74    pub fn with_serde_serialize(self) -> Self {
75        Self(self.0.with_serde_serialize())
76    }
77
78    /// Configure the exporter to export the types for `#[derive(serde::Deserialize)]`
79    pub fn with_serde_deserialize(self) -> Self {
80        Self(self.0.with_serde_deserialize())
81    }
82
83    /// Export the files into a single string.
84    ///
85    /// Note: This returns an error if the format is `Format::Files`.
86    pub fn export(&self, types: &TypeCollection) -> Result<String, Error> {
87        self.0.export(types)
88    }
89
90    /// Export the types to a specific file/folder.
91    ///
92    /// When configured when `format` is `Format::Files`, you must provide a directory path.
93    /// Otherwise, you must provide the path of a single file.
94    ///
95    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}