use std::fmt;
#[derive(Debug)]
pub enum ConvexTypeGeneratorError
{
MissingSchemaFile,
ParsingFailed
{
file: String,
details: String,
},
EmptySchemaFile
{
file: String,
},
InvalidPath(String),
InvalidUnicode(String),
SerializationFailed(serde_json::Error),
IOError
{
file: String,
error: std::io::Error,
},
InvalidSchema
{
context: String,
details: String,
},
CircularReference
{
path: Vec<String>,
},
InvalidType
{
found: String,
valid_types: Vec<String>,
},
}
impl fmt::Display for ConvexTypeGeneratorError
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
{
match self {
Self::MissingSchemaFile => write!(f, "Schema file not found"),
Self::ParsingFailed { file, details } => {
write!(f, "Failed to parse file '{}': {}", file, details)
}
Self::EmptySchemaFile { file } => {
write!(f, "Schema file '{}' is empty", file)
}
Self::InvalidPath(path) => {
write!(f, "Invalid path: {}", path)
}
Self::InvalidUnicode(path) => {
write!(f, "Path contains invalid Unicode: {}", path)
}
Self::SerializationFailed(err) => {
write!(f, "Failed to serialize AST: {}", err)
}
Self::IOError { file, error } => {
write!(f, "IO error while reading '{}': {}", file, error)
}
Self::InvalidSchema { context, details } => {
write!(f, "Invalid schema at {}: {}", context, details)
}
Self::CircularReference { path } => {
write!(f, "Circular type reference detected: {}", path.join(" -> "))
}
Self::InvalidType { found, valid_types } => {
write!(f, "Invalid type '{}'. Valid types are: {}", found, valid_types.join(", "))
}
}
}
}
impl From<std::io::Error> for ConvexTypeGeneratorError
{
fn from(error: std::io::Error) -> Self
{
ConvexTypeGeneratorError::IOError {
file: String::new(),
error,
}
}
}
impl std::error::Error for ConvexTypeGeneratorError {}
impl ConvexTypeGeneratorError
{
pub fn with_file_context(self, file: impl Into<String>) -> Self
{
match self {
Self::IOError { error, .. } => Self::IOError {
file: file.into(),
error,
},
other => other,
}
}
}