Line data Source code
1 1 : use regex::Regex;
2 : use std::{fs::read_to_string, path::PathBuf};
3 : use structopt::StructOpt;
4 :
5 : /// Manage tags written in comments in files
6 0 : #[derive(StructOpt, Debug)]
7 : #[structopt(name = "srgat", about = "tag search for source code")]
8 : pub struct Cli {
9 0 : /// Pring tags in the files
10 : #[structopt(short, long, parse(from_os_str))]
11 0 : files: Option<Vec<PathBuf>>,
12 0 : /// Print tags in the directory
13 : #[structopt(short = "r")]
14 0 : directory: Option<String>,
15 0 : /// Ignore the files
16 : #[structopt(short, long)]
17 0 : ignore: Option<String>,
18 : // /// Print the all saved tags in default file or target file
19 : // #[structopt(short = "t", default_value = "json")]
20 : // ftype: String,
21 : // /// Save the tags in the files to default file or target file
22 : // #[structopt(short, long)]
23 : // dump: Option<PathBuf>,
24 : }
25 :
26 0 : fn find_tags(num: usize, line: &str) {
27 0 : let tags = [
28 : "TODO: ",
29 : "INFO: ",
30 : "FIX: ",
31 : "WARNING: ",
32 : "NOTE: ",
33 : "HACK: ",
34 : "PERF: ",
35 : ];
36 0 : for tag in tags {
37 0 : if find_tag(line, tag) {
38 0 : print_line(num, line)
39 : }
40 : }
41 0 : }
42 :
43 2 : fn find_tag(line: &str, tag: &str) -> bool {
44 2 : let re = Regex::new(tag).unwrap();
45 2 : let contains_tag = re.find(line);
46 2 : match contains_tag {
47 2 : Some(_) => true,
48 0 : None => false,
49 : }
50 2 : }
51 :
52 : #[test]
53 2 : fn test_find_tag() {
54 1 : let result = find_tag("TODO: This is a todo.", "TODO: ");
55 1 : assert_eq!(result, true);
56 1 : let result = find_tag("NOTE: This is a NOTE.", "NOTE: ");
57 1 : assert_eq!(result, true);
58 2 : }
59 :
60 0 : fn run_files(v: Vec<PathBuf>) {
61 0 : read_files(v)
62 0 : }
63 :
64 0 : fn read_files(vp: Vec<PathBuf>) {
65 0 : for v in vp {
66 0 : read_file(v);
67 0 : println!("");
68 : }
69 : // NOTE: 改行
70 0 : }
71 :
72 0 : fn read_file(p: PathBuf) {
73 0 : println!("{:?}", p);
74 0 : let content = read_to_string(&p).expect("could not read file");
75 0 : read_lines(content)
76 0 : }
77 :
78 0 : fn read_lines(content: String) {
79 0 : for (i, line) in content.lines().enumerate() {
80 0 : let num = i + 1;
81 0 : find_tags(num, line)
82 : }
83 0 : }
84 :
85 0 : fn print_line(num: usize, line: &str) {
86 0 : println!("{}: {}", num, line);
87 0 : }
88 :
89 0 : fn run_dirctory(dir: String, ignore: Option<String>) {
90 0 : match ignore {
91 0 : Some(v) => println!("{:#?}, {}", dir, v),
92 0 : None => println!("{:#?}", dir),
93 : }
94 0 : }
95 :
96 : // TODO: showをerror表示から変える
97 0 : fn run_show() {
98 0 : println!("error")
99 0 : }
100 :
101 : // TODO: すっきりさせる
102 0 : fn run(cli: Cli) {
103 0 : match cli.files {
104 0 : Some(v) => run_files(v),
105 0 : None => match cli.directory {
106 0 : Some(v) => run_dirctory(v, cli.ignore),
107 0 : None => run_show(),
108 : },
109 : }
110 0 : }
111 :
112 0 : fn main() {
113 0 : let cli = Cli::from_args();
114 0 : run(cli)
115 0 : }
|