envuse_parser/envuse/
display_program_error.rs1use crate::{
2 errors::{parser_error::ParseError, program_error::ProgramError},
3 syntax_error::SyntaxError,
4};
5
6use std::error::Error;
7
8pub fn display_program_error<A, T: ToString>(
9 result: Result<A, Box<dyn Error>>,
10 source: T,
11 location_val: Option<String>,
12) -> Result<A, Box<dyn Error>> {
13 match result {
14 Err(error) if error.is::<SyntaxError>() => {
15 let syntax_error = error.downcast_ref::<SyntaxError>().unwrap();
16 do yeet ProgramError {
17 message: format!("SyntaxError: {}", syntax_error.message),
18 span: Some(syntax_error.span.clone()),
19 source: source.to_string(),
20 location: location_val,
21 cause: Some(error),
22 }
23 }
24 Err(error) if error.is::<ParseError>() => {
25 let syntax_error = error.downcast_ref::<ParseError>().unwrap();
26 do yeet ProgramError {
27 message: format!("ParseError: {}", syntax_error.message),
28 span: Some(syntax_error.span.clone()),
29 source: source.to_string(),
30 location: location_val,
31 cause: Some(error),
32 }
33 }
34 result_program => result_program,
35 }
36}