2023-09-27 17:51:03 +02:00
|
|
|
use itertools::Itertools;
|
|
|
|
use crate::data::RPC;
|
|
|
|
|
2023-10-02 15:41:00 +02:00
|
|
|
pub const JSON_INNER_IMPLS: &[(&str, &str)] = &[
|
|
|
|
("std::string", "String"),
|
|
|
|
("std::int8_t", "Int"),
|
|
|
|
("std::int16_t", "Int"),
|
|
|
|
("std::int32_t", "Int"),
|
|
|
|
("std::int64_t", "Int64"),
|
|
|
|
("std::uint8_t", "Uint"),
|
|
|
|
("std::uint16_t", "Uint"),
|
|
|
|
("std::uint32_t", "Uint"),
|
|
|
|
("std::uint64_t", "Uint64"),
|
|
|
|
("bool", "Bool"),
|
|
|
|
("std::float_t", "Double"),
|
|
|
|
("std::double_t", "Double")
|
|
|
|
];
|
|
|
|
|
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))
|
2023-10-02 15:41:00 +02:00
|
|
|
.chain(method.ret_stream.then(|| format!("MRPCStream<{}>&&", ty_to_str(method.ret.as_ref().unwrap()))))
|
2023-10-01 13:15:12 +02:00
|
|
|
.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
|
|
|
}
|
|
|
|
|
2023-10-02 09:50:27 +02:00
|
|
|
pub fn json_write(ty: &crate::data::FieldTy) -> String {
|
|
|
|
use crate::data::Types;
|
|
|
|
match &ty.ty {
|
|
|
|
Types::String => format!("__w.String({});", ty.name),
|
|
|
|
Types::Bool => format!("__w.Bool({});", ty.name),
|
|
|
|
Types::F32 | Types::F64 => format!("__w.Double({});", ty.name),
|
|
|
|
Types::I8 | Types::I16 | Types::I32 | Types::I64 => format!("__w.Int64({});", ty.name),
|
|
|
|
Types::U8 | Types::U16 | Types::U32 | Types::U64 => format!("__w.Uint64({});", ty.name),
|
|
|
|
Types::Named(_) => format!("{} >> __w;", ty.name),
|
|
|
|
Types::Optional(inner) => {
|
|
|
|
let inner = crate::data::FieldTy { name: format!("({}.value())", ty.name), ty: (**inner).clone() };
|
|
|
|
let inner = json_write(&inner);
|
|
|
|
format!(
|
|
|
|
"if ({}.has_value()) {{
|
|
|
|
{}
|
|
|
|
}} else __w.Null();", ty.name, inner)
|
|
|
|
},
|
|
|
|
Types::Array(inner) => {
|
|
|
|
let inner_var_name = format!("__{}__entry", ty.name);
|
|
|
|
let inner = crate::data::FieldTy { name: inner_var_name.clone(), ty: (**inner).clone() };
|
|
|
|
let inner = json_write(&inner);
|
|
|
|
format!(
|
|
|
|
"__w.StartArray();
|
|
|
|
for (const auto &{} : {}) {{
|
|
|
|
{}
|
|
|
|
}}
|
|
|
|
__w.EndArray();", inner_var_name, ty.name, inner)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-02 15:41:00 +02:00
|
|
|
pub fn streams_required(rpc: &RPC) -> Vec<String> {
|
|
|
|
let mut streams = std::collections::HashSet::new();
|
|
|
|
for s in &rpc.services {
|
|
|
|
for m in &s.methods {
|
|
|
|
if m.ret_stream {
|
|
|
|
streams.insert(ty_to_str(m.ret.as_ref().unwrap()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
streams.into_iter().collect()
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|