1use std::fmt;
2
3use crate::util;
4
5#[derive(Debug)]
6pub enum LogLevel {
7 Debug,
8 Error,
9 Warning,
10}
11
12impl AsRef<str> for LogLevel {
13 fn as_ref(&self) -> &str {
14 match self {
15 LogLevel::Debug => "debug",
16 LogLevel::Error => "error",
17 LogLevel::Warning => "warning",
18 }
19 }
20}
21
22#[derive(Debug, Default)]
23pub struct Log<'f, M> {
24 pub message: M,
25 pub file: Option<&'f str>,
26 pub line: Option<usize>,
27 pub col: Option<usize>,
28}
29
30impl<'f, M> Log<'f, M> {
31 pub fn message(message: M) -> Self {
32 Self {
33 message,
34 file: None,
35 line: None,
36 col: None,
37 }
38 }
39}
40
41impl<'f, M> fmt::Display for Log<'f, M>
42where
43 M: ToString,
44{
45 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
46 if self.file.is_none() && self.line.is_none() && self.col.is_none() {
47 return write!(f, "::{}", self.message.to_string());
48 }
49
50 let args = vec![
51 self.file.map(|f| util::cmd_arg("file", f)),
52 self.line.map(|l| util::cmd_arg("line", l.to_string())),
53 self.col.map(|c| util::cmd_arg("col", c.to_string())),
54 ];
55
56 let args = args.into_iter().flatten().collect::<Vec<_>>().join(",");
57
58 write!(f, " {}::{}", args, self.message.to_string())
59 }
60}