mrpc/src/generators/ts_c.rs

38 lines
1.3 KiB
Rust
Raw Normal View History

2023-09-27 17:51:03 +02:00
use itertools::Itertools;
use crate::data::RPC;
pub fn ty_to_str(ty: &crate::data::Types) -> String {
2023-09-27 17:51:03 +02:00
use crate::data::Types;
match &ty {
Types::String => "string".into(),
Types::Bool => "boolean".into(),
2023-09-27 17:51:03 +02:00
Types::F32 | Types::F64
|Types::I8 | Types::I16 | Types::I32 | Types::I64
|Types::U8 | Types::U16 | Types::U32 | Types::U64 => "number".into(),
Types::Named(name) => name.into(),
Types::Optional(inner) => format!("({}|null)", ty_to_str(inner)),
Types::Array(inner) => format!("{}[]", ty_to_str(inner))
2023-09-27 17:51:03 +02:00
}
}
pub fn method_args(method: &crate::data::MethodTy) -> String {
method.args.iter()
.map(|arg| format!("{}: {}", arg.name, ty_to_str(&arg.ty)))
.chain(method.ret_stream.then(|| format!("__cbk: (v: {}|null) => void", ty_to_str(method.ret.as_ref().unwrap()))))
.join(", ")
2023-09-27 17:51:03 +02:00
}
pub fn method_ret(method: &crate::data::MethodTy) -> String {
if method.ret_stream || method.ret.is_none() {
String::new()
} else {
format!(": Promise<{}>", ty_to_str(method.ret.as_ref().unwrap()))
2023-09-27 17:51:03 +02:00
}
}
pub fn gen(file_base_name: &std::path::PathBuf, rpc: &RPC) {
let f = std::fs::File::create(file_base_name.with_extension("ts")).unwrap();
crate::templates::typescript_client_ts(f, rpc).unwrap();
2023-09-27 17:51:03 +02:00
}