use miette::{IntoDiagnostic, Result, WrapErr};
use std::path::Path;
mod policies;
pub use policies::*;
mod links;
pub(crate) use links::*;
mod request;
pub use request::*;
mod schema;
pub use schema::*;
mod entities;
pub(crate) use entities::*;
pub(crate) fn read_from_file_or_stdin(
filename: Option<&impl AsRef<Path>>,
context: &str,
) -> Result<String> {
let mut src_str = String::new();
match filename {
Some(path) => {
src_str = std::fs::read_to_string(path)
.into_diagnostic()
.wrap_err_with(|| {
format!("failed to open {context} file {}", path.as_ref().display())
})?;
}
None => {
std::io::Read::read_to_string(&mut std::io::stdin(), &mut src_str)
.into_diagnostic()
.wrap_err_with(|| format!("failed to read {context} from stdin"))?;
}
};
Ok(src_str)
}
fn read_from_file(filename: impl AsRef<Path>, context: &str) -> Result<String> {
read_from_file_or_stdin(Some(&filename), context)
}
#[cfg(test)]
pub(crate) mod test_utils {
pub const TEMPFILE_FILTER: (&str, &str) = (r"/tmp/\.tmp[A-Za-z0-9]+", "<TEMPFILE>");
pub fn render_err(err: &miette::Report) -> String {
let mut buf = String::new();
miette::GraphicalReportHandler::new_themed(miette::GraphicalTheme::unicode_nocolor())
.render_report(&mut buf, err.as_ref())
.unwrap();
buf
}
}