use std::collections::BTreeMap;
use oxc_coverage_types::{BranchEntry, FileCoverage, FnEntry, Location};
#[expect(
clippy::redundant_pub_crate,
reason = "crate-internal type intentionally; the explicit pub(crate) documents that this is not part of the public API even though the parent module is already private"
)]
pub(crate) struct CoverageMaps {
pub(crate) path: String,
pub(crate) statement_locs: Vec<Location>,
pub(crate) fn_entries: Vec<FnEntry>,
pub(crate) branch_entries: Vec<BranchEntry>,
pub(crate) logical_branch_ids: Vec<usize>,
}
#[expect(
clippy::redundant_pub_crate,
reason = "crate-internal function intentionally; the explicit pub(crate) documents that this is not part of the public API even though the parent module is already private"
)]
pub(crate) fn build_file_coverage(maps: CoverageMaps) -> FileCoverage {
let CoverageMaps { path, statement_locs, fn_entries, branch_entries, logical_branch_ids } =
maps;
let statement_map: BTreeMap<String, Location> =
statement_locs.into_iter().enumerate().map(|(i, loc)| (i.to_string(), loc)).collect();
let fn_map: BTreeMap<String, FnEntry> =
fn_entries.into_iter().enumerate().map(|(i, e)| (i.to_string(), e)).collect();
let branch_map: BTreeMap<String, BranchEntry> = branch_entries
.into_iter()
.enumerate()
.filter(|(_, entry)| !entry.locations.is_empty())
.map(|(i, entry)| (i.to_string(), entry))
.collect();
let s = statement_map.keys().map(|k| (k.clone(), 0)).collect();
let f = fn_map.keys().map(|k| (k.clone(), 0)).collect();
let b =
branch_map.iter().map(|(k, entry)| (k.clone(), vec![0; entry.locations.len()])).collect();
let b_t = if logical_branch_ids.is_empty() {
None
} else {
Some(
logical_branch_ids
.iter()
.filter_map(|&id| {
let key = id.to_string();
let len = branch_map.get(&key)?.locations.len();
Some((key, vec![0; len]))
})
.collect(),
)
};
FileCoverage { path, statement_map, fn_map, branch_map, s, f, b, b_t, input_source_map: None }
}