MCPcopy Create free account
hub / github.com/Wulf/tsync / convert_type

Function convert_type

src/typescript.rs:174–214  ·  view source on GitHub ↗
(ty: &syn::Type)

Source from the content-addressed store, hash-verified

172}
173
174pub fn convert_type(ty: &syn::Type) -> TsType {
175 match ty {
176 syn::Type::Reference(p) => convert_type(&p.elem),
177 syn::Type::Path(p) => {
178 let segment = p.path.segments.last().unwrap();
179 let identifier = segment.ident.to_string();
180
181 if let Ok(ts_type) = try_match_ident_str(&identifier) {
182 ts_type.into()
183 } else if let Ok(ts_type) = try_match_with_args(&identifier, &segment.arguments) {
184 ts_type
185 } else if let Ok(ts_type) = extract_custom_type(segment) {
186 ts_type
187 } else {
188 "unknown".to_owned().into()
189 }
190 }
191 syn::Type::Tuple(t) => {
192 let types = t
193 .elems
194 .iter()
195 .map(convert_type)
196 .map(|ty| {
197 if ty.is_optional {
198 format!("{} | undefined", ty.ts_type)
199 } else {
200 ty.ts_type
201 }
202 })
203 .collect::<Vec<String>>()
204 .join(", ");
205
206 TsType {
207 ts_type: format!("[{types}]"),
208 is_optional: false,
209 }
210 }
211
212 _ => "unknown".to_owned().into(),
213 }
214}

Callers 6

convert_genericFunction · 0.85
convert_to_tsMethod · 0.85
process_fieldsFunction · 0.85
process_tuple_fieldsFunction · 0.85
get_intersectionsFunction · 0.85

Calls 3

try_match_ident_strFunction · 0.85
try_match_with_argsFunction · 0.85
extract_custom_typeFunction · 0.85

Tested by

no test coverage detected