1use ariadne::{Color, Label, Report, ReportKind, Source};
2use thiserror::Error;
3
4#[derive(Debug, Error)]
5pub enum CobbleError {
6 #[error("Parse error")]
7 ParseError(String),
8
9 #[error("IO error: {0}")]
10 IoError(#[from] std::io::Error),
11
12 #[error("Transpilation error: {0}")]
13 TranspileError(String),
14}
15
16pub fn report_error(filename: &str, src: &str, error: &CobbleError) {
17 match error {
18 CobbleError::ParseError(msg) => {
19 Report::build(ReportKind::Error, (filename, 0..1))
20 .with_message("Parse Error")
21 .with_label(
22 Label::new((filename, 0..1))
23 .with_message(msg.clone())
24 .with_color(Color::Red),
25 )
26 .finish()
27 .print((filename, Source::from(src)))
28 .unwrap();
29 }
30 CobbleError::TranspileError(msg) => {
31 Report::build(ReportKind::Error, (filename, 0..1))
32 .with_message("Transpilation Error")
33 .with_label(
34 Label::new((filename, 0..1))
35 .with_message(msg.clone())
36 .with_color(Color::Red),
37 )
38 .finish()
39 .print((filename, Source::from(src)))
40 .unwrap();
41 }
42 CobbleError::IoError(e) => {
43 eprintln!("IO Error: {}", e);
44 }
45 }
46}
47
48pub fn report_parse_errors(filename: &str, src: &str, errors: &[String]) {
50 for error in errors {
51 eprintln!("Parse error in {}: {}", filename, error);
52 }
53
54 if errors.len() == 1 {
56 Report::build(ReportKind::Error, (filename, 0..src.len().min(1)))
57 .with_message("Parse Error")
58 .with_note(&errors[0])
59 .finish()
60 .eprint((filename, Source::from(src)))
61 .ok();
62 }
63}