use serde::Serialize;
use serde_json::{Value, json};
use crate::error::{ErrorPayload, SillokError};
#[derive(Debug, Serialize)]
pub struct SuccessResponse {
pub ok: bool,
pub command: &'static str,
pub ids: Value,
pub data: Value,
pub warnings: Vec<String>,
}
#[derive(Debug, Serialize)]
pub struct FailureResponse {
pub ok: bool,
pub command: &'static str,
pub error: ErrorPayload,
}
#[derive(Debug)]
pub struct CommandOutcome {
pub command: &'static str,
pub ids: Value,
pub data: Value,
pub warnings: Vec<String>,
pub human: Option<String>,
}
impl CommandOutcome {
pub fn new(command: &'static str, data: Value) -> Self {
Self {
command,
ids: json!({}),
data,
warnings: Vec::new(),
human: None,
}
}
pub fn with_ids(mut self, ids: Value) -> Self {
self.ids = ids;
self
}
pub fn with_warnings(mut self, warnings: Vec<String>) -> Self {
self.warnings = warnings;
self
}
pub fn with_human(mut self, human: String) -> Self {
self.human = Some(human);
self
}
pub fn success_response(self) -> SuccessResponse {
SuccessResponse {
ok: true,
command: self.command,
ids: self.ids,
data: self.data,
warnings: self.warnings,
}
}
}
pub fn print_success(outcome: CommandOutcome, human: bool) -> Result<(), SillokError> {
if human {
match outcome.human {
Some(value) => {
println!("{value}");
Ok(())
}
None => {
println!("{}", serde_json::to_string_pretty(&outcome.data)?);
Ok(())
}
}
} else {
let response = outcome.success_response();
println!("{}", serde_json::to_string(&response)?);
Ok(())
}
}
pub fn print_failure(command: &'static str, error: &SillokError) -> Result<(), SillokError> {
let response = FailureResponse {
ok: false,
command,
error: error.payload(),
};
println!("{}", serde_json::to_string(&response)?);
Ok(())
}