use std::fmt;
pub type CodeGenResult<T> = Result<T, CodeGenError>;
#[derive(Debug)]
pub enum CodeGenError {
RootNotObject,
Io(std::io::Error),
Batch {
index: usize,
source: Box<CodeGenError>,
},
AllOfMergeEmpty,
AllOfMergeNonObjectSubschema { index: usize },
AllOfMergeConflictingPropertyType {
property_key: String,
subschema_indices: Vec<usize>,
},
AllOfMergeConflictingNumericBounds {
property_key: String,
keyword: String,
},
AllOfMergeConflictingEnum { property_key: String },
AllOfMergeConflictingConst { property_key: String },
AllOfMergeConflictingPattern { property_key: String },
AllOfMergeUnsupportedSubschema { index: usize, reason: String },
AnyOfEmpty,
OneOfEmpty,
RefResolution { ref_str: String, reason: String },
}
impl fmt::Display for CodeGenError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
CodeGenError::RootNotObject => write!(
f,
"root schema must have type \"object\" and non-empty properties"
),
CodeGenError::Io(e) => write!(f, "io error: {e}"),
CodeGenError::Batch { index, source } => {
write!(f, "schema at index {index}: {source}")
}
CodeGenError::AllOfMergeEmpty => {
write!(f, "allOf is present but empty (no subschemas to merge)")
}
CodeGenError::AllOfMergeNonObjectSubschema { index } => write!(
f,
"allOf subschema at index {index} is not object-like (need type \"object\" or non-empty properties)"
),
CodeGenError::AllOfMergeConflictingPropertyType {
property_key,
subschema_indices,
} => write!(
f,
"allOf merge: property \"{property_key}\" has conflicting types in subschemas at indices {subschema_indices:?}"
),
CodeGenError::AllOfMergeConflictingNumericBounds {
property_key,
keyword,
} => write!(
f,
"allOf merge: property \"{property_key}\" has conflicting {keyword} across subschemas"
),
CodeGenError::AllOfMergeConflictingEnum { property_key } => write!(
f,
"allOf merge: property \"{property_key}\" has conflicting enum values across subschemas"
),
CodeGenError::AllOfMergeConflictingConst { property_key } => write!(
f,
"allOf merge: property \"{property_key}\" has conflicting const values across subschemas"
),
CodeGenError::AllOfMergeConflictingPattern { property_key } => write!(
f,
"allOf merge: property \"{property_key}\" has conflicting pattern across subschemas"
),
CodeGenError::AllOfMergeUnsupportedSubschema { index, reason } => {
write!(
f,
"allOf subschema at index {index} is unsupported for merge: {reason}"
)
}
CodeGenError::AnyOfEmpty => {
write!(f, "anyOf is present but empty (no subschemas)")
}
CodeGenError::OneOfEmpty => {
write!(f, "oneOf is present but empty (no subschemas)")
}
CodeGenError::RefResolution { ref_str, reason } => {
write!(f, "could not resolve $ref \"{ref_str}\": {reason}")
}
}
}
}
impl std::error::Error for CodeGenError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
CodeGenError::Io(e) => Some(e),
CodeGenError::RootNotObject
| CodeGenError::AllOfMergeEmpty
| CodeGenError::AllOfMergeNonObjectSubschema { .. }
| CodeGenError::AllOfMergeConflictingPropertyType { .. }
| CodeGenError::AllOfMergeConflictingNumericBounds { .. }
| CodeGenError::AllOfMergeConflictingEnum { .. }
| CodeGenError::AllOfMergeConflictingConst { .. }
| CodeGenError::AllOfMergeConflictingPattern { .. }
| CodeGenError::AllOfMergeUnsupportedSubschema { .. }
| CodeGenError::AnyOfEmpty
| CodeGenError::OneOfEmpty
| CodeGenError::RefResolution { .. } => None,
CodeGenError::Batch { source, .. } => Some(source.as_ref()),
}
}
}
impl From<std::io::Error> for CodeGenError {
fn from(e: std::io::Error) -> Self {
CodeGenError::Io(e)
}
}