use super::swift::{after_key, is_source, number_of, string_of};
use std::path::{Path, PathBuf};
#[derive(Clone, Copy, Debug)]
pub(super) struct Region {
pub(super) ls: usize,
pub(super) cs: usize,
pub(super) le: usize,
pub(super) ce: usize,
pub(super) count: usize,
pub(super) file: usize,
pub(super) kind: usize,
}
pub(super) const CODE: usize = 0;
pub(super) const SKIPPED: usize = 2;
pub(super) struct Function {
pub(super) name: String,
pub(super) file: String,
pub(super) regions: Vec<Region>,
}
pub(super) fn function_lines(all: &[Region]) -> (usize, usize) {
let regions: Vec<&Region> = all.iter().filter(|r| r.file == 0).collect();
let lo = regions.iter().map(|r| r.ls).min().unwrap_or(1);
let hi = regions.iter().map(|r| r.le).max().unwrap_or(0);
let (mut lines, mut covered) = (0_usize, 0_usize);
for line in lo..=hi {
let first = regions
.iter()
.filter(|r| r.ls == line)
.map(|r| (r.cs, r.kind != SKIPPED))
.chain(
regions
.iter()
.filter(|r| r.le == line)
.map(|r| (r.ce, true)),
)
.min();
if first.is_some_and(|(_, counted)| !counted) {
continue;
}
let wrapped = regions
.iter()
.filter(|r| r.ls < line && line <= r.le)
.max_by_key(|r| (r.ls, r.cs, std::cmp::Reverse((r.le, r.ce))))
.filter(|r| r.kind != SKIPPED)
.map(|r| r.count);
let starts = regions
.iter()
.filter(|r| r.ls == line && r.kind == CODE)
.map(|r| r.count)
.max();
if wrapped.is_none() && starts.is_none() {
continue;
}
lines = lines.saturating_add(1);
if wrapped.unwrap_or(0).max(starts.unwrap_or(0)) > 0 {
covered = covered.saturating_add(1);
}
}
(lines, covered)
}
pub(super) fn relative(
file: &Path,
roots: &[Option<PathBuf>],
) -> Option<String> {
let rel = if file.is_relative() {
file
} else {
roots
.iter()
.flatten()
.find_map(|r| file.strip_prefix(r).ok())?
};
Some(rel.to_string_lossy().into_owned())
}
pub(super) fn source_files(
json: &str,
roots: &[Option<PathBuf>],
) -> Option<Vec<(String, usize, usize)>> {
let mut out = Vec::new();
for entry in elements(array_of(json, "files")?)? {
let (name, _) = string_of(after_key(entry, "filename")?)?;
let file = Path::new(&name);
if !is_source(file, roots) {
continue;
}
let lines = after_key(after_key(entry, "summary")?, "lines")?;
let lines = lines.get(..lines.find('}')?)?;
out.push((
relative(file, roots)?,
number_of(lines, "count")?,
number_of(lines, "covered")?,
));
}
Some(out)
}
pub(super) fn functions(
json: &str,
roots: &[Option<PathBuf>],
wanted: &dyn Fn(&str) -> bool,
) -> Option<Vec<Function>> {
let mut out = Vec::new();
for entry in elements(array_of(json, "functions")?)? {
let first = *elements(after_key(entry, "filenames")?)?.first()?;
let (file, _) = string_of(first)?;
let Some(file) = relative(Path::new(&file), roots) else {
continue;
};
if !wanted(&file) {
continue;
}
let (name, _) = string_of(after_key(entry, "name")?)?;
let mut regions = Vec::new();
for r in elements(after_key(entry, "regions")?)? {
let n: Vec<usize> = elements(r)?
.iter()
.map(|v| v.trim().parse().ok())
.collect::<Option<_>>()?;
regions.push(Region {
ls: *n.first()?,
cs: *n.get(1)?,
le: *n.get(2)?,
ce: *n.get(3)?,
count: *n.get(4)?,
file: *n.get(5)?,
kind: *n.get(7)?,
});
}
out.push(Function {
name,
file,
regions,
});
}
Some(out)
}
pub(super) fn array_of<'a>(json: &'a str, key: &str) -> Option<&'a str> {
let quoted = format!("\"{key}\"");
let mut rest = json;
loop {
let at = rest.find("ed)?.checked_add(quoted.len())?;
rest = rest.get(at..)?;
let v = rest.trim_start().strip_prefix(':')?.trim_start();
if v.starts_with('[') {
return Some(v);
}
}
}
pub(super) fn elements(v: &str) -> Option<Vec<&str>> {
let body = v.trim_start().strip_prefix('[')?;
let (mut out, mut depth, mut start) = (Vec::new(), 0_usize, 0_usize);
let (mut in_str, mut escaped) = (false, false);
for (i, ch) in body.char_indices() {
if in_str {
match (escaped, ch) {
(true, _) => escaped = false,
(false, '\\') => escaped = true,
(false, '"') => in_str = false,
_ => {}
}
continue;
}
match ch {
'"' => in_str = true,
'[' | '{' => depth = depth.checked_add(1)?,
']' | '}' if depth > 0 => depth = depth.checked_sub(1)?,
',' | ']' if depth == 0 => {
let item = body.get(start..i)?.trim();
if !item.is_empty() {
out.push(item);
}
if ch == ']' {
return Some(out);
}
start = i.checked_add(1)?;
}
_ => {}
}
}
None
}