use std::{fs::File, io::prelude::*};
use anyhow::Result;
use clap::ValueEnum;
use serde::{Deserialize, Serialize};
use crate::analysis;
pub fn tabled_vec_to_string(v: &[String]) -> String {
v.join(", ")
}
#[derive(Clone, Copy, Debug, ValueEnum, Serialize, Deserialize)]
pub enum Format {
Json,
Text,
}
impl Default for Format {
fn default() -> Self {
Self::Text
}
}
pub(crate) async fn output(results: &analysis::Results, format: &Format, filename: &Option<String>) -> Result<()> {
let output = match format {
Format::Json => serde_json::to_string(&results)?,
Format::Text => results.to_stdout_table()?,
};
match filename {
Some(filename) => {
let mut file = File::create(filename)?;
file.write_all(output.as_bytes())?;
}
None => {
println!("{output}");
}
}
Ok(())
}