use kernel::AxError;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum Form {
Human,
Json,
}
impl Form {
pub(super) fn of(args: &[String]) -> Self {
if args.iter().any(|a| a == "--json") {
Form::Json
} else {
Form::Human
}
}
}
pub(super) fn written(err: &AxError, form: Form) -> String {
match form {
Form::Human => human(err),
Form::Json => match serde_json::to_string(err) {
Ok(line) => format!("{line}\n"),
Err(fail) => format!(
"{}could not write this refusal as json: {fail}\n",
human(err)
),
},
}
}
fn human(err: &AxError) -> String {
let recovery = format!("{err}\nrecovery: {}\n", err.recovery());
match err.nearby() {
[] => recovery,
nearby => format!("{recovery}nearby: {}\n", nearby.join(", ")),
}
}