specta_typescript/
typescript.rs1use std::{borrow::Cow, path::Path};
2
3use specta::ResolvedTypes;
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: &ResolvedTypes) -> Result<String, Error> {
66 self.0.export(types)
67 }
68
69 pub fn export_to(&self, path: impl AsRef<Path>, types: &ResolvedTypes) -> Result<(), Error> {
75 self.0.export_to(path, types)
76 }
77}
78
79impl AsRef<Exporter> for Typescript {
80 fn as_ref(&self) -> &Exporter {
81 &self.0
82 }
83}
84
85impl AsMut<Exporter> for Typescript {
86 fn as_mut(&mut self) -> &mut Exporter {
87 &mut self.0
88 }
89}