style: code style

This commit is contained in:
2020-07-26 15:19:19 +08:00
parent dfbb8889a7
commit ffd49ec6ea
3 changed files with 24 additions and 27 deletions

View File

@@ -9,16 +9,15 @@ pub struct Parser{
impl Parser { impl Parser {
pub fn new(keyword: String, end_filter: Box<dyn Fn(Vec<char>) -> bool>) -> Parser { pub fn new(keyword: String, end_filter: Box<dyn Fn(Vec<char>) -> bool>) -> Parser {
let callback = Box::from(|text : String, line : usize, file : &str| { let callback = Box::from(|text: String, line: usize, file: &str| {
// let path = Path::new(file).strip_prefix(env::current_dir().unwrap().to_str().unwrap()).unwrap(); // let path = Path::new(file).strip_prefix(env::current_dir().unwrap().to_str().unwrap()).unwrap();
println!("{} {} {} {} : {}", file, "TODO".green() ,"Line ".green(), line.to_string().green(), text.blue()); println!("{} {} {} {} : {}", file, "TODO".green() ,"Line ".green(), line.to_string().green(), text.blue());
}); });
Parser{keyword: keyword, end_filter: end_filter, callback} Parser{ keyword, end_filter, callback }
} }
pub fn new_callback(keyword: String, end_filter: Box<dyn Fn(Vec<char>) -> bool>, callback: Box<dyn Fn(String, usize, &str)>) -> Parser { pub fn new_callback(keyword: String, end_filter: Box<dyn Fn(Vec<char>) -> bool>, callback: Box<dyn Fn(String, usize, &str)>) -> Parser {
Parser { keyword, end_filter, callback }
Parser{ keyword: keyword, end_filter : end_filter, callback }
} }
fn get_keyword(&self) -> String { fn get_keyword(&self) -> String {

View File

@@ -1,5 +1,5 @@
use std::fs::File; use std::fs::File;
use std::io::{ self, BufRead }; use std::io::{ self, BufRead, BufReader };
use std::path::Path; use std::path::Path;
use regex::{RegexSet}; use regex::{RegexSet};
use crate::token::*; use crate::token::*;
@@ -8,19 +8,17 @@ use crate::token::*;
// Returns an Iterator to the Reader of the lines of the file. // Returns an Iterator to the Reader of the lines of the file.
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>> where P: AsRef<Path>, { fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>> where P: AsRef<Path>, {
let file = File::open(filename)?; let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines()) Ok(BufReader::new(file).lines())
} }
pub fn regex_parser(path : &str, regex : Vec<String>, verbosity : i8) -> Result<Vec<Token>, io::Error> { pub fn regex_parser(path : &str, regex : Vec<String>, verbosity : i8) -> Result<Vec<Token>, io::Error> {
let set = RegexSet::new(regex).unwrap(); let set = RegexSet::new(regex).unwrap();
let mut tokens = vec![]; let mut tokens = vec![];
// let mut line_cpt = 0; for (line_cpt, line) in (read_lines(path)?).enumerate() {
for (line_cpt, line) in (read_lines(path)?).enumerate() { // ? PLUS 1 let line_cpt_from1 = line_cpt + 1;
// for line in read_lines(path)? {
// line_cpt +=1;
let line = line.unwrap(); let line = line.unwrap();
if set.is_match(line.to_lowercase().as_str()) { if set.is_match(line.to_lowercase().as_str()) {
tokens.push(Token::new(path.to_string(), line_cpt, line, verbosity)); tokens.push(Token::new(path.to_string(), line_cpt_from1, line, verbosity));
// println!("{}", t); // println!("{}", t);
} }
} }

View File

@@ -31,14 +31,14 @@ impl Token {
// } // }
let mut t = Token { let mut t = Token {
file: file, file,
line: line, line,
keyword: "todo".to_string(), keyword: "todo".into(),
comment: None, comment: None,
priority: None, priority: None,
date: None, date: None,
member: None, member: None,
verbosity: verbosity verbosity,
}; };
for i in 0..fields.len() { for i in 0..fields.len() {
@@ -71,23 +71,23 @@ impl Token {
} }
pub fn inline(&self) { pub fn inline(&self) {
let mut s; let mut inline_msg = vec![
s = string_format!("{} line: {} {} ".to_string(), self.file.clone(), self.line.to_string().green().to_string(), self.keyword.clone().green().to_string()); format!("{} line: {} {:<6} ", self.file, self.line.to_string().green(), self.keyword.clone().green())
if self.member.is_some() { ];
s = string_format!("{} Member: {}".to_string(),s ,self.member.clone().unwrap().red().to_string()); if let Some(member) = &self.member {
inline_msg.push(format!("member: {}", member.red()));
} }
if self.priority.is_some() { if let Some(priority) = &self.priority {
s = string_format!("{} Priority: {}".to_string(), s, self.priority.clone().unwrap().red().to_string()); inline_msg.push(format!("priority: {}", priority.red()));
} }
if self.date.is_some() { if let Some(date) = &self.date {
s = string_format!("{} Deadline: {}".to_string(), s, self.date.clone().unwrap().to_string().red().to_string()); inline_msg.push(format!("deadline: {}", date.to_string().red()));
} }
if self.comment.is_some() { if let Some(comment) = &self.comment {
s = string_format!("{} {}".to_string(), s, self.comment.clone().unwrap().blue().to_string()); inline_msg.push(format!("{}", comment.blue()));
} }
println!("{}", s); println!("{}", inline_msg.join(" "));
} }
} }
// To use the `{}` marker, the trait `fmt::Display` must be implemented // To use the `{}` marker, the trait `fmt::Display` must be implemented