use serde::Serialize;
use ossctl_core::SCHEMA_VERSION;
use crate::error::CliError;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum OutputFormat {
#[default]
Text,
Json,
}
impl OutputFormat {
pub fn from_json_flag(json: bool) -> Self {
if json {
Self::Json
} else {
Self::Text
}
}
}
#[derive(Serialize)]
struct SuccessEnvelope<'a, T: Serialize> {
schema_version: u32,
data: &'a T,
warnings: &'a [String],
}
pub fn emit_json<T: Serialize>(body: &T, warnings: &[String]) -> Result<(), CliError> {
let envelope = SuccessEnvelope {
schema_version: SCHEMA_VERSION,
data: body,
warnings,
};
let mut s = serde_json::to_string_pretty(&envelope)
.map_err(|e| CliError::system("internal_serialize", e.to_string()))?;
s.push('\n');
print!("{s}");
Ok(())
}