zaplib_shader_compiler/
error.rs

1use crate::{code_fragment::CodeFragment, span::Span};
2use std::fmt;
3
4#[derive(Clone, Debug)]
5pub struct ParseError {
6    pub span: Span,
7    pub message: String,
8}
9
10impl fmt::Display for ParseError {
11    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
12        write!(f, "{}", self.message)
13    }
14}
15
16impl ParseError {
17    pub fn format_for_console(&self, code_fragments: &[CodeFragment]) -> String {
18        let code_fragment = &code_fragments[self.span.code_fragment_id.0];
19        let pos = self.span.start;
20        // Hacky debugging: besides printing the file/line/col and so on, we also pull out a
21        // string (without newlines) from the actual code, and position it such that `pos`
22        // appears right under our "vvvvvv". :-)
23        format!(
24            "Error parsing shader at {} => {}\n\naround:             vvvvvv\n{}",
25            code_fragment.name_line_col_at_offset(pos),
26            self.message,
27            code_fragment.code().chars().skip(pos - 20).take(50).collect::<String>().replace('\n', " "),
28        )
29    }
30}