Skip to main content

covguard_ports/
lib.rs

1//! Shared port traits and boundary DTOs for covguard's hexagonal architecture.
2
3use std::collections::BTreeMap;
4use std::ops::RangeInclusive;
5use std::path::Path;
6
7/// Canonical changed-line representation used at the port boundary.
8pub type ChangedRanges = BTreeMap<String, Vec<RangeInclusive<u32>>>;
9
10/// Canonical line-hit representation used at the port boundary.
11pub type CoverageMap = BTreeMap<String, BTreeMap<u32, u32>>;
12
13/// Parsed diff payload used by the diff provider port.
14#[derive(Debug, Clone, PartialEq, Eq, Default)]
15pub struct DiffParseResult {
16    /// Changed lines grouped by normalized repo-relative path.
17    pub changed_ranges: ChangedRanges,
18    /// Binary file paths detected in the diff.
19    pub binary_files: Vec<String>,
20}
21
22/// Port for obtaining changed ranges from diff input.
23pub trait DiffProvider {
24    /// Parse unified diff text and return changed ranges plus metadata.
25    fn parse_patch(&self, text: &str) -> Result<DiffParseResult, String>;
26
27    /// Load a unified diff between two refs from a repository path.
28    fn load_diff_from_git(
29        &self,
30        base: &str,
31        head: &str,
32        repo_root: &Path,
33    ) -> Result<String, String>;
34}
35
36/// Port for loading and merging LCOV coverage data.
37pub trait CoverageProvider {
38    /// Parse an LCOV payload into a normalized coverage map.
39    fn parse_lcov(&self, text: &str, strip_prefixes: &[String]) -> Result<CoverageMap, String>;
40
41    /// Merge multiple coverage maps into one.
42    fn merge_coverage(&self, maps: Vec<CoverageMap>) -> CoverageMap;
43}
44
45/// Port for obtaining the current UTC time.
46pub trait Clock {
47    /// Returns the current time in UTC.
48    fn now(&self) -> chrono::DateTime<chrono::Utc>;
49}
50
51/// Port for reading source lines from the repository.
52pub trait RepoReader {
53    /// Returns the source line at 1-based `line_no`, or `None` when unavailable.
54    fn read_line(&self, path: &str, line_no: u32) -> Option<String>;
55}