Day 8: Blitzen's Debug Blitz

The North Poleā€™s DevOps room was unusually quietā€”well, as quiet as it could be with Blitzen strutting around like he owned the place. The reindeer had declared himself Tech Leadā„¢ (again), and with Santa still off on his "vision quest", nobody had dared challenge him. Every five minutes, Blitzen would casually remind the room, "Iā€™m the Tech Lead, in case anyone forgot."

The elves, however, had not forgotten. Frostbyte, the teamā€™s fastest typist, was already regretting showing up for work today.


The Discovery of Errors

It all started when Blitzen stumbled upon a string of unusual log entries:

ERROR: Toy Tracker 3000 overheating.
ERROR: SleighOS failed to authenticate.
ERROR: Reindeer AI unable to locate Prancer.

He squinted at the terminal, his antlers practically buzzing with excitement. ā€œThisā€¦ this is big,ā€ Blitzen declared dramatically. ā€œThese errors need to be isolated, analyzed, and stored in their own file. This could save Christmas!ā€

Prancer, barely looking up from their desk, muttered, ā€œCouldnā€™t we just pipe the logs through grep ERROR and append it to a file with >> file.log?ā€

Blitzen whipped around, scandalized. ā€œPrancer, what part of 'Iā€™m the Tech Lead' didnā€™t you understand? We donā€™t use grep here. We build solutions. Innovative solutions. In Rust!ā€

ā€œHereā€™s the plan,ā€ Blitzen said, pacing like a founder pitching to VCs. ā€œWeā€™ll extend our LogQuery tool to not just search logs, but also export specific entries to a file. A Rustacean-grade solution, not some bash script hack job.ā€

An elf raised their hand timidly. ā€œBut why?ā€

Blitzen grinned. ā€œBecause Iā€™m the Tech Lead.ā€


Your Mission

You must come to the elves rescue! Implement the export_to_file(&self, keyword: &str, file_path: &str) method in LogQuery that:

  1. Use the search method we created earlier to get logs matching the keyword.
  2. Loop over the returned logs and write them to a file, the location is given by the parameter path.
  3. Handles Errors: If the file canā€™t be created or written to, the function should return an appropriate error instead of crashing.

Hints

If youā€™re stuck or need a starting point, here are some hints to help you along the way!

Click here to show hints.
  • Make sure you import the necessary modules. e.g., use std::{fs::File, io::Write};.

  • Get the logs using the search method we created in the previous challenge. e.g. let logs = self.search(keyword);.

  • Create a mutable file using File::create. e.g. let mut file = File::create(path)?;

  • Properly handle errors with Result and ? to propagate them.

  • Loop over the logs and use the writeln! macro to write to the file. e.g.

    for log in logs {
        writeln!(file, "{}", log)?;
    }
  • Return Ok(()) if everything goes well.