(&self)
| 197 | } |
| 198 | |
| 199 | pub fn state(&self) -> State { |
| 200 | let mut source_file = |
| 201 | File::open(&self.path).expect("We were unable to open the exercise file!"); |
| 202 | |
| 203 | let source = { |
| 204 | let mut s = String::new(); |
| 205 | source_file |
| 206 | .read_to_string(&mut s) |
| 207 | .expect("We were unable to read the exercise file!"); |
| 208 | s |
| 209 | }; |
| 210 | |
| 211 | let re = Regex::new(I_AM_DONE_REGEX).unwrap(); |
| 212 | |
| 213 | if !re.is_match(&source) { |
| 214 | return State::Done; |
| 215 | } |
| 216 | |
| 217 | let matched_line_index = source |
| 218 | .lines() |
| 219 | .enumerate() |
| 220 | .find_map(|(i, line)| if re.is_match(line) { Some(i) } else { None }) |
| 221 | .expect("This should not happen at all"); |
| 222 | |
| 223 | let min_line = ((matched_line_index as i32) - (CONTEXT as i32)).max(0) as usize; |
| 224 | let max_line = matched_line_index + CONTEXT; |
| 225 | |
| 226 | let context = source |
| 227 | .lines() |
| 228 | .enumerate() |
| 229 | .filter(|&(i, _)| i >= min_line && i <= max_line) |
| 230 | .map(|(i, line)| ContextLine { |
| 231 | line: line.to_string(), |
| 232 | number: i + 1, |
| 233 | important: i == matched_line_index, |
| 234 | }) |
| 235 | .collect(); |
| 236 | |
| 237 | State::Pending(context) |
| 238 | } |
| 239 | |
| 240 | // Check that the exercise looks to be solved using self.state() |
| 241 | // This is not the best way to check since |
no outgoing calls