#![allow(dead_code)]
use std::process;
use super::{displayer::Displayer, ErrorDetails};
#[derive(Clone)]
pub enum LogType {
Error,
Warning,
Info
}
pub struct Logger {
pub kind: LogType,
pub row: usize,
pub col: usize,
pub len: usize,
pub path: Option<String>,
pub code: Option<String>,
pub message: Option<String>,
pub comment: Option<String>
}
impl Logger {
pub fn new(path: Option<String>, code: Option<String>, row: usize, col: usize, len: usize, kind: LogType) -> Self {
Logger {
kind,
path,
row,
col,
len,
code,
message: None,
comment: None
}
}
pub fn new_msg(message: impl AsRef<str>, kind: LogType) -> Self {
Logger {
kind,
row: 0,
col: 0,
len: 0,
path: None,
code: None,
message: Some(message.as_ref().to_string()),
comment: None
}
}
pub fn new_err_msg(message: impl AsRef<str>) -> Self {
Logger::new_msg(message, LogType::Error)
}
pub fn new_warn_msg(message: impl AsRef<str>) -> Self {
Logger::new_msg(message, LogType::Warning)
}
pub fn new_info_msg(message: impl AsRef<str>) -> Self {
Logger::new_msg(message, LogType::Info)
}
pub fn new_err_with_details(path: Option<String>, code: Option<String>, details: ErrorDetails) -> Self {
let det = details.get_pos_by_file_or_code(path.clone(), code.clone());
Logger::new(path, code, det.0, det.1, details.len, LogType::Error)
}
pub fn new_warn_with_details(path: Option<String>, code: Option<String>, details: ErrorDetails) -> Self {
let det = details.get_pos_by_file_or_code(path.clone(), code.clone());
Logger::new(path, code, det.0, det.1, details.len, LogType::Warning)
}
pub fn new_info_with_details(path: Option<String>, code: Option<String>, details: ErrorDetails) -> Self {
let det = details.get_pos_by_file_or_code(path.clone(), code.clone());
Logger::new(path, code, det.0, det.1, details.len, LogType::Info)
}
pub fn new_err_at_position(path: Option<String>, code: Option<String>, (row, col): (usize, usize)) -> Self {
Logger::new(path, code, row, col, 0, LogType::Error)
}
pub fn new_warn_at_position(path: Option<String>, code: Option<String>, (row, col): (usize, usize)) -> Self {
Logger::new(path, code, row, col, 0, LogType::Warning)
}
pub fn new_info_at_position(path: Option<String>, code: Option<String>, (row, col): (usize, usize)) -> Self {
Logger::new(path, code, row, col, 0, LogType::Info)
}
pub fn attach_message<T: AsRef<str>>(mut self, text: T) -> Self {
self.message = Some(String::from(text.as_ref()));
self
}
pub fn attach_comment<T: AsRef<str>>(mut self, text: T) -> Self {
self.comment = Some(String::from(text.as_ref()));
self
}
pub fn attach_code(mut self, code: String) -> Self {
self.code = Some(code);
self
}
pub fn show(self) -> Self {
let color = match &self.kind {
LogType::Error => (255, 80, 80),
LogType::Warning => (255, 180, 80),
LogType::Info => (80, 80, 255)
};
if self.row > 0 && self.col > 0 {
Displayer::new(color, self.row, self.col, self.len)
.header(self.kind.clone())
.text(self.message.clone())
.path(self.path.clone())
.padded_text(self.comment.clone())
.snippet(self.code.clone())
}
else {
Displayer::new(color, self.row, self.col, self.len)
.header(self.kind.clone())
.text(self.message.clone())
.padded_text(self.comment.clone());
}
self
}
pub fn exit(self) {
process::exit(1);
}
}
#[cfg(test)]
mod test {
#[test]
fn test_logger() {
}
}