futucli/cmd/proto_json/
transport.rs1use anyhow::{Result, anyhow, bail};
5use prost::Message;
6use serde::Serialize;
7
8use crate::common::connect_gateway;
9use crate::output::OutputFormat;
10
11pub(super) fn print_proto_response<T>(format: OutputFormat, response: &T) -> Result<()>
12where
13 T: Serialize,
14{
15 match format {
16 OutputFormat::Table | OutputFormat::Json | OutputFormat::Markdown => {
17 println!("{}", serde_json::to_string_pretty(response)?);
18 }
19 OutputFormat::Jsonl => {
20 println!("{}", serde_json::to_string(response)?);
21 }
22 }
23 Ok(())
24}
25
26pub(super) fn ensure_success(
27 label: &str,
28 ret_type: i32,
29 ret_msg: Option<&str>,
30 err_code: Option<i32>,
31) -> Result<()> {
32 if ret_type == 0 {
33 return Ok(());
34 }
35 bail!("{label} ret_type={ret_type} msg={ret_msg:?} err_code={err_code:?}")
36}
37
38pub(super) async fn send_proto<Req, Resp>(
39 gateway: &str,
40 client_id: &str,
41 proto_id: u32,
42 request: Req,
43) -> Result<Resp>
44where
45 Req: Message,
46 Resp: Message + Default,
47{
48 let (client, _rx) = connect_gateway(gateway, client_id).await?;
49 let frame = client.request(proto_id, request.encode_to_vec()).await?;
50 Resp::decode(frame.body.as_ref()).map_err(|err| anyhow!("decode response: {err}"))
51}