tr_lang/errwarn/
errwarn.rs

1use crate::util::{get_lang, SupportedLanguage};
2use std::error;
3use std::fmt;
4use std::process::exit;
5
6#[derive(Debug)]
7pub struct Error {
8    name: String,
9    explanation: String,
10    position: (usize, usize, String),
11    after_note: Option<String>,
12}
13impl Error {
14    pub fn error(&self) -> ! {
15        self.eprint();
16        exit(1);
17    }
18    pub fn eprint(&self) {
19        match get_lang() {
20            SupportedLanguage::English => {
21                eprintln!(
22                    "\n[ERROR] {}, Line {:?}, Column {:?}",
23                    self.position.2, self.position.0, self.position.1
24                );
25                eprintln!("    {}: {}", self.name, self.explanation);
26            }
27            SupportedLanguage::Turkish => {
28                eprintln!(
29                    "\n[HATA] {}, Satır {:?}, Sütun {:?}",
30                    self.position.2, self.position.0, self.position.1
31                );
32                eprintln!("    {}: {}", self.name, self.explanation);
33            }
34        }
35        if let Some(note) = self.after_note.clone() {
36            for line in note.lines() {
37                println!("    {line}");
38            }
39        }
40    }
41}
42impl fmt::Display for Error {
43    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44        write!(f, "{:?}", self)
45    }
46}
47impl error::Error for Error {}
48
49#[allow(non_snake_case)]
50pub mod ErrorGenerator {
51    use super::Error;
52    use crate::store::globarg::SUPRESS_WARN;
53    use crate::util::{get_lang, SupportedLanguage};
54
55    pub fn error(
56        name: &str,
57        explanation: &str,
58        line: usize,
59        col: usize,
60        file: String,
61        after_note: Option<String>,
62    ) -> Error {
63        Error {
64            name: name.to_string(),
65            explanation: explanation.to_string(),
66            position: (line, col, file),
67            after_note,
68        }
69    }
70    pub fn warning(
71        name: &'static str,
72        explanation: &'static str,
73        line: usize,
74        col: usize,
75        file: String,
76    ) -> Box<dyn Fn() -> () + 'static> {
77        Box::new(move || {
78            if !unsafe { SUPRESS_WARN } {
79                match get_lang() {
80                    SupportedLanguage::English => {
81                        eprintln!("[WARNING] {}, Line {:?}, Column {:?}", file, line, col);
82                        eprintln!("    {}: {}", name, explanation);
83                    }
84                    SupportedLanguage::Turkish => {
85                        eprintln!("[UYARI] {}, Satır {:?}, Sütun {:?}", file, line, col);
86                        eprintln!("    {}: {}", name, explanation);
87                    }
88                }
89            }
90        })
91    }
92}