Files
foxhunt/bin/fxt/src/output.rs
jgrusewski 724bd64429 feat: fxt 2.0 skeleton — new command tree with 15 command groups
Clean rewrite of the fxt CLI with unified gRPC client, JSON/human
output abstraction, and stub implementations for all 15 command groups:
auth, trade, train, tune, model, agent, backtest, broker, data,
service, cluster, risk, config, watch (TUI), mcp.

Deleted old fat-client commands, client modules, types, prelude, and
tests (-12,769 net lines). Fixed pre-existing clippy issues in auth
module (indexing_slicing in encryption.rs, manual_let_else in
token_manager.rs, str_to_string in build.rs).

60 tests passing (53 lib + 7 binary), 0 clippy warnings.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-03 21:35:18 +01:00

46 lines
1.4 KiB
Rust

//! Output format abstraction for JSON and human-readable rendering.
//!
//! Every command result type implements [`HumanReadable`] so the CLI can
//! switch between `--json` (machine-readable) and the default tabular output
//! with a single [`render`] call.
use serde::Serialize;
use std::io::Write;
/// Output format selected by the global `--json` flag.
#[derive(Debug, Clone, Copy)]
pub enum OutputFormat {
/// Default: pretty-printed tables/text for humans.
Human,
/// Structured JSON (for CI pipelines, LLM tool-use, jq).
Json,
}
/// Render any serializable result in the chosen format.
///
/// # Errors
///
/// Returns an error if JSON serialization or stdout writing fails.
pub fn render<T: Serialize + HumanReadable>(result: &T, format: OutputFormat) -> anyhow::Result<()> {
match format {
OutputFormat::Json => {
let json = serde_json::to_string_pretty(result)?;
let mut stdout = std::io::stdout().lock();
writeln!(stdout, "{json}")?;
}
OutputFormat::Human => {
result.print_human();
}
}
Ok(())
}
/// Trait for human-readable terminal rendering.
///
/// Implementors print directly to stdout using `println!`, `colored`, or
/// `tabled` — whatever looks best for the data type.
pub trait HumanReadable {
/// Print a human-friendly representation to stdout.
fn print_human(&self);
}