]> git.lizzy.rs Git - rust.git/blob - src/issues.rs
2a5eaae287a16cec3ac8f98fb43e272c6245ee0d
[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 {
74             " without issue number"
75         } else {
76             ""
77         };
78
79         write!(fmt, "{}{}", msg, details)
80     }
81 }
82
83 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
84 enum IssueType {
85     Todo,
86     Fixme,
87 }
88
89 enum IssueClassification {
90     Good,
91     Bad(Issue),
92     None,
93 }
94
95 pub struct BadIssueSeeker {
96     state: Seeking,
97     report_todo: ReportTactic,
98     report_fixme: ReportTactic,
99 }
100
101 impl BadIssueSeeker {
102     pub fn new(report_todo: ReportTactic, report_fixme: ReportTactic) -> BadIssueSeeker {
103         BadIssueSeeker {
104             state: Seeking::Issue {
105                 todo_idx: 0,
106                 fixme_idx: 0,
107             },
108             report_todo: report_todo,
109             report_fixme: report_fixme,
110         }
111     }
112
113     // Check whether or not the current char is conclusive evidence for an
114     // unnumbered TO-DO or FIX-ME.
115     pub fn inspect(&mut self, c: char) -> Option<Issue> {
116         match self.state {
117             Seeking::Issue { todo_idx, fixme_idx } => {
118                 self.state = self.inspect_issue(c, todo_idx, fixme_idx);
119             }
120             Seeking::Number { issue, part } => {
121                 let result = self.inspect_number(c, issue, part);
122
123                 if let IssueClassification::None = result {
124                     return None;
125                 }
126
127                 self.state = Seeking::Issue {
128                     todo_idx: 0,
129                     fixme_idx: 0,
130                 };
131
132                 if let IssueClassification::Bad(issue) = result {
133                     return Some(issue);
134                 }
135             }
136         }
137
138         None
139     }
140
141     fn inspect_issue(&mut self, c: char, mut todo_idx: usize, mut fixme_idx: usize) -> Seeking {
142         // FIXME: Should we also check for lower case characters?
143         if self.report_todo.is_enabled() && c == TO_DO_CHARS[todo_idx] {
144             todo_idx += 1;
145             if todo_idx == TO_DO_CHARS.len() {
146                 return Seeking::Number {
147                     issue: Issue {
148                         issue_type: IssueType::Todo,
149                         missing_number: if let ReportTactic::Unnumbered = self.report_todo {
150                             true
151                         } else {
152                             false
153                         },
154                     },
155                     part: NumberPart::OpenParen,
156                 };
157             }
158             fixme_idx = 0;
159         } else if self.report_fixme.is_enabled() && c == FIX_ME_CHARS[fixme_idx] {
160             // Exploit the fact that the character sets of todo and fixme
161             // are disjoint by adding else.
162             fixme_idx += 1;
163             if fixme_idx == FIX_ME_CHARS.len() {
164                 return Seeking::Number {
165                     issue: Issue {
166                         issue_type: IssueType::Fixme,
167                         missing_number: if let ReportTactic::Unnumbered = self.report_fixme {
168                             true
169                         } else {
170                             false
171                         },
172                     },
173                     part: NumberPart::OpenParen,
174                 };
175             }
176             todo_idx = 0;
177         } else {
178             todo_idx = 0;
179             fixme_idx = 0;
180         }
181
182         Seeking::Issue {
183             todo_idx: todo_idx,
184             fixme_idx: fixme_idx,
185         }
186     }
187
188     fn inspect_number(&mut self,
189                       c: char,
190                       issue: Issue,
191                       mut part: NumberPart)
192                       -> IssueClassification {
193         if !issue.missing_number || c == '\n' {
194             return IssueClassification::Bad(issue);
195         } else if c == ')' {
196             return if let NumberPart::CloseParen = part {
197                 IssueClassification::Good
198             } else {
199                 IssueClassification::Bad(issue)
200             };
201         }
202
203         match part {
204             NumberPart::OpenParen => {
205                 if c != '(' {
206                     return IssueClassification::Bad(issue);
207                 } else {
208                     part = NumberPart::Pound;
209                 }
210             }
211             NumberPart::Pound => {
212                 if c == '#' {
213                     part = NumberPart::Number;
214                 }
215             }
216             NumberPart::Number => {
217                 if c >= '0' && c <= '9' {
218                     part = NumberPart::CloseParen;
219                 } else {
220                     return IssueClassification::Bad(issue);
221                 }
222             }
223             NumberPart::CloseParen => {}
224         }
225
226         self.state = Seeking::Number {
227             part: part,
228             issue: issue,
229         };
230
231         IssueClassification::None
232     }
233 }
234
235 #[test]
236 fn find_unnumbered_issue() {
237     fn check_fail(text: &str, failing_pos: usize) {
238         let mut seeker = BadIssueSeeker::new(ReportTactic::Unnumbered, ReportTactic::Unnumbered);
239         assert_eq!(Some(failing_pos),
240                    text.chars().position(|c| seeker.inspect(c).is_some()));
241     }
242
243     fn check_pass(text: &str) {
244         let mut seeker = BadIssueSeeker::new(ReportTactic::Unnumbered, ReportTactic::Unnumbered);
245         assert_eq!(None,
246                    text.chars().position(|c| seeker.inspect(c).is_some()));
247     }
248
249     check_fail("TODO\n", 4);
250     check_pass(" TO FIX DOME\n");
251     check_fail(" \n FIXME\n", 8);
252     check_fail("FIXME(\n", 6);
253     check_fail("FIXME(#\n", 7);
254     check_fail("FIXME(#1\n", 8);
255     check_fail("FIXME(#)1\n", 7);
256     check_pass("FIXME(#1222)\n");
257     check_fail("FIXME(#12\n22)\n", 9);
258     check_pass("FIXME(@maintainer, #1222, hello)\n");
259     check_fail("TODO(#22) FIXME\n", 15);
260 }
261
262 #[test]
263 fn find_issue() {
264     fn is_bad_issue(text: &str, report_todo: ReportTactic, report_fixme: ReportTactic) -> bool {
265         let mut seeker = BadIssueSeeker::new(report_todo, report_fixme);
266         text.chars().any(|c| seeker.inspect(c).is_some())
267     }
268
269     assert!(is_bad_issue("TODO(@maintainer, #1222, hello)\n",
270                          ReportTactic::Always,
271                          ReportTactic::Never));
272
273     assert!(!is_bad_issue("TODO: no number\n",
274                           ReportTactic::Never,
275                           ReportTactic::Always));
276
277     assert!(is_bad_issue("This is a FIXME(#1)\n",
278                          ReportTactic::Never,
279                          ReportTactic::Always));
280
281     assert!(!is_bad_issue("bad FIXME\n",
282                           ReportTactic::Always,
283                           ReportTactic::Never));
284 }
285
286 #[test]
287 fn issue_type() {
288     let mut seeker = BadIssueSeeker::new(ReportTactic::Always, ReportTactic::Never);
289     let expected = Some(Issue {
290         issue_type: IssueType::Todo,
291         missing_number: false,
292     });
293
294     assert_eq!(expected,
295                "TODO(#100): more awesomeness"
296                    .chars()
297                    .map(|c| seeker.inspect(c))
298                    .find(Option::is_some)
299                    .unwrap());
300
301     let mut seeker = BadIssueSeeker::new(ReportTactic::Never, ReportTactic::Unnumbered);
302     let expected = Some(Issue {
303         issue_type: IssueType::Fixme,
304         missing_number: true,
305     });
306
307     assert_eq!(expected,
308                "Test. FIXME: bad, bad, not good"
309                    .chars()
310                    .map(|c| seeker.inspect(c))
311                    .find(Option::is_some)
312                    .unwrap());
313 }