Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

Commit

Permalink
Add more into-JSON coverage conversions (#2725)
Browse files Browse the repository at this point in the history
  • Loading branch information
ranweiler authored Jan 4, 2023
1 parent a8d5ab7 commit b995cc2
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
18 changes: 18 additions & 0 deletions src/agent/onefuzz-file-format/src/coverage/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@ impl BinaryCoverageJson {
}
}

// Convert into the latest format.
impl From<BinaryCoverage> for BinaryCoverageJson {
fn from(source: BinaryCoverage) -> Self {
v1::BinaryCoverageJson::from(source).into()
}
}

impl From<v0::BinaryCoverageJson> for BinaryCoverageJson {
fn from(v0: v0::BinaryCoverageJson) -> Self {
Self::V0(v0)
}
}
impl From<v1::BinaryCoverageJson> for BinaryCoverageJson {
fn from(v1: v1::BinaryCoverageJson) -> Self {
Self::V1(v1)
}
}

impl TryFrom<BinaryCoverageJson> for BinaryCoverage {
type Error = anyhow::Error;

Expand Down
18 changes: 18 additions & 0 deletions src/agent/onefuzz-file-format/src/coverage/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@ impl SourceCoverageJson {
}
}

// Convert into the latest format.
impl From<SourceCoverage> for SourceCoverageJson {
fn from(source: SourceCoverage) -> Self {
v1::SourceCoverageJson::from(source).into()
}
}

impl From<v0::SourceCoverageJson> for SourceCoverageJson {
fn from(v0: v0::SourceCoverageJson) -> Self {
Self::V0(v0)
}
}
impl From<v1::SourceCoverageJson> for SourceCoverageJson {
fn from(v1: v1::SourceCoverageJson) -> Self {
Self::V1(v1)
}
}

impl TryFrom<SourceCoverageJson> for SourceCoverage {
type Error = anyhow::Error;

Expand Down
24 changes: 22 additions & 2 deletions src/agent/onefuzz-file-format/src/coverage/source/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,37 @@ pub type SourceFile = String;
pub type HitCount = u32;
pub use line_number::LineNumber;

#[derive(Deserialize, Serialize)]
#[derive(Default, Deserialize, Serialize)]
pub struct SourceCoverageJson {
#[serde(flatten)]
pub files: BTreeMap<SourceFile, FileCoverageJson>,
}

#[derive(Deserialize, Serialize)]
#[derive(Default, Deserialize, Serialize)]
pub struct FileCoverageJson {
pub lines: BTreeMap<LineNumber, HitCount>,
}

impl From<SourceCoverage> for SourceCoverageJson {
fn from(source: SourceCoverage) -> Self {
let mut json = SourceCoverageJson::default();

for (path, file) in source.files {
let mut file_json = FileCoverageJson::default();

for (line, count) in file.lines {
let line_number = LineNumber(line.number());
let hit_count = count.0;
file_json.lines.insert(line_number, hit_count);
}

json.files.insert(path.to_string(), file_json);
}

json
}
}

impl TryFrom<SourceCoverageJson> for SourceCoverage {
type Error = anyhow::Error;

Expand Down

0 comments on commit b995cc2

Please sign in to comment.