sqlexpr-congo-rust 1.0.0

Parser for SqlExprParser - Generated by CongoCC
Documentation
//! Error types for the parser. Generated by CongoCC Parser Generator. Do not edit.

use std::fmt;

/// Result type for parsing operations
pub type ParseResult<T> = Result<T, ParseError>;

/// Error type for parsing failures
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ParseError {
    /// Error message describing what went wrong
    pub message: String,
    /// Position in the input where the error occurred
    pub position: Option<usize>,
    /// Line number where the error occurred (if available)
    pub line: Option<usize>,
    /// Column number where the error occurred (if available)
    pub column: Option<usize>,
}

impl ParseError {
    /// Create a new parse error with just a message
    pub fn new(message: impl Into<String>) -> Self {
        ParseError {
            message: message.into(),
            position: None,
            line: None,
            column: None,
        }
    }

    /// Create a parse error with position information
    pub fn at_position(message: impl Into<String>, position: usize) -> Self {
        ParseError {
            message: message.into(),
            position: Some(position),
            line: None,
            column: None,
        }
    }

    /// Create a parse error with line and column information
    pub fn at_location(message: impl Into<String>, line: usize, column: usize) -> Self {
        ParseError {
            message: message.into(),
            position: None,
            line: Some(line),
            column: Some(column),
        }
    }

    /// Add position information to an existing error
    pub fn with_position(mut self, position: usize) -> Self {
        self.position = Some(position);
        self
    }

    /// Add line/column information to an existing error
    pub fn with_location(mut self, line: usize, column: usize) -> Self {
        self.line = Some(line);
        self.column = Some(column);
        self
    }
}

impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let (Some(line), Some(column)) = (self.line, self.column) {
            write!(f, "Parse error at line {}, column {}: {}", line, column, self.message)
        } else if let Some(position) = self.position {
            write!(f, "Parse error at position {}: {}", position, self.message)
        } else {
            write!(f, "Parse error: {}", self.message)
        }
    }
}

impl std::error::Error for ParseError {}