futucli/cmd/trade_ext/
write_output.rs1use anyhow::Result;
2use serde::Serialize;
3
4use crate::output::OutputFormat;
5
6#[derive(Serialize)]
7pub(crate) struct TradeWriteSuccess<'a> {
8 pub(crate) operation: &'a str,
9 pub(crate) order_id: u64,
10 #[serde(skip_serializing_if = "Option::is_none")]
11 pub(crate) order_id_ex: Option<&'a str>,
12 #[serde(skip_serializing_if = "Option::is_none")]
13 pub(crate) returned_order_id: Option<u64>,
14}
15
16pub(crate) fn render_trade_write_success(
17 format: OutputFormat,
18 success: TradeWriteSuccess<'_>,
19) -> Result<String> {
20 match format {
21 OutputFormat::Json => serde_json::to_string_pretty(&success).map_err(Into::into),
22 OutputFormat::Jsonl => serde_json::to_string(&success).map_err(Into::into),
23 OutputFormat::Table | OutputFormat::Markdown => Ok(match success.operation {
24 "place_order" => format!(
25 "✅ order placed, order_id={} order_id_ex={}",
26 success.order_id,
27 success.order_id_ex.unwrap_or("")
28 ),
29 "modify_order" => format!(
30 "✅ modify_order ok: order_id={} returned_order_id={}",
31 success.order_id,
32 success.returned_order_id.unwrap_or(success.order_id)
33 ),
34 "cancel_order" => format!("✅ cancel_order ok: order_id={}", success.order_id),
35 "reconfirm_order" => format!("✅ reconfirm_order ok: order_id={}", success.order_id),
36 other => format!("✅ {other} ok: order_id={}", success.order_id),
37 }),
38 }
39}
40
41pub(crate) fn emit_trade_write_success(
42 format: OutputFormat,
43 success: TradeWriteSuccess<'_>,
44) -> Result<()> {
45 println!("{}", render_trade_write_success(format, success)?);
46 Ok(())
47}