use thiserror::Error;
pub type Result<T> = std::result::Result<T, OpenApiError>;
#[derive(Error, Debug)]
pub enum OpenApiError {
#[error("Failed to parse YAML: {0}")]
YamlParse(#[from] serde_yaml::Error),
#[error("Failed to parse JSON: {0}")]
JsonParse(#[from] serde_json::Error),
#[error("Invalid OpenAPI specification: {0}")]
InvalidSpec(String),
#[error("Missing required field: {0}")]
MissingField(String),
#[error("Unsupported schema type: {0}")]
UnsupportedType(String),
#[error("Failed to resolve reference '{reference}': {reason}")]
ReferenceResolution {
reference: String,
reason: String,
},
#[error("Circular reference detected: {0}")]
CircularReference(String),
#[error("Invalid schema composition (allOf/oneOf/anyOf): {0}")]
InvalidComposition(String),
#[cfg(feature = "fetch")]
#[error("Failed to fetch OpenAPI spec from URL: {0}")]
FetchError(String),
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("{0}")]
Other(String),
}
impl OpenApiError {
pub fn invalid_spec(msg: impl Into<String>) -> Self {
Self::InvalidSpec(msg.into())
}
pub fn missing_field(field: impl Into<String>) -> Self {
Self::MissingField(field.into())
}
pub fn unsupported_type(type_name: impl Into<String>) -> Self {
Self::UnsupportedType(type_name.into())
}
pub fn reference_resolution(reference: impl Into<String>, reason: impl Into<String>) -> Self {
Self::ReferenceResolution {
reference: reference.into(),
reason: reason.into(),
}
}
pub fn circular_reference(path: impl Into<String>) -> Self {
Self::CircularReference(path.into())
}
pub fn invalid_composition(msg: impl Into<String>) -> Self {
Self::InvalidComposition(msg.into())
}
}