doc_type/ts/types.rs
1use std::fmt::Display;
2
3#[derive(Debug, PartialEq, Eq)]
4pub enum FormatError {
5 TrimError,
6}
7
8#[derive(Debug)]
9pub struct FormattedType(pub String);
10
11impl Display for FormattedType {
12 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
13 write!(f, "{}", self.0)
14 }
15}
16
17/// Trait implementing trim functions that you can use on a
18/// [`FormattedType`].
19///
20/// You can use multiple functions on a single [`FormattedType`] like so:
21///
22/// ```
23/// use crate::generate_typedoc;
24/// use crate::TrimFunctions;
25/// use specta::Type;
26///
27/// #[derive(Type)]
28/// struct Foo {
29/// bar: String,
30/// baz: i32
31/// }
32/// let data = generate_typedoc::<Foo>().unwrap();
33/// let trimmed_a = data.trim_export().to_string();
34/// assert_eq!(trimmed_a, "type Foo = { bar: string, baz: number}");
35/// let trimmed_b = data.trim_export().replace_equals().remove_type_word().to_string();
36/// assert_eq!(trimmed_b, "Foo { bar: string, baz: number }")
37/// ```
38pub trait TrimFunctions
39where
40 Self: Display + From<String>,
41{
42 fn trim_export(&self) -> Self {
43 Self::from(self.to_string().replace("export ", ""))
44 }
45 fn remove_type_word(&self) -> Self {
46 Self::from(self.to_string().replace("type ", ""))
47 }
48 fn replace_equals(&self) -> Self {
49 Self::from(self.to_string().replace(" =", ""))
50 }
51}
52
53impl From<String> for FormattedType {
54 fn from(value: String) -> Self {
55 Self(value)
56 }
57}
58
59impl TrimFunctions for FormattedType {}