From b995cc25ad866c74306ccc0feaf67e2131974c2f Mon Sep 17 00:00:00 2001 From: Joe Ranweiler Date: Wed, 4 Jan 2023 10:54:20 -0800 Subject: [PATCH] Add more into-JSON coverage conversions (#2725) --- .../src/coverage/binary.rs | 18 ++++++++++++++ .../src/coverage/source.rs | 18 ++++++++++++++ .../src/coverage/source/v1.rs | 24 +++++++++++++++++-- 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/agent/onefuzz-file-format/src/coverage/binary.rs b/src/agent/onefuzz-file-format/src/coverage/binary.rs index 8918612d09..5fc6346536 100644 --- a/src/agent/onefuzz-file-format/src/coverage/binary.rs +++ b/src/agent/onefuzz-file-format/src/coverage/binary.rs @@ -31,6 +31,24 @@ impl BinaryCoverageJson { } } +// Convert into the latest format. +impl From for BinaryCoverageJson { + fn from(source: BinaryCoverage) -> Self { + v1::BinaryCoverageJson::from(source).into() + } +} + +impl From for BinaryCoverageJson { + fn from(v0: v0::BinaryCoverageJson) -> Self { + Self::V0(v0) + } +} +impl From for BinaryCoverageJson { + fn from(v1: v1::BinaryCoverageJson) -> Self { + Self::V1(v1) + } +} + impl TryFrom for BinaryCoverage { type Error = anyhow::Error; diff --git a/src/agent/onefuzz-file-format/src/coverage/source.rs b/src/agent/onefuzz-file-format/src/coverage/source.rs index 6dd246b2ec..74e7adcb10 100644 --- a/src/agent/onefuzz-file-format/src/coverage/source.rs +++ b/src/agent/onefuzz-file-format/src/coverage/source.rs @@ -31,6 +31,24 @@ impl SourceCoverageJson { } } +// Convert into the latest format. +impl From for SourceCoverageJson { + fn from(source: SourceCoverage) -> Self { + v1::SourceCoverageJson::from(source).into() + } +} + +impl From for SourceCoverageJson { + fn from(v0: v0::SourceCoverageJson) -> Self { + Self::V0(v0) + } +} +impl From for SourceCoverageJson { + fn from(v1: v1::SourceCoverageJson) -> Self { + Self::V1(v1) + } +} + impl TryFrom for SourceCoverage { type Error = anyhow::Error; diff --git a/src/agent/onefuzz-file-format/src/coverage/source/v1.rs b/src/agent/onefuzz-file-format/src/coverage/source/v1.rs index c1eb87d109..3c2843d56d 100644 --- a/src/agent/onefuzz-file-format/src/coverage/source/v1.rs +++ b/src/agent/onefuzz-file-format/src/coverage/source/v1.rs @@ -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, } -#[derive(Deserialize, Serialize)] +#[derive(Default, Deserialize, Serialize)] pub struct FileCoverageJson { pub lines: BTreeMap, } +impl From 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 for SourceCoverage { type Error = anyhow::Error;