use std::fmt;
pub type GqlResult<T> = std::result::Result<T, GqlError>;
#[derive(Debug, Clone)]
pub enum GqlError {
LexError {
message: String,
position: usize,
},
ParseError {
message: String,
token: Option<String>,
position: usize,
},
SemanticError {
message: String,
},
ExecutionError {
message: String,
},
GraphError(crate::GraphError),
TypeError {
expected: String,
found: String,
},
VariableNotFound {
name: String,
},
LabelNotFound {
name: String,
},
PropertyNotFound {
name: String,
},
}
impl fmt::Display for GqlError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
GqlError::LexError { message, position } => {
write!(f, "Lexical error at position {position}: {message}")
}
GqlError::ParseError {
message,
token,
position,
} => {
if let Some(token) = token {
write!(
f,
"Parse error at position {position} near '{token}': {message}"
)
} else {
write!(f, "Parse error at position {position}: {message}")
}
}
GqlError::SemanticError { message } => {
write!(f, "Semantic error: {message}")
}
GqlError::ExecutionError { message } => {
write!(f, "Execution error: {message}")
}
GqlError::GraphError(err) => {
write!(f, "Graph error: {err}")
}
GqlError::TypeError { expected, found } => {
write!(f, "Type error: expected {expected}, found {found}")
}
GqlError::VariableNotFound { name } => {
write!(f, "Variable not found: {name}")
}
GqlError::LabelNotFound { name } => {
write!(f, "Label not found: {name}")
}
GqlError::PropertyNotFound { name } => {
write!(f, "Property not found: {name}")
}
}
}
}
impl std::error::Error for GqlError {}
impl From<crate::GraphError> for GqlError {
fn from(err: crate::GraphError) -> Self {
GqlError::GraphError(err)
}
}