use crate::dsl::error::{Diagnostics, PrintError};
use crate::dsl::policy::Language;
use crate::dsl::{parse, print};
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum ConvertError {
Parse(Diagnostics),
Print(PrintError),
}
impl std::fmt::Display for ConvertError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ConvertError::Parse(found) => write!(f, "the input does not parse: {found}"),
ConvertError::Print(err) => write!(f, "the output cannot be written: {err}"),
}
}
}
impl std::error::Error for ConvertError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
ConvertError::Parse(found) => Some(found),
ConvertError::Print(err) => Some(err),
}
}
}
pub fn convert(text: &str, to: Language) -> Result<String, ConvertError> {
let parsed = parse("<input>", text).map_err(ConvertError::Parse)?;
print(&parsed.policy, to).map_err(ConvertError::Print)
}