1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use syn::Type;

#[derive(Debug)]
pub struct TypescriptType {
    pub typescript_type: String,
    pub is_optional: bool,
}

impl TypescriptType {
    pub fn new(typescript_type: String, is_optional: bool) -> Self {
        Self {
            typescript_type,
            is_optional
        }
    }
}

impl From<String> for TypescriptType {
    fn from(typescript_type: String) -> TypescriptType {
        TypescriptType {
            typescript_type,
            is_optional: false,
        }
    }
}

fn convert_generic(generic_type: &syn::GenericArgument) -> TypescriptType {
    match generic_type {
        syn::GenericArgument::Type(rust_type) => convert_primitive(rust_type),
        _ => "unknown".to_string().into(),
    }
}

/// Function for converting basic rust primitive types to typescript types and interfaces
pub fn convert_primitive(rust_primitive: &Type) -> TypescriptType {
    match rust_primitive {
        Type::Reference(path) => convert_primitive(&path.elem),
        Type::Path(path) => {
            let segment = path.path.segments.last().unwrap();
            let tokens = &segment.ident;
            let arguments = &segment.arguments;
            let parsed_type = tokens.to_string();
            // TODO: Add in cases for chrono dates as well
            match parsed_type.as_str() {
                "u8" => "number".to_string().into(),
                "u16" => "number".to_string().into(),
                "i64" => "number".to_string().into(),
                "u64" => "number".to_string().into(),
                "u128" => "number".to_string().into(),
                "i8" => "number".to_string().into(),
                "i16" => "number".to_string().into(),
                "i32" => "number".to_string().into(),
                "u32" => "number".to_string().into(),
                "i128" => "number".to_string().into(),
                "isize" => "number".to_string().into(),
                "usize" => "number".to_string().into(),
                "f32" => "number".to_string().into(),
                "f64" => "number".to_string().into(),
                "bool" => "boolean".to_string().into(),
                "char" => "string".to_string().into(),
                "str" => "string".to_string().into(),
                "String" => "string".to_string().into(),
                "NaiveDateTime" => "Date".to_string().into(),
                "DateTime" => "Date".to_string().into(),
                "Path" => TypescriptType {
                    is_optional: false,
                    typescript_type: match arguments {
                        syn::PathArguments::Parenthesized(parenthesized_argument) => {
                            format!("{:?}", parenthesized_argument)
                        }
                        syn::PathArguments::AngleBracketed(anglebracketed_argument) => {
                            convert_generic(anglebracketed_argument.args.first().unwrap()).typescript_type
                        }
                        _ => "unknown".to_string(),
                    },
                },
                "Json" => TypescriptType {
                    is_optional: false,
                    typescript_type: match arguments {
                        syn::PathArguments::Parenthesized(parenthesized_argument) => {
                            format!("{:?}", parenthesized_argument)
                        }
                        syn::PathArguments::AngleBracketed(anglebracketed_argument) => {
                            convert_generic(anglebracketed_argument.args.first().unwrap()).typescript_type
                        }
                        _ => "unknown".to_string(),
                    },
                },
                "Option" => TypescriptType {
                    is_optional: true,
                    typescript_type: match arguments {
                        syn::PathArguments::Parenthesized(parenthesized_argument) => {
                            format!("{:?}", parenthesized_argument)
                        }
                        syn::PathArguments::AngleBracketed(anglebracketed_argument) => {
                            convert_generic(anglebracketed_argument.args.first().unwrap()).typescript_type
                        }
                        _ => "unknown".to_string(),
                    },
                },
                "Vec" => match arguments {
                    syn::PathArguments::Parenthesized(parenthesized_argument) => {
                        format!("{:?}", parenthesized_argument)
                    }
                    syn::PathArguments::AngleBracketed(anglebracketed_argument) => format!(
                        "Array<{}>",
                        match convert_generic(anglebracketed_argument.args.first().unwrap()) {
                            TypescriptType {
                                is_optional: true,
                                typescript_type,
                            } => format!("{} | undefined", typescript_type),
                            TypescriptType {
                                is_optional: false,
                                typescript_type,
                            } => typescript_type,
                        }
                    ),
                    _ => "unknown".to_string(),
                }
                .into(),
                "HashMap" => match arguments {
                    syn::PathArguments::Parenthesized(parenthesized_argument) => {
                        format!("{:?}", parenthesized_argument)
                    }
                    syn::PathArguments::AngleBracketed(anglebracketed_argument) => format!(
                        "Record<{}>",
                        anglebracketed_argument
                            .args
                            .iter()
                            .map(|arg| match convert_generic(arg) {
                                TypescriptType {
                                    is_optional: true,
                                    typescript_type,
                                } => format!("{} | undefined", typescript_type),
                                TypescriptType {
                                    is_optional: false,
                                    typescript_type,
                                } => typescript_type,
                            })
                            .collect::<Vec<String>>()
                            .join(", ")
                    ),
                    _ => "any".to_string(),
                }
                .into(),
                _ => parsed_type.to_string().into(),
            }
        }
        _ => "any".to_string().into(),
    }
}