]> git.lizzy.rs Git - rust.git/blob - src/issues.rs
Merge pull request #324 from marcusklaas/macro-uses
[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 { todo_idx: 0, fixme_idx: 0 },
105             report_todo: report_todo,
106             report_fixme: report_fixme,
107         }
108     }
109
110     // Check whether or not the current char is conclusive evidence for an
111     // unnumbered TO-DO or FIX-ME.
112     pub fn inspect(&mut self, c: char) -> Option<Issue> {
113         match self.state {
114             Seeking::Issue { todo_idx, fixme_idx } => {
115                 self.state = self.inspect_issue(c, todo_idx, fixme_idx);
116             }
117             Seeking::Number { issue, part } => {
118                 let result = self.inspect_number(c, issue, part);
119
120                 if let IssueClassification::None = result {
121                     return None;
122                 }
123
124                 self.state = Seeking::Issue { todo_idx: 0, fixme_idx: 0 };
125
126                 if let IssueClassification::Bad(issue) = result {
127                     return Some(issue);
128                 }
129             }
130         }
131
132         None
133     }
134
135     fn inspect_issue(&mut self, c: char, mut todo_idx: usize, mut fixme_idx: usize) -> Seeking {
136         // FIXME: Should we also check for lower case characters?
137         if self.report_todo.is_enabled() && c == TO_DO_CHARS[todo_idx] {
138             todo_idx += 1;
139             if todo_idx == TO_DO_CHARS.len() {
140                 return Seeking::Number {
141                     issue: Issue {
142                         issue_type: IssueType::Todo,
143                         missing_number: if let ReportTactic::Unnumbered = self.report_todo {
144                             true
145                         } else {
146                             false
147                         }
148                     },
149                     part: NumberPart::OpenParen
150                 };
151             }
152             fixme_idx = 0;
153         } else if self.report_fixme.is_enabled() && c == FIX_ME_CHARS[fixme_idx] {
154             // Exploit the fact that the character sets of todo and fixme
155             // are disjoint by adding else.
156             fixme_idx += 1;
157             if fixme_idx == FIX_ME_CHARS.len() {
158                 return Seeking::Number {
159                     issue: Issue {
160                         issue_type: IssueType::Fixme,
161                         missing_number: if let ReportTactic::Unnumbered = self.report_fixme {
162                             true
163                         } else {
164                             false
165                         }
166                     },
167                     part: NumberPart::OpenParen
168                 };
169             }
170             todo_idx = 0;
171         } else {
172             todo_idx = 0;
173             fixme_idx = 0;
174         }
175
176         Seeking::Issue { todo_idx: todo_idx, fixme_idx: fixme_idx }
177     }
178
179     fn inspect_number(&mut self,
180                       c: char,
181                       issue: Issue,
182                       mut part: NumberPart)
183                       -> IssueClassification {
184         if !issue.missing_number || c == '\n' {
185             return IssueClassification::Bad(issue);
186         } else if c == ')' {
187             return if let NumberPart::CloseParen = part {
188                 IssueClassification::Good
189             } else {
190                 IssueClassification::Bad(issue)
191             };
192         }
193
194         match part {
195             NumberPart::OpenParen => {
196                 if c != '(' {
197                     return IssueClassification::Bad(issue);
198                 } else {
199                     part = NumberPart::Pound;
200                 }
201             }
202             NumberPart::Pound => {
203                 if c == '#' {
204                     part = NumberPart::Number;
205                 }
206             }
207             NumberPart::Number => {
208                 if c >= '0' && c <= '9' {
209                     part = NumberPart::CloseParen;
210                 } else {
211                     return IssueClassification::Bad(issue);
212                 }
213             }
214             NumberPart::CloseParen => {}
215         }
216
217         self.state = Seeking::Number { part: part, issue: issue };
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), text.chars().position(|c| seeker.inspect(c).is_some()));
228     }
229
230     fn check_pass(text: &str) {
231         let mut seeker = BadIssueSeeker::new(ReportTactic::Unnumbered, ReportTactic::Unnumbered);
232         assert_eq!(None, text.chars().position(|c| seeker.inspect(c).is_some()));
233     }
234
235     check_fail("TODO\n", 4);
236     check_pass(" TO FIX DOME\n");
237     check_fail(" \n FIXME\n", 8);
238     check_fail("FIXME(\n", 6);
239     check_fail("FIXME(#\n", 7);
240     check_fail("FIXME(#1\n", 8);
241     check_fail("FIXME(#)1\n", 7);
242     check_pass("FIXME(#1222)\n");
243     check_fail("FIXME(#12\n22)\n", 9);
244     check_pass("FIXME(@maintainer, #1222, hello)\n");
245     check_fail("TODO(#22) FIXME\n", 15);
246 }
247
248 #[test]
249 fn find_issue() {
250     fn is_bad_issue(text: &str, report_todo: ReportTactic, report_fixme: ReportTactic) -> bool {
251         let mut seeker = BadIssueSeeker::new(report_todo, report_fixme);
252         text.chars().any(|c| seeker.inspect(c).is_some())
253     }
254
255     assert!(is_bad_issue("TODO(@maintainer, #1222, hello)\n",
256                          ReportTactic::Always,
257                          ReportTactic::Never));
258
259     assert!(!is_bad_issue("TODO: no number\n", ReportTactic::Never, ReportTactic::Always));
260
261     assert!(is_bad_issue("This is a FIXME(#1)\n", ReportTactic::Never, ReportTactic::Always));
262
263     assert!(!is_bad_issue("bad FIXME\n", ReportTactic::Always, ReportTactic::Never));
264 }
265
266 #[test]
267 fn issue_type() {
268     let mut seeker = BadIssueSeeker::new(ReportTactic::Always, ReportTactic::Never);
269     let expected = Some(Issue { issue_type: IssueType::Todo, missing_number: false });
270
271     assert_eq!(expected,
272                "TODO(#100): more awesomeness"
273                    .chars()
274                    .map(|c| seeker.inspect(c))
275                    .find(Option::is_some)
276                    .unwrap());
277
278     let mut seeker = BadIssueSeeker::new(ReportTactic::Never, ReportTactic::Unnumbered);
279     let expected = Some(Issue { issue_type: IssueType::Fixme, missing_number: true });
280
281     assert_eq!(expected,
282                "Test. FIXME: bad, bad, not good"
283                    .chars()
284                    .map(|c| seeker.inspect(c))
285                    .find(Option::is_some)
286                    .unwrap());
287 }