]> git.lizzy.rs Git - rust.git/blob - src/issues.rs
Reformat source code using new defaults
[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         *self != ReportTactic::Never
26     }
27 }
28
29 #[derive(Clone, Copy)]
30 enum Seeking {
31     Issue { todo_idx: usize, fixme_idx: usize },
32     Number { issue: Issue, part: NumberPart },
33 }
34
35 #[derive(Clone, Copy)]
36 enum NumberPart {
37     OpenParen,
38     Pound,
39     Number,
40     CloseParen,
41 }
42
43 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
44 pub struct Issue {
45     issue_type: IssueType,
46     // Indicates whether we're looking for issues with missing numbers, or
47     // all issues of this type.
48     missing_number: bool,
49 }
50
51 impl fmt::Display for Issue {
52     fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
53         let msg = match self.issue_type {
54             IssueType::Todo => "TODO",
55             IssueType::Fixme => "FIXME",
56         };
57         let details = if self.missing_number {
58             " without issue number"
59         } else {
60             ""
61         };
62
63         write!(fmt, "{}{}", msg, details)
64     }
65 }
66
67 #[derive(PartialEq, Eq, Debug, Clone, Copy)]
68 enum IssueType {
69     Todo,
70     Fixme,
71 }
72
73 enum IssueClassification {
74     Good,
75     Bad(Issue),
76     None,
77 }
78
79 pub struct BadIssueSeeker {
80     state: Seeking,
81     report_todo: ReportTactic,
82     report_fixme: ReportTactic,
83 }
84
85 impl BadIssueSeeker {
86     pub fn new(report_todo: ReportTactic, report_fixme: ReportTactic) -> BadIssueSeeker {
87         BadIssueSeeker {
88             state: Seeking::Issue {
89                 todo_idx: 0,
90                 fixme_idx: 0,
91             },
92             report_todo: report_todo,
93             report_fixme: report_fixme,
94         }
95     }
96
97     // Check whether or not the current char is conclusive evidence for an
98     // unnumbered TO-DO or FIX-ME.
99     pub fn inspect(&mut self, c: char) -> Option<Issue> {
100         match self.state {
101             Seeking::Issue {
102                 todo_idx,
103                 fixme_idx,
104             } => {
105                 self.state = self.inspect_issue(c, todo_idx, fixme_idx);
106             }
107             Seeking::Number { issue, part } => {
108                 let result = self.inspect_number(c, issue, part);
109
110                 if let IssueClassification::None = result {
111                     return None;
112                 }
113
114                 self.state = Seeking::Issue {
115                     todo_idx: 0,
116                     fixme_idx: 0,
117                 };
118
119                 if let IssueClassification::Bad(issue) = result {
120                     return Some(issue);
121                 }
122             }
123         }
124
125         None
126     }
127
128     fn inspect_issue(&mut self, c: char, mut todo_idx: usize, mut fixme_idx: usize) -> Seeking {
129         // FIXME: Should we also check for lower case characters?
130         if self.report_todo.is_enabled() && c == TO_DO_CHARS[todo_idx] {
131             todo_idx += 1;
132             if todo_idx == TO_DO_CHARS.len() {
133                 return Seeking::Number {
134                     issue: Issue {
135                         issue_type: IssueType::Todo,
136                         missing_number: if let ReportTactic::Unnumbered = self.report_todo {
137                             true
138                         } else {
139                             false
140                         },
141                     },
142                     part: NumberPart::OpenParen,
143                 };
144             }
145             fixme_idx = 0;
146         } else if self.report_fixme.is_enabled() && c == FIX_ME_CHARS[fixme_idx] {
147             // Exploit the fact that the character sets of todo and fixme
148             // are disjoint by adding else.
149             fixme_idx += 1;
150             if fixme_idx == FIX_ME_CHARS.len() {
151                 return Seeking::Number {
152                     issue: Issue {
153                         issue_type: IssueType::Fixme,
154                         missing_number: if let ReportTactic::Unnumbered = self.report_fixme {
155                             true
156                         } else {
157                             false
158                         },
159                     },
160                     part: NumberPart::OpenParen,
161                 };
162             }
163             todo_idx = 0;
164         } else {
165             todo_idx = 0;
166             fixme_idx = 0;
167         }
168
169         Seeking::Issue {
170             todo_idx: todo_idx,
171             fixme_idx: fixme_idx,
172         }
173     }
174
175     fn inspect_number(
176         &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!(
228             Some(failing_pos),
229             text.chars().position(|c| seeker.inspect(c).is_some())
230         );
231     }
232
233     fn check_pass(text: &str) {
234         let mut seeker = BadIssueSeeker::new(ReportTactic::Unnumbered, ReportTactic::Unnumbered);
235         assert_eq!(None, text.chars().position(|c| seeker.inspect(c).is_some()));
236     }
237
238     check_fail("TODO\n", 4);
239     check_pass(" TO FIX DOME\n");
240     check_fail(" \n FIXME\n", 8);
241     check_fail("FIXME(\n", 6);
242     check_fail("FIXME(#\n", 7);
243     check_fail("FIXME(#1\n", 8);
244     check_fail("FIXME(#)1\n", 7);
245     check_pass("FIXME(#1222)\n");
246     check_fail("FIXME(#12\n22)\n", 9);
247     check_pass("FIXME(@maintainer, #1222, hello)\n");
248     check_fail("TODO(#22) FIXME\n", 15);
249 }
250
251 #[test]
252 fn find_issue() {
253     fn is_bad_issue(text: &str, report_todo: ReportTactic, report_fixme: ReportTactic) -> bool {
254         let mut seeker = BadIssueSeeker::new(report_todo, report_fixme);
255         text.chars().any(|c| seeker.inspect(c).is_some())
256     }
257
258     assert!(is_bad_issue(
259         "TODO(@maintainer, #1222, hello)\n",
260         ReportTactic::Always,
261         ReportTactic::Never,
262     ));
263
264     assert!(!is_bad_issue(
265         "TODO: no number\n",
266         ReportTactic::Never,
267         ReportTactic::Always,
268     ));
269
270     assert!(is_bad_issue(
271         "This is a FIXME(#1)\n",
272         ReportTactic::Never,
273         ReportTactic::Always,
274     ));
275
276     assert!(!is_bad_issue(
277         "bad FIXME\n",
278         ReportTactic::Always,
279         ReportTactic::Never,
280     ));
281 }
282
283 #[test]
284 fn issue_type() {
285     let mut seeker = BadIssueSeeker::new(ReportTactic::Always, ReportTactic::Never);
286     let expected = Some(Issue {
287         issue_type: IssueType::Todo,
288         missing_number: false,
289     });
290
291     assert_eq!(
292         expected,
293         "TODO(#100): more awesomeness"
294             .chars()
295             .map(|c| seeker.inspect(c))
296             .find(Option::is_some)
297             .unwrap()
298     );
299
300     let mut seeker = BadIssueSeeker::new(ReportTactic::Never, ReportTactic::Unnumbered);
301     let expected = Some(Issue {
302         issue_type: IssueType::Fixme,
303         missing_number: true,
304     });
305
306     assert_eq!(
307         expected,
308         "Test. FIXME: bad, bad, not good"
309             .chars()
310             .map(|c| seeker.inspect(c))
311             .find(Option::is_some)
312             .unwrap()
313     );
314 }