Skip to main content

substrait_explain/parser/
errors.rs

1use std::fmt;
2
3use substrait::proto::Plan;
4use thiserror::Error;
5
6use super::MessageParseError;
7use super::extensions::ExtensionParseError;
8use crate::extensions::registry::ExtensionError;
9
10/// Context for parse errors and warnings
11#[derive(Debug, Clone)]
12pub struct ParseContext {
13    pub line_no: i64,
14    pub line: String,
15}
16
17impl ParseContext {
18    pub fn new(line_no: i64, line: String) -> Self {
19        Self { line_no, line }
20    }
21}
22
23impl fmt::Display for ParseContext {
24    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25        write!(f, "line {}: '{}'", self.line_no, self.line)
26    }
27}
28
29/// Parse errors that prevent successful parsing
30#[derive(Debug, Clone, Error)]
31pub enum ParseError {
32    #[error("Error parsing extension on {0}: {1}")]
33    Extension(ParseContext, #[source] ExtensionParseError),
34
35    #[error("Error parsing extension detail on {0}: {1}")]
36    ExtensionDetail(ParseContext, #[source] ExtensionError),
37
38    #[error("Error parsing plan on {0}: {1}")]
39    Plan(ParseContext, #[source] MessageParseError),
40
41    #[error("Expected '{expected}' on {context}")]
42    Expected {
43        expected: &'static str,
44        context: ParseContext,
45    },
46
47    #[error("No relation found in plan")]
48    NoRelationFound,
49
50    #[error("Unregistered extension '{name}' on {context}")]
51    UnregisteredExtension { name: String, context: ParseContext },
52
53    #[error("Failed to parse relation on {0}: {1}")]
54    RelationParse(ParseContext, String),
55
56    #[error("Invalid input at {0}: {1}")]
57    ValidationError(ParseContext, String),
58
59    #[error("Error parsing section header on line {0}: {1}")]
60    Initial(ParseContext, #[source] MessageParseError),
61
62    #[error("Error parsing relation: {0}")]
63    Relation(ParseContext, #[source] MessageParseError),
64}
65
66/// Result type for the public Parser API
67pub type ParseResult = Result<Plan, ParseError>;