2023-09-27 17:51:03 +02:00
|
|
|
use itertools::Itertools;
|
|
|
|
use crate::data::RPC;
|
|
|
|
|
2023-10-01 13:15:12 +02:00
|
|
|
pub fn ty_to_str(ty: &crate::data::Types) -> String {
|
2023-09-27 17:51:03 +02:00
|
|
|
use crate::data::Types;
|
2023-10-01 13:15:12 +02:00
|
|
|
match &ty {
|
|
|
|
Types::String => "std::string".into(),
|
|
|
|
Types::Bool => "bool".into(),
|
|
|
|
Types::F32 => "std::float_t".into(),
|
|
|
|
Types::F64 => "std::double_t".into(),
|
|
|
|
Types::I8 => "std::int8_t".into(),
|
|
|
|
Types::I16 => "std::int16_t".into(),
|
|
|
|
Types::I32 => "std::int32_t".into(),
|
|
|
|
Types::I64 => "std::int64_t".into(),
|
|
|
|
Types::U8 => "std::uint8_t".into(),
|
|
|
|
Types::U16 => "std::uint16_t".into(),
|
|
|
|
Types::U32 => "std::uint32_t".into(),
|
|
|
|
Types::U64 => "std::uint64_t".into(),
|
|
|
|
Types::Named(name) => name.into(),
|
|
|
|
Types::Optional(inner) => format!("std::optional<{}>", ty_to_str(inner)),
|
|
|
|
Types::Array(inner) => format!("std::vector<{}>", ty_to_str(inner))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn method_args(method: &crate::data::MethodTy) -> String {
|
|
|
|
method.args.iter()
|
|
|
|
.map(|arg| format!("{} &&{}", ty_to_str(&arg.ty), arg.name))
|
|
|
|
.chain(method.ret_stream.then(|| format!("std::shared_ptr<MRPCStream<{}>>&&", ty_to_str(method.ret.as_ref().unwrap()))))
|
|
|
|
.join(", ")
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn method_ret(method: &crate::data::MethodTy) -> String {
|
|
|
|
if method.ret_stream || method.ret.is_none() {
|
|
|
|
"void".into()
|
2023-09-27 17:51:03 +02:00
|
|
|
} else {
|
2023-10-01 13:15:12 +02:00
|
|
|
ty_to_str(method.ret.as_ref().unwrap())
|
2023-09-27 17:51:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-01 13:15:12 +02:00
|
|
|
pub fn call_args(method: &crate::data::MethodTy) -> String {
|
|
|
|
method.args.iter()
|
|
|
|
.map(|arg| format!("std::move({})", arg.name))
|
|
|
|
.chain(method.ret_stream.then_some(String::from("std::move(__stream)")))
|
|
|
|
.join(", ")
|
2023-09-27 17:51:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn gen(file_base_name: &std::path::PathBuf, rpc: &RPC) {
|
2023-10-01 13:15:12 +02:00
|
|
|
let header_name = file_base_name.with_extension("h");
|
|
|
|
let header_name = header_name.file_name().unwrap().to_str().unwrap();
|
|
|
|
let h = std::fs::File::create(file_base_name.with_extension("h")).unwrap();
|
|
|
|
let c = std::fs::File::create(file_base_name.with_extension("cpp")).unwrap();
|
|
|
|
crate::templates::cpp_server_h(h, rpc).unwrap();
|
|
|
|
crate::templates::cpp_server_cpp(c, header_name, rpc).unwrap();
|
2023-09-27 17:51:03 +02:00
|
|
|
}
|