(content: &str, output: String)
| 65 | pub fn process(content: &str, output: String) -> Result<(), ProcessorError> { |
| 66 | let instructions = get_instructions(output); |
| 67 | let mut active_handlers = HashMap::<String, Box<dyn InstructionLineHandler>>::new(); |
| 68 | |
| 69 | for (line_number, line) in content.lines().enumerate() { |
| 70 | let captures = INSTRUCTION_LINE_REGEX.find(line); |
| 71 | match captures { |
| 72 | None => { |
| 73 | for (_, h) in active_handlers.iter() { |
| 74 | h.handle_line(line)?; |
| 75 | } |
| 76 | } |
| 77 | Some(_match) => { |
| 78 | let net_line = line.trim_start_matches('#').trim_start(); |
| 79 | let is_closing = net_line.starts_with('/'); |
| 80 | let net_line = net_line.trim_start_matches('/').trim_start(); |
| 81 | |
| 82 | let mut words = net_line.split(' ').map(|s| s.trim()).filter(|s| !s.is_empty()); |
| 83 | let instruction_name = words |
| 84 | .next() |
| 85 | .ok_or_else(|| ProcessorError::InstructionNotFound(line_number, line.into()))? |
| 86 | .to_uppercase(); |
| 87 | |
| 88 | let instruction = instructions |
| 89 | .get(&instruction_name.as_ref()) |
| 90 | .ok_or_else(|| ProcessorError::InstructionNotExisting(instruction_name.clone(), line_number, line.into()))?; |
| 91 | |
| 92 | match (is_closing, instruction.needs_closing()) { |
| 93 | (true, false) => { |
| 94 | return Err(ProcessorError::ClosingTagFound(instruction_name, line_number, line.into())); |
| 95 | } |
| 96 | (true, true) => { |
| 97 | active_handlers |
| 98 | .remove(&instruction_name) |
| 99 | .ok_or_else(|| ProcessorError::MissingOpeningTag(instruction_name, line_number, line.into()))?; |
| 100 | } |
| 101 | (false, _) => { |
| 102 | let handler = instruction.start(words.map(Into::into).collect())?; |
| 103 | if instruction.needs_closing() { |
| 104 | active_handlers.insert(instruction_name, handler); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | Ok(()) |
| 113 | } |
| 114 | |
| 115 | #[cfg(test)] |
| 116 | mod test { |
| 117 | use super::*; |
| 118 |
no test coverage detected