use std::fmt;
pub type JsonSchemaParseResult<T> = Result<T, JsonSchemaParseError>;
#[derive(Debug)]
pub enum JsonSchemaParseError {
Serde(serde_json::Error),
UnknownField {
key: String,
path: String,
},
Io(std::io::Error),
}
impl fmt::Display for JsonSchemaParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
JsonSchemaParseError::Serde(e) => write!(f, "invalid JSON Schema: {e}"),
JsonSchemaParseError::UnknownField { key, path } => {
write!(f, "unknown schema key \"{key}\" at {path}")
}
JsonSchemaParseError::Io(e) => write!(f, "io error: {e}"),
}
}
}
impl std::error::Error for JsonSchemaParseError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
JsonSchemaParseError::Serde(e) => Some(e),
JsonSchemaParseError::UnknownField { .. } => None,
JsonSchemaParseError::Io(e) => Some(e),
}
}
}
impl From<serde_json::Error> for JsonSchemaParseError {
fn from(e: serde_json::Error) -> Self {
JsonSchemaParseError::Serde(e)
}
}
impl From<std::io::Error> for JsonSchemaParseError {
fn from(e: std::io::Error) -> Self {
JsonSchemaParseError::Io(e)
}
}