Skip to content

Commit

Permalink
refactor: use text utils into separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
camshaft committed Dec 12, 2024
1 parent 7c445f6 commit 5b2a229
Show file tree
Hide file tree
Showing 45 changed files with 541 additions and 710 deletions.
4 changes: 4 additions & 0 deletions duvet-core/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,10 @@ impl Slice<SourceFile> {
.map_err(|err| self.error(err, "error here"))
}

pub fn line(&self) -> usize {
self.line_range().start
}

pub fn line_range(&self) -> Range<usize> {
let mapping = self.file.mapping();
let start = mapping.offset_to_line(self.start);
Expand Down
4 changes: 2 additions & 2 deletions duvet/src/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,15 @@ impl Extract {
}
}

fn extract_sections<'a>(spec: &'a Specification) -> Vec<(&'a Section<'a>, Vec<Feature<'a>>)> {
fn extract_sections<'a>(spec: &'a Specification) -> Vec<(&'a Section, Vec<Feature<'a>>)> {
spec.sorted_sections()
.par_iter()
.map(|section| extract_section(section))
.filter(|(_section, features)| !features.is_empty())
.collect()
}

fn extract_section<'a>(section: &'a Section<'a>) -> (&'a Section<'a>, Vec<Feature<'a>>) {
fn extract_section<'a>(section: &'a Section) -> (&'a Section, Vec<Feature<'a>>) {
let mut features = vec![];
let lines = &section.lines[..];

Expand Down
1 change: 0 additions & 1 deletion duvet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ mod extract;
mod project;
mod report;
mod source;
mod sourcemap;
mod specification;
mod target;
mod text;
Expand Down
2 changes: 1 addition & 1 deletion duvet/src/report/ci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn enforce_source(report: &TargetReport) -> Result<(), anyhow::Error> {

// record all references to specific sections
for reference in &report.references {
let line = reference.line;
let line = reference.line();

significant_lines.insert(line);

Expand Down
24 changes: 13 additions & 11 deletions duvet/src/report/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
use super::{Reference, ReportResult, TargetReport};
use crate::{
annotation::{AnnotationLevel, AnnotationType},
sourcemap::Str,
specification::Line,
};
use duvet_core::file::Slice;
use rayon::prelude::*;
use std::{
collections::{BTreeMap, BTreeSet, HashMap},
Expand Down Expand Up @@ -277,7 +277,7 @@ pub fn report_source<Output: Write>(
requirements.insert(reference.annotation_id);
}
references
.entry(reference.line)
.entry(reference.line())
.or_default()
.push(reference);
}
Expand Down Expand Up @@ -323,7 +323,7 @@ pub fn report_source<Output: Write>(
if let Line::Str(line) = line {
item!(
arr,
if let Some(refs) = references.get(&line.line) {
if let Some(refs) = references.get(&line.line()) {
report_references(
line,
refs,
Expand Down Expand Up @@ -363,7 +363,7 @@ pub fn report_source<Output: Write>(
}

fn report_references<Output: Write>(
line: &Str,
line: &Slice,
refs: &[&Reference],
requirements: &mut BTreeSet<usize>,
output: &mut Output,
Expand All @@ -377,21 +377,23 @@ fn report_references<Output: Write>(

assert!(!refs.is_empty());
arr!(|arr| {
let mut start = line.pos;
let end = line.pos + line.len();
let line_range = line.range();
let line_pos = line_range.start;
let mut start = line_pos;
let end = line_range.end;

while start < end {
let mut min_end = end;
let current_refs = refs.iter().filter(|r| {
if r.start <= start {
if start < r.end {
min_end = min_end.min(r.end);
if r.start() <= start {
if start < r.end() {
min_end = min_end.min(r.end());
true
} else {
false
}
} else {
min_end = min_end.min(r.start);
min_end = min_end.min(r.start());
false
}
});
Expand Down Expand Up @@ -419,7 +421,7 @@ fn report_references<Output: Write>(
item!(arr, w!(status.id()));

// output the actual text
item!(arr, s!(line[(start - line.pos)..(min_end - line.pos)]));
item!(arr, s!(line[(start - line_pos)..(min_end - line_pos)]));
})
);

Expand Down
20 changes: 7 additions & 13 deletions duvet/src/report/lcov.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,26 @@ use std::{
const IMPL_BLOCK: &str = "0,0";
const TEST_BLOCK: &str = "1,0";

macro_rules! line {
($value:expr) => {
$value.line
};
}

macro_rules! record {
($block:expr, $line_hits:ident, $line:expr, $title:expr, $count:expr) => {
if $count != 0 {
$line_hits.insert($line);
}
put!("BRDA:{},{},{}", $line, $block, $count);
if let Some(title) = $title {
if let Some(title) = &$title {
let mut title_count = $count;
if title_count != 0 {
if !$line_hits.contains(&line!(title)) {
if !$line_hits.contains(&title.line()) {
// mark the title as recorded
$line_hits.insert(line!(title));
$line_hits.insert(title.line());
} else {
// the title was already recorded
title_count = 0;
}
}

put!("FNDA:{},{}", title_count, title);
put!("BRDA:{},{},{}", line!(title), $block, title_count);
put!("BRDA:{},{},{}", title.line(), $block, title_count);
}
};
}
Expand Down Expand Up @@ -76,7 +70,7 @@ fn report_source<Output: Write>(report: &TargetReport, output: &mut Output) -> R
// record all sections
for section in report.specification.sections.values() {
let title = &section.full_title;
put!("FN:{},{}", line!(title), title);
put!("FN:{},{}", title.line(), title);
}

put!("FNF:{}", report.specification.sections.len());
Expand All @@ -90,12 +84,12 @@ fn report_source<Output: Write>(report: &TargetReport, output: &mut Output) -> R
for reference in &report.references {
let title = if let Some(section_id) = reference.annotation.target_section() {
let section = report.specification.sections.get(section_id).unwrap();
Some(section.full_title)
Some(&section.full_title)
} else {
None
};

let line = line!(reference);
let line = reference.line();

macro_rules! citation {
($count:expr) => {
Expand Down
37 changes: 25 additions & 12 deletions duvet/src/report/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub struct Report {
issue_link: Option<String>,
}

#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Eq, Ord)]
#[derive(Clone, Debug, PartialEq, PartialOrd, Eq, Ord)]
struct Reference<'a> {
line: usize,
start: usize,
Expand All @@ -67,6 +67,20 @@ struct Reference<'a> {
level: AnnotationLevel,
}

impl Reference<'_> {
pub fn start(&self) -> usize {
self.start
}

pub fn end(&self) -> usize {
self.end
}

pub fn line(&self) -> usize {
self.line
}
}

#[derive(Debug)]
enum ReportError<'a> {
QuoteMismatch { annotation: &'a Annotation },
Expand Down Expand Up @@ -133,20 +147,19 @@ impl Report {

if let Some(section_id) = section_id {
if let Some(section) = spec.section(section_id) {
let contents = section.contents();
let contents = section.view();

for (annotation_id, annotation) in annotations {
if annotation.quote.is_empty() {
// empty quotes don't count towards coverage but are still
// references
let line = section.full_title.line;
let range = section.full_title.range();
let text = section.full_title.clone();
results.push(Ok((
target,
Reference {
line,
start: range.start,
end: range.end,
start: text.range().start,
end: text.range().end,
line: text.line(),
annotation,
annotation_id: *annotation_id,
level: annotation.level,
Expand All @@ -156,13 +169,13 @@ impl Report {
}

if let Some(range) = annotation.quote_range(&contents) {
for (line, range) in contents.ranges(range) {
for text in contents.ranges(range) {
results.push(Ok((
target,
Reference {
line,
start: range.start,
end: range.end,
start: text.range().start,
end: text.range().end,
line: text.line(),
annotation,
annotation_id: *annotation_id,
level: annotation.level,
Expand Down Expand Up @@ -299,7 +312,7 @@ pub struct ReportResult<'a> {
pub struct TargetReport<'a> {
target: &'a Target,
references: BTreeSet<Reference<'a>>,
specification: &'a Specification<'a>,
specification: &'a Specification,
require_citations: bool,
require_tests: bool,
statuses: status::StatusMap,
Expand Down
4 changes: 2 additions & 2 deletions duvet/src/report/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ pub struct Stat {

impl Stat {
fn record(&mut self, reference: &Reference) {
let start = reference.start as u64;
let end = reference.end as u64;
let start = reference.start() as u64;
let end = reference.end() as u64;
let len = end - start.max(self.cursor);
if len > 0 {
self.range += len;
Expand Down
6 changes: 3 additions & 3 deletions duvet/src/report/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl StatusMap {
// first build up a map of all of the references at any given offset
for r in references {
if r.annotation.anno != AnnotationType::Spec {
for offset in r.start..r.end {
for offset in r.start()..r.end() {
coverage.entry(offset).or_default().push(r);
}
} else {
Expand All @@ -44,10 +44,10 @@ impl StatusMap {
.map(|(anno_id, refs)| {
let mut spec = SpecReport::default();
for r in refs {
for offset in r.start..r.end {
for offset in r.start()..r.end() {
spec.insert(offset, r);
}
for (offset, refs) in coverage.range(r.start..r.end) {
for (offset, refs) in coverage.range(r.start()..r.end()) {
for r in refs {
spec.insert(*offset, r);
spec.related.insert(r.annotation_id);
Expand Down
91 changes: 0 additions & 91 deletions duvet/src/sourcemap.rs

This file was deleted.

Loading

0 comments on commit 5b2a229

Please sign in to comment.