]> git.lizzy.rs Git - rust.git/blob - src/issues.rs
Merge pull request #1015 from srinivasreddy/readme
[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 pub use config::ReportTactic;
17
18 const TO_DO_CHARS: &'static [char] = &['T', 'O', 'D', 'O'];
19 const FIX_ME_CHARS: &'static [char] = &['F', 'I', 'X', 'M', 'E'];
20
21 // Enabled implementation detail is here because it is
22 // irrelevant outside the issues module
23 impl ReportTactic {
24     fn is_enabled(&self) -> bool {
25         match *self {
26             ReportTactic::Always => true,
27             ReportTactic::Unnumbered => true,
28             ReportTactic::Never => false,
29         }
30     }
31 }
32
33 #[derive(Clone, Copy)]
34 enum Seeking {
35     Issue { todo_idx: usize, fixme_idx: usize },
36     Number { issue: Issue, part: NumberPart },
37 }
38
39 #[derive(Clone, Copy)]
40 enum NumberPart {
41     OpenParen,
42     Pound,
43     Number,
44     CloseParen,
45 }
46
47 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
48 pub struct Issue {
49     issue_type: IssueType,
50     // Indicates whether we're looking for issues with missing numbers, or
51     // all issues of this type.
52     missing_number: bool,
53 }
54
55 impl fmt::Display for Issue {
56     fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
57         let msg = match self.issue_type {
58             IssueType::Todo => "TODO",
59             IssueType::Fixme => "FIXME",
60         };
61         let details = if self.missing_number {
62             " without issue number"
63         } else {
64             ""
65         };
66
67         write!(fmt, "{}{}", msg, details)
68     }
69 }
70
71 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
72 enum IssueType {
73     Todo,
74     Fixme,
75 }
76
77 enum IssueClassification {
78     Good,
79     Bad(Issue),
80     None,
81 }
82
83 pub struct BadIssueSeeker {
84     state: Seeking,
85     report_todo: ReportTactic,
86     report_fixme: ReportTactic,
87 }
88
89 impl BadIssueSeeker {
90     pub fn new(report_todo: ReportTactic, report_fixme: ReportTactic) -> BadIssueSeeker {
91         BadIssueSeeker {
92             state: Seeking::Issue {
93                 todo_idx: 0,
94                 fixme_idx: 0,
95             },
96             report_todo: report_todo,
97             report_fixme: report_fixme,
98         }
99     }
100
101     // Check whether or not the current char is conclusive evidence for an
102     // unnumbered TO-DO or FIX-ME.
103     pub fn inspect(&mut self, c: char) -> Option<Issue> {
104         match self.state {
105             Seeking::Issue { todo_idx, fixme_idx } => {
106                 self.state = self.inspect_issue(c, todo_idx, fixme_idx);
107             }
108             Seeking::Number { issue, part } => {
109                 let result = self.inspect_number(c, issue, part);
110
111                 if let IssueClassification::None = result {
112                     return None;
113                 }
114
115                 self.state = Seeking::Issue {
116                     todo_idx: 0,
117                     fixme_idx: 0,
118                 };
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 {
171             todo_idx: todo_idx,
172             fixme_idx: fixme_idx,
173         }
174     }
175
176     fn inspect_number(&mut self,
177                       c: char,
178                       issue: Issue,
179                       mut part: NumberPart)
180                       -> IssueClassification {
181         if !issue.missing_number || c == '\n' {
182             return IssueClassification::Bad(issue);
183         } else if c == ')' {
184             return if let NumberPart::CloseParen = part {
185                 IssueClassification::Good
186             } else {
187                 IssueClassification::Bad(issue)
188             };
189         }
190
191         match part {
192             NumberPart::OpenParen => {
193                 if c != '(' {
194                     return IssueClassification::Bad(issue);
195                 } else {
196                     part = NumberPart::Pound;
197                 }
198             }
199             NumberPart::Pound => {
200                 if c == '#' {
201                     part = NumberPart::Number;
202                 }
203             }
204             NumberPart::Number => {
205                 if c >= '0' && c <= '9' {
206                     part = NumberPart::CloseParen;
207                 } else {
208                     return IssueClassification::Bad(issue);
209                 }
210             }
211             NumberPart::CloseParen => {}
212         }
213
214         self.state = Seeking::Number {
215             part: part,
216             issue: issue,
217         };
218
219         IssueClassification::None
220     }
221 }
222
223 #[test]
224 fn find_unnumbered_issue() {
225     fn check_fail(text: &str, failing_pos: usize) {
226         let mut seeker = BadIssueSeeker::new(ReportTactic::Unnumbered, ReportTactic::Unnumbered);
227         assert_eq!(Some(failing_pos),
228                    text.chars().position(|c| seeker.inspect(c).is_some()));
229     }
230
231     fn check_pass(text: &str) {
232         let mut seeker = BadIssueSeeker::new(ReportTactic::Unnumbered, ReportTactic::Unnumbered);
233         assert_eq!(None, text.chars().position(|c| seeker.inspect(c).is_some()));
234     }
235
236     check_fail("TODO\n", 4);
237     check_pass(" TO FIX DOME\n");
238     check_fail(" \n FIXME\n", 8);
239     check_fail("FIXME(\n", 6);
240     check_fail("FIXME(#\n", 7);
241     check_fail("FIXME(#1\n", 8);
242     check_fail("FIXME(#)1\n", 7);
243     check_pass("FIXME(#1222)\n");
244     check_fail("FIXME(#12\n22)\n", 9);
245     check_pass("FIXME(@maintainer, #1222, hello)\n");
246     check_fail("TODO(#22) FIXME\n", 15);
247 }
248
249 #[test]
250 fn find_issue() {
251     fn is_bad_issue(text: &str, report_todo: ReportTactic, report_fixme: ReportTactic) -> bool {
252         let mut seeker = BadIssueSeeker::new(report_todo, report_fixme);
253         text.chars().any(|c| seeker.inspect(c).is_some())
254     }
255
256     assert!(is_bad_issue("TODO(@maintainer, #1222, hello)\n",
257                          ReportTactic::Always,
258                          ReportTactic::Never));
259
260     assert!(!is_bad_issue("TODO: no number\n",
261                           ReportTactic::Never,
262                           ReportTactic::Always));
263
264     assert!(is_bad_issue("This is a FIXME(#1)\n",
265                          ReportTactic::Never,
266                          ReportTactic::Always));
267
268     assert!(!is_bad_issue("bad FIXME\n", ReportTactic::Always, ReportTactic::Never));
269 }
270
271 #[test]
272 fn issue_type() {
273     let mut seeker = BadIssueSeeker::new(ReportTactic::Always, ReportTactic::Never);
274     let expected = Some(Issue {
275         issue_type: IssueType::Todo,
276         missing_number: false,
277     });
278
279     assert_eq!(expected,
280                "TODO(#100): more awesomeness"
281                    .chars()
282                    .map(|c| seeker.inspect(c))
283                    .find(Option::is_some)
284                    .unwrap());
285
286     let mut seeker = BadIssueSeeker::new(ReportTactic::Never, ReportTactic::Unnumbered);
287     let expected = Some(Issue {
288         issue_type: IssueType::Fixme,
289         missing_number: true,
290     });
291
292     assert_eq!(expected,
293                "Test. FIXME: bad, bad, not good"
294                    .chars()
295                    .map(|c| seeker.inspect(c))
296                    .find(Option::is_some)
297                    .unwrap());
298 }