vxx2 / vlib / v3 / errors / util.v
37 lines · 32 sloc · 729 bytes · 6c4d26f1f98c5464b012a375667ab345e462f7e0
Raw
1module errors
2
3import v3.token
4import term
5
6// Kind lists kind values used by errors.
7pub enum Kind {
8 warning
9 notice
10 error
11}
12
13// str returns the string form for Kind.
14pub fn (e Kind) str() string {
15 return match e {
16 .warning { 'warning' }
17 .notice { 'notice' }
18 .error { 'error' }
19 }
20}
21
22// color supports color handling for Kind.
23pub fn (e Kind) color(s string) string {
24 return match e {
25 .warning { term.yellow(s) }
26 .notice { term.blue(s) }
27 .error { term.red(s) }
28 }
29}
30
31// error supports error handling for errors.
32pub fn error(msg string, details string, kind Kind, pos token.Position) {
33 eprintln(pos.str() + ' -> ' + term.bold(kind.color(kind.str())) + ': ' + msg)
34 if details.len > 0 {
35 eprintln(details)
36 }
37}
38