Skip to main content

paper_gap/
report.rs

1use crate::{Result, analyse, arxiv, pwc, repos};
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Serialize, Deserialize)]
5pub struct ScanReport {
6    pub paper_results: Vec<PaperResult>,
7}
8
9#[derive(Debug, Serialize, Deserialize)]
10pub struct PaperResult {
11    pub paper: arxiv::Paper,
12    pub code_links: Vec<pwc::CodeLink>,
13    pub repositories: Vec<repos::Repository>,
14    pub repository_analyses: Vec<analyse::RepositoryAnalysis>,
15    pub gap_assessment: analyse::GapAssessment,
16}
17
18#[derive(Clone, Copy)]
19pub enum OutputFormat {
20    Markdown,
21    Json,
22}
23
24pub fn render(report: &ScanReport, format: OutputFormat) -> Result<String> {
25    Ok(match format {
26        OutputFormat::Json => serde_json::to_string_pretty(report)?,
27        OutputFormat::Markdown => markdown(report),
28    })
29}
30
31fn markdown(report: &ScanReport) -> String {
32    let mut results: Vec<_> = report.paper_results.iter().collect();
33    results.sort_by_key(|result| std::cmp::Reverse(result.gap_assessment.implementation_gap_score));
34
35    let mut out = format!(
36        "# paper-gap report\n\n{} papers analysed. Ranked by implementation gap score.\n\n",
37        results.len()
38    );
39
40    for (index, result) in results.iter().enumerate() {
41        let p = &result.paper;
42        out.push_str(&format!(
43            "## {}. {}\n\n- Implementation gap score: {}\n- arXiv: {}\n- Published: {}\n- Authors: {}\n- Categories: {}\n",
44            index + 1,
45            p.title,
46            result.gap_assessment.implementation_gap_score,
47            p.arxiv_id.as_deref().unwrap_or(""),
48            p.published_date.as_deref().unwrap_or("unknown"),
49            p.authors.join(", "),
50            p.categories.join(", ")
51        ));
52
53        push_code_links(&mut out, &result.code_links);
54        push_repositories(&mut out, &result.repositories);
55        push_score(&mut out, &result.gap_assessment);
56
57        out.push_str(&format!("\n{}\n\n", p.abstract_text));
58    }
59
60    out
61}
62
63fn push_code_links(out: &mut String, links: &[pwc::CodeLink]) {
64    if links.is_empty() {
65        out.push_str("- Papers with Code links: none known\n");
66        return;
67    }
68    out.push_str("- Papers with Code links:\n");
69    for link in links {
70        out.push_str(&format!("  - {}\n", link.repository_url));
71    }
72}
73
74fn push_repositories(out: &mut String, repositories: &[repos::Repository]) {
75    if repositories.is_empty() {
76        out.push_str("- Repository search results: none found\n");
77        return;
78    }
79    out.push_str("- Repository search results:\n");
80    for repo in repositories {
81        out.push_str(&format!("  - {} ({})\n", repo.url, repo.forge));
82    }
83}
84
85fn push_score(out: &mut String, assessment: &analyse::GapAssessment) {
86    let c = &assessment.components;
87    out.push_str("- Score components:\n");
88    out.push_str(&format!("  - no_code_score: {}\n", c.no_code_score));
89    out.push_str(&format!(
90        "  - prototype_only_score: {}\n",
91        c.prototype_only_score
92    ));
93    out.push_str(&format!("  - stale_repo_score: {}\n", c.stale_repo_score));
94    out.push_str(&format!(
95        "  - packaging_gap_score: {}\n",
96        c.packaging_gap_score
97    ));
98    out.push_str(&format!("  - docs_gap_score: {}\n", c.docs_gap_score));
99    out.push_str(&format!("  - tests_gap_score: {}\n", c.tests_gap_score));
100    out.push_str(&format!("  - license_gap_score: {}\n", c.license_gap_score));
101    out.push_str(&format!(
102        "  - reproducibility_gap_score: {}\n",
103        c.reproducibility_gap_score
104    ));
105    out.push_str(&format!(
106        "  - ecosystem_gap_score: {}\n",
107        c.ecosystem_gap_score
108    ));
109
110    if assessment.gaps.is_empty() {
111        out.push_str("- Gap reasons: none\n");
112        return;
113    }
114    out.push_str("- Gap reasons:\n");
115    for gap in &assessment.gaps {
116        out.push_str(&format!(
117            "  - {:?}: +{} — {}\n",
118            gap.gap_type, gap.points, gap.explanation
119        ));
120    }
121}