use std::collections::HashMap;
use std::path::Path;
use miette::{GraphicalReportHandler, GraphicalTheme};
use serde::de::DeserializeOwned;
use crate::MartinError;
use crate::config::file::parse_config;
use crate::logging::LogFormat;
const SNAPSHOT_WIDTH: usize = 80;
pub(crate) fn parse_yaml<T: DeserializeOwned>(yaml: &str) -> T {
let opts = serde_saphyr::options! {
with_snippet: false,
};
serde_saphyr::from_str_with_options::<T>(yaml, opts)
.unwrap_or_else(|e| panic!("expected `{yaml}` to parse, but got error:\n{e}"))
}
pub(crate) fn render_failure(yaml: &str) -> String {
let env: HashMap<String, String> = HashMap::new();
let err = parse_config(yaml, &env, Path::new("config.yaml"))
.err()
.unwrap_or_else(|| panic!("expected configuration to fail to parse:\n{yaml}"));
let report = err
.to_miette_report()
.unwrap_or_else(|| panic!("expected a miette-renderable error for:\n{yaml}"));
let mut buf = String::new();
GraphicalReportHandler::new_themed(GraphicalTheme::unicode_nocolor())
.with_width(SNAPSHOT_WIDTH)
.with_links(false)
.render_report(&mut buf, report.as_ref())
.expect("rendering into a String is infallible");
buf
}
pub(crate) fn render_failure_json(yaml: &str) -> String {
let env: HashMap<String, String> = HashMap::new();
let err = parse_config(yaml, &env, Path::new("config.yaml"))
.err()
.unwrap_or_else(|| panic!("expected configuration to fail to parse:\n{yaml}"));
MartinError::ConfigFileError(err).render_diagnostic_with(LogFormat::Json)
}