detee-cli/src/lib.rs
2025-03-16 22:28:19 +02:00

51 lines
1.1 KiB
Rust

pub mod config;
pub mod constants;
pub mod name_generator;
pub mod operators;
pub mod packagers;
pub mod sgx;
pub mod snp;
pub mod utils;
use serde::Serialize;
pub trait HumanOutput {
fn human_cli_print(&self);
}
#[derive(Serialize)]
pub struct SimpleOutput {
message: String,
}
impl From<&str> for SimpleOutput {
fn from(message: &str) -> Self {
Self { message: message.to_string() }
}
}
impl HumanOutput for SimpleOutput {
fn human_cli_print(&self) {
println!("{}", self.message);
}
}
pub fn cli_print<T: HumanOutput + serde::Serialize>(output: Result<T, Box<dyn std::error::Error>>) {
let output = match output {
Ok(output) => output,
Err(e) => {
println!("Error: {e}");
std::process::exit(1);
}
};
match std::env::var("FORMAT") {
Ok(format) if format.as_str() == "JSON" => {
println!("{}", serde_json::to_string_pretty(&output).unwrap())
}
Ok(format) if format.as_str() == "YAML" => {
print!("{}", serde_yaml::to_string(&output).unwrap())
}
_ => output.human_cli_print(),
};
}