specta_typescript/
typescript.rs1use std::{borrow::Cow, path::Path};
2
3use specta::{Format, Types};
4
5use crate::{Branded, BrandedTypeExporter, Error, Exporter, Layout};
6
7#[derive(Debug, Clone)]
9#[non_exhaustive]
10pub struct Typescript(Exporter);
11
12impl Default for Typescript {
13 fn default() -> Self {
14 Exporter::default().into()
15 }
16}
17
18impl From<Typescript> for Exporter {
19 fn from(value: Typescript) -> Self {
20 value.0
21 }
22}
23
24impl From<Exporter> for Typescript {
25 fn from(mut value: Exporter) -> Self {
26 value.jsdoc = false;
27 Self(value)
28 }
29}
30
31impl Typescript {
32 pub fn new() -> Self {
34 Default::default()
35 }
36
37 pub fn header(self, header: impl Into<Cow<'static, str>>) -> Self {
41 Self(self.0.header(header))
42 }
43
44 pub fn layout(self, layout: Layout) -> Self {
46 Self(self.0.layout(layout))
47 }
48
49 pub fn branded_type_impl(
53 self,
54 builder: impl for<'a> Fn(BrandedTypeExporter<'a>, &Branded) -> Result<Cow<'static, str>, Error>
55 + Send
56 + Sync
57 + 'static,
58 ) -> Self {
59 Self(self.0.branded_type_impl(builder))
60 }
61
62 pub fn export(&self, types: &Types, format: impl Format) -> Result<String, Error> {
66 self.0.export(types, format)
67 }
68
69 pub fn export_to(
75 &self,
76 path: impl AsRef<Path>,
77 types: &Types,
78 format: impl Format,
79 ) -> Result<(), Error> {
80 self.0.export_to(path, types, format)
81 }
82}
83
84impl AsRef<Exporter> for Typescript {
85 fn as_ref(&self) -> &Exporter {
86 &self.0
87 }
88}
89
90impl AsMut<Exporter> for Typescript {
91 fn as_mut(&mut self) -> &mut Exporter {
92 &mut self.0
93 }
94}