use thiserror::Error;
#[derive(Debug, Error)]
pub enum ParseError {
#[error("SQL syntax error: {0}")]
SqlSyntax(String),
#[error("invalid graph pattern: {0}")]
InvalidPattern(String),
#[error("invalid vector operation: {0}")]
InvalidVectorOp(String),
#[error("invalid graph operation: {0}")]
InvalidGraphOp(String),
#[error("unsupported feature: {0}")]
Unsupported(String),
#[error("empty query")]
EmptyQuery,
#[error("invalid identifier: {0}")]
InvalidIdentifier(String),
#[error("invalid literal: {0}")]
InvalidLiteral(String),
#[error("unexpected token: expected {expected}, found {found}")]
UnexpectedToken {
expected: String,
found: String,
},
#[error("missing required clause: {0}")]
MissingClause(String),
#[error("invalid operator {operator} for types {left_type} and {right_type}")]
InvalidOperator {
operator: String,
left_type: String,
right_type: String,
},
#[error("query materialized {actual} rows, limit is {limit}")]
QueryTooLarge {
actual: usize,
limit: usize,
},
}
impl From<sqlparser::parser::ParserError> for ParseError {
fn from(err: sqlparser::parser::ParserError) -> Self {
Self::SqlSyntax(err.to_string())
}
}
pub type ParseResult<T> = Result<T, ParseError>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn error_display() {
let err = ParseError::SqlSyntax("unexpected EOF".to_string());
assert!(err.to_string().contains("SQL syntax error"));
assert!(err.to_string().contains("unexpected EOF"));
}
#[test]
fn unexpected_token_display() {
let err = ParseError::UnexpectedToken {
expected: "identifier".to_string(),
found: "number".to_string(),
};
assert!(err.to_string().contains("expected identifier"));
assert!(err.to_string().contains("found number"));
}
}