style: code style

This commit is contained in:
2020-07-26 15:31:26 +08:00
parent ffd49ec6ea
commit d4aaebf94e

View File

@@ -1,5 +1,4 @@
use chrono::NaiveDate; use chrono::NaiveDate;
use string_format::string_format;
use std::fmt; use std::fmt;
use colored::Colorize; use colored::Colorize;
use regex::Regex; use regex::Regex;
@@ -19,7 +18,7 @@ pub struct Token{
impl Token { impl Token {
pub fn new(file: String, line: usize, s: String, verbosity: i8) -> Token { pub fn new(file: String, line: usize, s: String, verbosity: i8) -> Token {
// println!("{}", s); // println!("{}", s);
let fields : Vec<&str>= s.split_whitespace().collect(); let fields: Vec<&str>= s.split_whitespace().collect();
let number_regex = Regex::new("\\b[1-9]\\b").unwrap(); let number_regex = Regex::new("\\b[1-9]\\b").unwrap();
let date_regex = Regex::new("(\\d*/\\d*/\\d*)").unwrap(); let date_regex = Regex::new("(\\d*/\\d*/\\d*)").unwrap();
let member_regex = Regex::new("!\\w*").unwrap(); let member_regex = Regex::new("!\\w*").unwrap();
@@ -59,11 +58,10 @@ impl Token {
t.member = Some(member); t.member = Some(member);
} else { } else {
if t.comment.is_none() { t.comment = match t.comment {
t.comment = Some(fields[i].to_string()); None => Some(fields[i].to_string()),
} else { Some(comment) => Some(format!("{} {}", comment, fields[i])),
t.comment = Some(string_format!("{} {}".to_string(),t.comment.unwrap(), fields[i].to_string())); };
}
} }
} }
@@ -93,32 +91,34 @@ impl Token {
// To use the `{}` marker, the trait `fmt::Display` must be implemented // To use the `{}` marker, the trait `fmt::Display` must be implemented
// manually for the type. // manually for the type.
impl fmt::Display for Token { impl fmt::Display for Token {
// This trait requires `fmt` with this exact signature.
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut s; let mut multiline_msg = vec![
format!("{} line: {} {}", self.file, self.line.to_string().green(), self.keyword.green())
];
s = string_format!("{} line: {} {} \n".to_string(), self.file.clone(), self.line.to_string().green().to_string(), self.keyword.clone().green().to_string()); if self.verbosity <= 1 {
if self.verbosity <= 1{ if let Some(comment) = &self.comment {
if self.comment.is_some() { multiline_msg.push(format!("{}", comment.blue()));
s = string_format!("{}{}\n".to_string(), s, self.comment.clone().unwrap().blue().to_string());
} }
} else {
if let Some(member) = &self.member {
multiline_msg.push(format!("- member : {}", member.red()));
} }
else { if let Some(priority) = &self.priority {
if self.member.is_some() { multiline_msg.push(format!("- priority: {}", priority.red()));
s = string_format!("{}Member: {}\n".to_string(),s ,self.member.clone().unwrap().red().to_string());
} }
if self.priority.is_some() { if let Some(date) = &self.date {
s = string_format!("{}Priority: {}\n".to_string(), s, self.priority.clone().unwrap().red().to_string()); multiline_msg.push(format!("- deadline: {}", date.to_string().red()));
} }
if self.date.is_some() { if let Some(comment) = &self.comment {
s = string_format!("{}Deadline: {}\n".to_string(), s, self.date.clone().unwrap().to_string().red().to_string()); multiline_msg.push(format!("{}", comment.blue()));
}
if self.comment.is_some() {
s = string_format!("{}{}\n".to_string(), s, self.comment.clone().unwrap().blue().to_string());
} }
// if self.comment.is_some() {
// s = string_format!("{}{}\n".to_string(), s, self.comment.clone().unwrap().blue().to_string());
// }
} }
write!(f, "{}", s)?; write!(f, "{}\n", multiline_msg.join("\n"))?;
Ok(()) Ok(())
} }
} }