use std::io::{self, Write};
use crate::error::Error;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Format {
Human,
Json,
}
#[derive(Debug, Clone, Copy)]
pub struct Output {
pub format: Format,
}
impl Output {
pub fn new(json: bool) -> Self {
Self {
format: if json { Format::Json } else { Format::Human },
}
}
pub fn is_json(&self) -> bool {
self.format == Format::Json
}
pub fn json(&self, value: &serde_json::Value) {
if self.is_json()
&& let Ok(s) = serde_json::to_string_pretty(value)
{
println!("{s}");
}
}
pub fn line(&self, text: impl AsRef<str>) {
if !self.is_json() {
println!("{}", text.as_ref());
}
}
pub fn warn(&self, text: impl AsRef<str>) {
let _ = writeln!(io::stderr(), "{}", text.as_ref());
}
pub fn emit_error(&self, err: &Error) -> i32 {
match self.format {
Format::Json => {
if let Ok(s) = serde_json::to_string_pretty(&err.to_json()) {
let _ = writeln!(io::stderr(), "{s}");
}
}
Format::Human => {
let _ = writeln!(
io::stderr(),
"error ({}): {}",
err.kind.as_str(),
err.message
);
}
}
err.exit_code()
}
}