Skip to main content

specta_typescript/
jsdoc.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 JSDoc(Exporter);
12
13impl Default for JSDoc {
14    fn default() -> Self {
15        let mut exporter = Exporter::default();
16        exporter.jsdoc = true;
17        exporter.into()
18    }
19}
20
21impl From<JSDoc> for Exporter {
22    fn from(value: JSDoc) -> Self {
23        value.0
24    }
25}
26
27impl From<Exporter> for JSDoc {
28    fn from(mut value: Exporter) -> Self {
29        value.jsdoc = true;
30        Self(value)
31    }
32}
33
34impl JSDoc {
35    /// Construct a new JSDoc exporter with the default options configured.
36    pub fn new() -> Self {
37        Default::default()
38    }
39
40    /// Configure a header for the file.
41    ///
42    /// This is perfect for configuring lint ignore rules or other file-level comments.
43    pub fn header(self, header: impl Into<Cow<'static, str>>) -> Self {
44        Self(self.0.header(header))
45    }
46
47    /// Configure the BigInt handling behaviour
48    pub fn bigint(self, bigint: BigIntExportBehavior) -> Self {
49        Self(self.0.bigint(bigint))
50    }
51
52    /// Configure the layout of the generated file
53    pub fn layout(self, layout: Layout) -> Self {
54        Self(self.0.layout(layout))
55    }
56
57    /// Configure how `specta_typescript::branded!` types are rendered.
58    ///
59    /// See [`Exporter::branded_type_impl`] for details.
60    pub fn branded_type_impl(
61        self,
62        builder: impl for<'a> Fn(BrandedTypeExporter<'a>, &Branded) -> Result<Cow<'static, str>, Error>
63        + Send
64        + Sync
65        + 'static,
66    ) -> Self {
67        Self(self.0.branded_type_impl(builder))
68    }
69
70    /// Configure the exporter to use specta-serde with the specified mode
71    pub fn with_serde(self, mode: SerdeMode) -> Self {
72        Self(self.0.with_serde(mode))
73    }
74
75    /// Configure the exporter to export the types for `#[derive(serde::Serialize)]`
76    pub fn with_serde_serialize(self) -> Self {
77        Self(self.0.with_serde_serialize())
78    }
79
80    /// Configure the exporter to export the types for `#[derive(serde::Deserialize)]`
81    pub fn with_serde_deserialize(self) -> Self {
82        Self(self.0.with_serde_deserialize())
83    }
84
85    /// Export the files into a single string.
86    ///
87    /// Note: This returns an error if the format is `Format::Files`.
88    pub fn export(&self, types: &TypeCollection) -> Result<String, Error> {
89        self.0.export(types)
90    }
91
92    /// Export the types to a specific file/folder.
93    ///
94    /// When configured when `format` is `Format::Files`, you must provide a directory path.
95    /// Otherwise, you must provide the path of a single file.
96    ///
97    pub fn export_to(&self, path: impl AsRef<Path>, types: &TypeCollection) -> Result<(), Error> {
98        self.0.export_to(path, types)
99    }
100}
101
102impl AsRef<Exporter> for JSDoc {
103    fn as_ref(&self) -> &Exporter {
104        &self.0
105    }
106}
107
108impl AsMut<Exporter> for JSDoc {
109    fn as_mut(&mut self) -> &mut Exporter {
110        &mut self.0
111    }
112}