specta_typescript/
jsdoc.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 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 pub fn new() -> Self {
37 Default::default()
38 }
39
40 pub fn header(self, header: impl Into<Cow<'static, str>>) -> Self {
44 Self(self.0.header(header))
45 }
46
47 pub fn bigint(self, bigint: BigIntExportBehavior) -> Self {
49 Self(self.0.bigint(bigint))
50 }
51
52 pub fn layout(self, layout: Layout) -> Self {
54 Self(self.0.layout(layout))
55 }
56
57 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 pub fn with_serde(self, mode: SerdeMode) -> Self {
72 Self(self.0.with_serde(mode))
73 }
74
75 pub fn with_serde_serialize(self) -> Self {
77 Self(self.0.with_serde_serialize())
78 }
79
80 pub fn with_serde_deserialize(self) -> Self {
82 Self(self.0.with_serde_deserialize())
83 }
84
85 pub fn export(&self, types: &TypeCollection) -> Result<String, Error> {
89 self.0.export(types)
90 }
91
92 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}