]> git.lizzy.rs Git - rust.git/blob - src/issues.rs
Merge pull request #128 from marcusklaas/subexpr
[rust.git] / src / issues.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // Objects for seeking through a char stream for occurences of TODO and FIXME.
12 // Depending on the loaded configuration, may also check that these have an
13 // associated issue number.
14
15 use std::fmt;
16
17 static TO_DO_CHARS: &'static [char] = &['T', 'O', 'D', 'O'];
18 static FIX_ME_CHARS: &'static [char] = &['F', 'I', 'X', 'M', 'E'];
19
20 #[derive(Clone, Copy)]
21 pub enum ReportTactic {
22     Always,
23     Unnumbered,
24     Never,
25 }
26
27 impl ReportTactic {
28     fn is_enabled(&self) -> bool {
29         match *self {
30             ReportTactic::Always => true,
31             ReportTactic::Unnumbered => true,
32             ReportTactic::Never => false
33         }
34     }
35 }
36
37 impl_enum_decodable!(ReportTactic, Always, Unnumbered, Never);
38
39 #[derive(Clone, Copy)]
40 enum Seeking {
41     Issue {
42         todo_idx: usize,
43         fixme_idx: usize,
44     },
45     Number {
46         issue: Issue,
47         part: NumberPart,
48     },
49 }
50
51 #[derive(Clone, Copy)]
52 enum NumberPart {
53     OpenParen,
54     Pound,
55     Number,
56     CloseParen,
57 }
58
59 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
60 pub struct Issue {
61     issue_type: IssueType,
62     // Indicates whether we're looking for issues with missing numbers, or
63     // all issues of this type.
64     missing_number: bool,
65 }
66
67 impl fmt::Display for Issue {
68     fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
69         let msg = match self.issue_type {
70             IssueType::Todo => "TODO",
71             IssueType::Fixme => "FIXME",
72         };
73         let details = if self.missing_number { " without issue number" } else { "" };
74
75         write!(fmt, "{}{}", msg, details)
76     }
77 }
78
79 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
80 enum IssueType {
81     Todo,
82     Fixme,
83 }
84
85 enum IssueClassification {
86     Good,
87     Bad(Issue),
88     None,
89 }
90
91 pub struct BadIssueSeeker {
92     state: Seeking,
93     report_todo: ReportTactic,
94     report_fixme: ReportTactic,
95 }
96
97 impl BadIssueSeeker {
98     pub fn new(report_todo: ReportTactic, report_fixme: ReportTactic) -> BadIssueSeeker {
99         BadIssueSeeker { state: Seeking::Issue { todo_idx: 0, fixme_idx: 0 },
100                          report_todo: report_todo,
101                          report_fixme: report_fixme, }
102     }
103
104     // Check whether or not the current char is conclusive evidence for an
105     // unnumbered TO-DO or FIX-ME.
106     pub fn inspect(&mut self, c: char) -> Option<Issue> {
107         match self.state {
108             Seeking::Issue { todo_idx, fixme_idx } => {
109                 self.state = self.inspect_issue(c, todo_idx, fixme_idx);
110             },
111             Seeking::Number { issue, part } => {
112                 let result = self.inspect_number(c, issue, part);
113
114                 if let IssueClassification::None = result {
115                     return None;
116                 }
117
118                 self.state = Seeking::Issue { todo_idx: 0, fixme_idx: 0 };
119
120                 if let IssueClassification::Bad(issue) = result {
121                     return Some(issue);
122                 }
123             }
124         }
125
126         None
127     }
128
129     fn inspect_issue(&mut self, c: char, mut todo_idx: usize, mut fixme_idx: usize) -> Seeking {
130         // FIXME: Should we also check for lower case characters?
131         if self.report_todo.is_enabled() && c == TO_DO_CHARS[todo_idx] {
132             todo_idx += 1;
133             if todo_idx == TO_DO_CHARS.len() {
134                 return Seeking::Number {
135                     issue: Issue {
136                         issue_type: IssueType::Todo,
137                         missing_number: if let ReportTactic::Unnumbered = self.report_todo {
138                             true
139                         } else {
140                             false
141                         }
142                     },
143                     part: NumberPart::OpenParen
144                 };
145             }
146             fixme_idx = 0;
147         } else if self.report_fixme.is_enabled() && c == FIX_ME_CHARS[fixme_idx] {
148             // Exploit the fact that the character sets of todo and fixme
149             // are disjoint by adding else.
150             fixme_idx += 1;
151             if fixme_idx == FIX_ME_CHARS.len() {
152                 return Seeking::Number {
153                     issue: Issue {
154                         issue_type: IssueType::Fixme,
155                         missing_number: if let ReportTactic::Unnumbered = self.report_fixme {
156                             true
157                         } else {
158                             false
159                         }
160                     },
161                     part: NumberPart::OpenParen
162                 };
163             }
164             todo_idx = 0;
165         } else {
166             todo_idx = 0;
167             fixme_idx = 0;
168         }
169
170         Seeking::Issue { todo_idx: todo_idx, fixme_idx: fixme_idx }
171     }
172
173     fn inspect_number(&mut self,
174                       c: char,
175                       issue: Issue,
176                       mut part: NumberPart)
177                       -> IssueClassification {
178         if ! issue.missing_number || c == '\n' {
179             return IssueClassification::Bad(issue);
180         } else if c == ')' {
181             return if let NumberPart::CloseParen = part {
182                 IssueClassification::Good
183             } else {
184                 IssueClassification::Bad(issue)
185             };
186         }
187
188         match part {
189             NumberPart::OpenParen => {
190                 if c != '(' {
191                     return IssueClassification::Bad(issue);
192                 } else {
193                     part = NumberPart::Pound;
194                 }
195             },
196             NumberPart::Pound => {
197                 if c == '#' {
198                     part = NumberPart::Number;
199                 }
200             },
201             NumberPart::Number => {
202                 if c >= '0' && c <= '9' {
203                     part = NumberPart::CloseParen;
204                 } else {
205                     return IssueClassification::Bad(issue);
206                 }
207             },
208             NumberPart::CloseParen => {}
209         }
210
211         self.state = Seeking::Number {
212             part: part,
213             issue: issue
214         };
215
216         IssueClassification::None
217     }
218 }
219
220 #[test]
221 fn find_unnumbered_issue() {
222     fn check_fail(text: &str, failing_pos: usize) {
223         let mut seeker = BadIssueSeeker::new(ReportTactic::Unnumbered, ReportTactic::Unnumbered);
224         assert_eq!(Some(failing_pos), text.chars().position(|c| seeker.inspect(c).is_some()));
225     }
226
227     fn check_pass(text: &str) {
228         let mut seeker = BadIssueSeeker::new(ReportTactic::Unnumbered, ReportTactic::Unnumbered);
229         assert_eq!(None, text.chars().position(|c| seeker.inspect(c).is_some()));
230     }
231
232     check_fail("TODO\n", 4);
233     check_pass(" TO FIX DOME\n");
234     check_fail(" \n FIXME\n", 8);
235     check_fail("FIXME(\n", 6);
236     check_fail("FIXME(#\n", 7);
237     check_fail("FIXME(#1\n", 8);
238     check_fail("FIXME(#)1\n", 7);
239     check_pass("FIXME(#1222)\n");
240     check_fail("FIXME(#12\n22)\n", 9);
241     check_pass("FIXME(@maintainer, #1222, hello)\n");
242     check_fail("TODO(#22) FIXME\n", 15);
243 }
244
245 #[test]
246 fn find_issue() {
247     fn is_bad_issue(text: &str, report_todo: ReportTactic, report_fixme: ReportTactic) -> bool {
248         let mut seeker = BadIssueSeeker::new(report_todo, report_fixme);
249         text.chars().any(|c| seeker.inspect(c).is_some())
250     }
251
252     assert!(is_bad_issue("TODO(@maintainer, #1222, hello)\n",
253                          ReportTactic::Always,
254                          ReportTactic::Never));
255
256     assert!(! is_bad_issue("TODO: no number\n",
257                            ReportTactic::Never,
258                            ReportTactic::Always));
259
260     assert!(is_bad_issue("This is a FIXME(#1)\n",
261                          ReportTactic::Never,
262                          ReportTactic::Always));
263
264     assert!(! is_bad_issue("bad FIXME\n",
265                            ReportTactic::Always,
266                            ReportTactic::Never));
267 }
268
269 #[test]
270 fn issue_type() {
271     let mut seeker = BadIssueSeeker::new(ReportTactic::Always, ReportTactic::Never);
272     let expected = Some(Issue { issue_type: IssueType::Todo, missing_number: false });
273
274     assert_eq!(expected,
275                "TODO(#100): more awesomeness".chars()
276                                        .map(|c| seeker.inspect(c))
277                                        .find(Option::is_some)
278                                        .unwrap());
279
280     let mut seeker = BadIssueSeeker::new(ReportTactic::Never, ReportTactic::Unnumbered);
281     let expected = Some(Issue { issue_type: IssueType::Fixme, missing_number: true });
282
283     assert_eq!(expected,
284                "Test. FIXME: bad, bad, not good".chars()
285                                                 .map(|c| seeker.inspect(c))
286                                                 .find(Option::is_some)
287                                                 .unwrap());
288 }