Repository: denoland/deno
File: cli/tools/coverage/reporter.rs (Rust)

Code before:
    println!("{}", separator);
    println!("{}", header);
    println!("{}", separator);
    entries.iter().for_each(|(node, stats)| {
      self.print_coverage_line(node, node_max, stats);
    });
    println!("{}", separator);
    self.print_coverage_line("All files", node_max, root_stats);
    println!("{}", separator);
  }
}

pub struct LcovCoverageReporter {}

impl CoverageReporter for LcovCoverageReporter {
  fn done(
    &self,
    _coverage_root: &Path,
    file_reports: &[(CoverageReport, String)],
  ) {
    file_reports.iter().for_each(|(report, file_text)| {
      self.report(report, file_text).unwrap();
    });
    if let Some((report, _)) = file_reports.first() {
      if let Some(ref output) = report.output {
        let path = output.canonicalize().unwrap().to_string_lossy().to_string();
        let url = Url::from_file_path(path).unwrap();
        log::info!("Lcov coverage report has been generated at {}", url);
      }
    }
  }
}

impl LcovCoverageReporter {
  pub fn new() -> LcovCoverageReporter {
    LcovCoverageReporter {}
  }

  fn report(
    &self,
    coverage_report: &CoverageReport,
    _file_text: &str,
  ) -> Result<(), AnyError> {
    // pipes output to stdout if no file is specified
    let out_mode: Result<Box<dyn Write>, Error> = match coverage_report.output {
      // only append to the file as the file should be created already
      Some(ref path) => File::options()
        .append(true)
        .open(path)
        .map(|f| Box::new(f) as Box<dyn Write>),
      None => Ok(Box::new(io::stdout())),

Review comment (bug):
This unwrap is sus here 😬 maybe consider failing gracefully with an error message? Also you don't need to do the conversion to string first - `Url::from_file_path` will accept a `PathBuf`

Code after:
    println!("{}", separator);
    println!("{}", header);
    println!("{}", separator);
    entries.iter().for_each(|(node, stats)| {
      self.print_coverage_line(node, node_max, stats);
    });
    println!("{}", separator);
    self.print_coverage_line("All files", node_max, root_stats);
    println!("{}", separator);
  }
}

pub struct LcovCoverageReporter {}

impl CoverageReporter for LcovCoverageReporter {
  fn done(
    &self,
    _coverage_root: &Path,
    file_reports: &[(CoverageReport, String)],
  ) {
    file_reports.iter().for_each(|(report, file_text)| {
      self.report(report, file_text).unwrap();
    });
    if let Some((report, _)) = file_reports.first() {
      if let Some(ref output) = report.output {
        if let Ok(path) = output.canonicalize() {
          let url = Url::from_file_path(path).unwrap();
          log::info!("Lcov coverage report has been generated at {}", url);
        } else {
          log::error!(
            "Failed to resolve the output path of Lcov report: {}",
            output.display()
          );
        }
      }
    }
  }
}

impl LcovCoverageReporter {
  pub fn new() -> LcovCoverageReporter {
    LcovCoverageReporter {}
  }

  fn report(
    &self,
    coverage_report: &CoverageReport,
    _file_text: &str,
  ) -> Result<(), AnyError> {
    // pipes output to stdout if no file is specified
    let out_mode: Result<Box<dyn Write>, Error> = match coverage_report.output {