|
| 1 | +use std::collections::HashMap; |
| 2 | +use std::fs::File; |
| 3 | +use std::io::Read; |
| 4 | + |
| 5 | +use backend::{self}; |
| 6 | +use backend::ast::{BindgenAttrs, Export, Function}; |
| 7 | +use proc_macro2::{self}; |
| 8 | +use serde_json::{self}; |
| 9 | +use syn::{self}; |
| 10 | + |
| 11 | +use api_extractor; |
| 12 | +use definitions::*; |
| 13 | + |
| 14 | +pub fn ts_to_program(file_name: &str) -> backend::ast::Program { |
| 15 | + api_extractor::run(); |
| 16 | + |
| 17 | + let ts_package = parse_json(file_name); |
| 18 | + |
| 19 | + let mut program = backend::ast::Program::default(); |
| 20 | + |
| 21 | + for (name, export) in ts_package.exports { |
| 22 | + match export { |
| 23 | + TsExport::TsClass { members } => { |
| 24 | + for (member_name, member) in members { |
| 25 | + match member { |
| 26 | + TsClassMembers::TsProperty { .. } => {} |
| 27 | + TsClassMembers::TsConstructor { .. } => {} |
| 28 | + TsClassMembers::TsMethod { parameters, return_value, .. } => { |
| 29 | + let function = build_function(member_name, parameters, return_value); |
| 30 | + |
| 31 | + program.exports.push(Export { |
| 32 | + class: Some(syn::Ident::new(&name, proc_macro2::Span::call_site())), |
| 33 | + method: true, |
| 34 | + mutable: false, |
| 35 | + constructor: None, |
| 36 | + function, |
| 37 | + }); |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + TsExport::TsFunction { parameters, return_value } => { |
| 44 | + let function = build_function(name, parameters, return_value); |
| 45 | + |
| 46 | + program.exports.push(Export { |
| 47 | + class: None, |
| 48 | + method: false, |
| 49 | + mutable: false, |
| 50 | + constructor: None, |
| 51 | + function, |
| 52 | + }); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + program |
| 58 | +} |
| 59 | + |
| 60 | +fn parse_json(file_name: &str) -> TsPackage { |
| 61 | + let mut file = File::open(file_name).unwrap(); |
| 62 | + let mut data = String::new(); |
| 63 | + file.read_to_string(&mut data).unwrap(); |
| 64 | + |
| 65 | + serde_json::from_str(&data).unwrap() |
| 66 | +} |
| 67 | + |
| 68 | +fn build_function(name: String, parameters: HashMap<String, TsMethodProperty>, return_value: TsReturnValue) -> Function { |
| 69 | + let arguments = parameters.iter().map( |(_name, property)| { |
| 70 | + let property_type = rust_type(&property.property_type); |
| 71 | + |
| 72 | + let mut segments = syn::punctuated::Punctuated::new(); |
| 73 | + segments.push(syn::PathSegment { |
| 74 | + ident: syn::Ident::new(property_type, proc_macro2::Span::call_site()), |
| 75 | + arguments: syn::PathArguments::None, |
| 76 | + }); |
| 77 | + |
| 78 | + syn::Type::Path(syn::TypePath { |
| 79 | + qself: None, |
| 80 | + path: syn::Path { |
| 81 | + leading_colon: None, |
| 82 | + segments, |
| 83 | + } |
| 84 | + }) |
| 85 | + }).collect::<Vec<_>>(); |
| 86 | + |
| 87 | + let ret_property_type = rust_type(&return_value.property_type); |
| 88 | + |
| 89 | + let mut ret_segments = syn::punctuated::Punctuated::new(); |
| 90 | + ret_segments.push(syn::PathSegment { |
| 91 | + ident: syn::Ident::new(ret_property_type, proc_macro2::Span::call_site()), |
| 92 | + arguments: syn::PathArguments::None, |
| 93 | + }); |
| 94 | + |
| 95 | + let ret = syn::Type::Path(syn::TypePath { |
| 96 | + qself: None, |
| 97 | + path: syn::Path { |
| 98 | + leading_colon: None, |
| 99 | + segments: ret_segments, |
| 100 | + } |
| 101 | + }); |
| 102 | + |
| 103 | + let rust_decl = Box::new(syn::FnDecl { |
| 104 | + fn_token: Default::default(), |
| 105 | + generics: Default::default(), |
| 106 | + paren_token: Default::default(), |
| 107 | + //TODO investigate if inputs should be taken from arguments |
| 108 | + inputs: Default::default(), |
| 109 | + variadic: None, |
| 110 | + output: syn::ReturnType::Type(Default::default(), Box::new(ret.clone())), |
| 111 | + }); |
| 112 | + |
| 113 | + Function { |
| 114 | + name: syn::Ident::new(&name, proc_macro2::Span::call_site()), |
| 115 | + arguments, |
| 116 | + ret: Some(ret), |
| 117 | + opts: BindgenAttrs::default(), |
| 118 | + rust_attrs: Vec::new(), |
| 119 | + rust_decl, |
| 120 | + rust_vis: syn::Visibility::Public(syn::VisPublic { |
| 121 | + pub_token: Default::default(), |
| 122 | + }), |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +// TODO: implement this properly |
| 127 | +fn rust_type(js_type: &str) -> &'static str { |
| 128 | + match js_type { |
| 129 | + "string" => "String", |
| 130 | + "number" => "String", |
| 131 | + "boolean" => "bool", |
| 132 | + "symbol" => "String", |
| 133 | + "object" => "String", |
| 134 | + "function" => "String", |
| 135 | + "void" => "String", |
| 136 | + _ => "String", |
| 137 | + } |
| 138 | +} |
0 commit comments