1use crate::parser;
2use crate::Parser;
3use anyhow::{bail, Result};
4use core::str;
5use serde::{Deserialize, Serialize};
6use std::convert::TryFrom;
7use std::path::Path;
8use std::str::FromStr;
9
10#[derive(clap::ValueEnum, Debug, Serialize, Deserialize, Clone, Copy)]
11pub enum Formats {
12 Simplecov,
13 Clover,
14 Cobertura,
15 Coverprofile,
16 Lcov,
17 Jacoco,
18 Qlty,
19}
20
21impl std::fmt::Display for Formats {
22 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
23 match self {
24 Formats::Simplecov => write!(f, "simplecov"),
25 Formats::Clover => write!(f, "clover"),
26 Formats::Cobertura => write!(f, "cobertura"),
27 Formats::Coverprofile => write!(f, "coverprofile"),
28 Formats::Lcov => write!(f, "lcov"),
29 Formats::Jacoco => write!(f, "jacoco"),
30 Formats::Qlty => write!(f, "qlty"),
31 }
32 }
33}
34
35impl TryFrom<&Path> for Formats {
36 type Error = anyhow::Error;
37
38 fn try_from(path: &Path) -> Result<Self> {
39 match path.extension().and_then(std::ffi::OsStr::to_str) {
40 Some("info") => Ok(Formats::Lcov),
41 Some("json") => Ok(Formats::Simplecov),
42 Some("jsonl") => Ok(Formats::Qlty),
43 Some("out") => Ok(Formats::Coverprofile),
44 Some("xml") => {
45 let path_str = path.to_str().unwrap();
46 if path_str.contains("jacoco") {
47 Ok(Formats::Jacoco)
48 } else if path_str.contains("clover") {
49 Ok(Formats::Clover)
50 } else {
51 Ok(Formats::Cobertura)
52 }
53 }
54 _ => bail!(
55 "Unsupported file format for coverage report: {}",
56 path.display()
57 ),
58 }
59 }
60}
61
62impl FromStr for Formats {
63 type Err = anyhow::Error;
64
65 fn from_str(s: &str) -> Result<Self> {
66 match s {
67 "simplecov" => Ok(Formats::Simplecov),
68 "clover" => Ok(Formats::Clover),
69 "cobertura" => Ok(Formats::Cobertura),
70 "coverprofile" => Ok(Formats::Coverprofile),
71 "lcov" => Ok(Formats::Lcov),
72 "jacoco" => Ok(Formats::Jacoco),
73 "qlty" => Ok(Formats::Qlty),
74 _ => bail!("Unsupported coverage report format: {}", s),
75 }
76 }
77}
78
79pub fn parser_for(&format: &Formats) -> Box<dyn Parser> {
80 match format {
81 Formats::Simplecov => Box::new(parser::Simplecov::new()),
82 Formats::Clover => Box::new(parser::Clover::new()),
83 Formats::Cobertura => Box::new(parser::Cobertura::new()),
84 Formats::Coverprofile => Box::new(parser::Coverprofile::new()),
85 Formats::Lcov => Box::new(parser::Lcov::new()),
86 Formats::Jacoco => Box::new(parser::Jacoco::new()),
87 Formats::Qlty => Box::new(parser::Qlty::new()),
88 }
89}