Skip to main content

mz_deploy/project/error/
parse.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10//! Parse errors for SQL parsing operations.
11//!
12//! This module defines errors that occur when parsing SQL statements
13//! from project files.
14
15use crate::project::syntax::variables::VariableError;
16use std::fmt;
17use std::path::PathBuf;
18
19/// Errors that occur during SQL parsing.
20#[derive(Debug)]
21pub enum ParseError {
22    /// Failed to parse SQL statements
23    SqlParseFailed {
24        /// The file containing the SQL
25        path: PathBuf,
26        /// The SQL text that failed to parse
27        sql: String,
28        /// The underlying parser error
29        source: mz_sql_parser::parser::ParserStatementError,
30    },
31
32    /// Failed to parse SQL statements from multiple sources
33    StatementsParseFailed {
34        /// Error message
35        message: String,
36    },
37
38    /// SQL file contains variable references with no definition
39    UnresolvedVariables(VariableError),
40}
41
42impl fmt::Display for ParseError {
43    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44        match self {
45            ParseError::SqlParseFailed { source, .. } => write!(f, "{}", source.error),
46            ParseError::StatementsParseFailed { message } => write!(f, "{}", message),
47            ParseError::UnresolvedVariables(inner) => {
48                let names: Vec<String> = inner
49                    .unresolved
50                    .iter()
51                    .map(|v| format!(":{}", v.name))
52                    .collect();
53                write!(
54                    f,
55                    "unresolved variables in {}: {}",
56                    inner.path.display(),
57                    names.join(", ")
58                )
59            }
60        }
61    }
62}
63
64impl std::error::Error for ParseError {
65    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
66        match self {
67            ParseError::SqlParseFailed { source, .. } => Some(source),
68            ParseError::StatementsParseFailed { .. } => None,
69            ParseError::UnresolvedVariables(_) => None,
70        }
71    }
72}