|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +use anyhow::Result; |
| 5 | +use coverage::binary::{BinaryCoverage, Count, ModuleBinaryCoverage}; |
| 6 | +use debuggable_module::path::FilePath; |
| 7 | +use debuggable_module::Offset; |
| 8 | +use serde::{Deserialize, Serialize}; |
| 9 | + |
| 10 | +#[derive(Deserialize, Serialize)] |
| 11 | +#[serde(transparent)] |
| 12 | +pub struct BinaryCoverageJson { |
| 13 | + #[serde(flatten)] |
| 14 | + pub modules: Vec<BinaryModuleCoverageJson>, |
| 15 | +} |
| 16 | + |
| 17 | +#[derive(Deserialize, Serialize)] |
| 18 | +pub struct BinaryModuleCoverageJson { |
| 19 | + pub module: String, |
| 20 | + pub blocks: Vec<BinaryBlockCoverageJson>, |
| 21 | +} |
| 22 | + |
| 23 | +#[derive(Deserialize, Serialize)] |
| 24 | +pub struct BinaryBlockCoverageJson { |
| 25 | + pub offset: u32, |
| 26 | + pub count: u32, |
| 27 | +} |
| 28 | + |
| 29 | +impl TryFrom<BinaryCoverage> for BinaryCoverageJson { |
| 30 | + type Error = anyhow::Error; |
| 31 | + |
| 32 | + fn try_from(binary: BinaryCoverage) -> Result<Self> { |
| 33 | + let mut modules = Vec::new(); |
| 34 | + |
| 35 | + for (module, offsets) in binary.modules { |
| 36 | + let mut blocks = Vec::new(); |
| 37 | + |
| 38 | + for (offset, count) in offsets.as_ref() { |
| 39 | + let offset = u32::try_from(offset.0)?; |
| 40 | + let count = count.0; |
| 41 | + let block = BinaryBlockCoverageJson { offset, count }; |
| 42 | + blocks.push(block); |
| 43 | + } |
| 44 | + |
| 45 | + let module = module.as_str().to_owned(); |
| 46 | + let module = BinaryModuleCoverageJson { module, blocks }; |
| 47 | + |
| 48 | + modules.push(module); |
| 49 | + } |
| 50 | + |
| 51 | + Ok(Self { modules }) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +impl TryFrom<BinaryCoverageJson> for BinaryCoverage { |
| 56 | + type Error = anyhow::Error; |
| 57 | + |
| 58 | + fn try_from(json: BinaryCoverageJson) -> Result<Self> { |
| 59 | + let mut process = BinaryCoverage::default(); |
| 60 | + |
| 61 | + for coverage_json in json.modules { |
| 62 | + let mut coverage = ModuleBinaryCoverage::default(); |
| 63 | + |
| 64 | + for block in coverage_json.blocks { |
| 65 | + let offset = Offset(u64::from(block.offset)); |
| 66 | + let count = Count(block.count); |
| 67 | + coverage.offsets.insert(offset, count); |
| 68 | + } |
| 69 | + |
| 70 | + let path = FilePath::new(coverage_json.module)?; |
| 71 | + |
| 72 | + process.modules.insert(path, coverage); |
| 73 | + } |
| 74 | + |
| 75 | + Ok(process) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +#[cfg(test)] |
| 80 | +mod tests; |
0 commit comments