1use crate::model::DiffResult;
2
3pub fn format_diff_text(diff: &DiffResult, breaking_only: bool) -> String {
4 let mut out = String::new();
5 if !breaking_only {
6 out.push_str(&format!(
7 "Summary: {} entity, {} axiom, {} annotation, {} import, {} inference, {} breaking\n\n",
8 diff.entity_changes.len(),
9 diff.axiom_changes.len(),
10 diff.annotation_changes.len(),
11 diff.import_changes.len(),
12 diff.inference_changes.len(),
13 diff.breaking_changes.len(),
14 ));
15 }
16 if !diff.breaking_changes.is_empty() {
17 out.push_str("Breaking changes:\n");
18 for b in &diff.breaking_changes {
19 out.push_str(&format!(" - [{}] {}\n", reason_label(b.reason), b.message));
20 }
21 out.push('\n');
22 }
23 if breaking_only {
24 return out;
25 }
26 if !diff.entity_changes.is_empty() {
27 out.push_str("Entity changes:\n");
28 for e in &diff.entity_changes {
29 out.push_str(&format!(" - {:?} {}\n", e.kind, e.iri));
30 }
31 out.push('\n');
32 }
33 if !diff.axiom_changes.is_empty() {
34 out.push_str("Axiom changes:\n");
35 for a in &diff.axiom_changes {
36 out.push_str(&format!(
37 " - {} {} {} {} {}\n",
38 a.change, a.axiom_kind, a.subject, a.predicate, a.object
39 ));
40 }
41 out.push('\n');
42 }
43 if !diff.annotation_changes.is_empty() {
44 out.push_str("Annotation changes:\n");
45 for a in &diff.annotation_changes {
46 out.push_str(&format!(" - {} {} {} {}\n", a.change, a.subject, a.predicate, a.object));
47 }
48 out.push('\n');
49 }
50 if !diff.import_changes.is_empty() {
51 out.push_str("Import changes:\n");
52 for i in &diff.import_changes {
53 out.push_str(&format!(" - {} {}\n", i.change, i.import_iri));
54 }
55 out.push('\n');
56 }
57 out
58}
59
60pub fn format_diff_markdown(diff: &DiffResult, breaking_only: bool) -> String {
61 let mut out = String::from("# Ontology semantic diff\n\n");
62 if !diff.breaking_changes.is_empty() {
63 out.push_str("## Breaking changes\n\n");
64 for b in &diff.breaking_changes {
65 out.push_str(&format!("- **{}**: {}\n", reason_label(b.reason), b.message));
66 }
67 out.push('\n');
68 }
69 if breaking_only {
70 return out;
71 }
72 out.push_str(&format!(
73 "## Summary\n\n| Category | Count |\n|---|---|\n| Entities | {} |\n| Axioms | {} |\n| Annotations | {} |\n| Imports | {} |\n| Inferences | {} |\n| Breaking | {} |\n\n",
74 diff.entity_changes.len(),
75 diff.axiom_changes.len(),
76 diff.annotation_changes.len(),
77 diff.import_changes.len(),
78 diff.inference_changes.len(),
79 diff.breaking_changes.len(),
80 ));
81 out
82}
83
84pub fn format_diff_pr_summary(diff: &DiffResult) -> String {
85 let mut out = String::from("## Ontology changes\n\n");
86 if !diff.breaking_changes.is_empty() {
87 out.push_str("> **Breaking changes detected** — review carefully before merge.\n\n");
88 for b in &diff.breaking_changes {
89 out.push_str(&format!("- **{}**: {}\n", reason_label(b.reason), b.message));
90 }
91 out.push('\n');
92 }
93 let counts = format!(
94 "| Entities | Axioms | Annotations | Imports | Inferences |\n|---|---|---|---|---|\n| {} | {} | {} | {} | {} |\n\n",
95 diff.entity_changes.len(),
96 diff.axiom_changes.len(),
97 diff.annotation_changes.len(),
98 diff.import_changes.len(),
99 diff.inference_changes.len(),
100 );
101 out.push_str(&counts);
102
103 if !diff.entity_changes.is_empty() {
104 out.push_str("### Entities\n\n");
105 for e in diff.entity_changes.iter().take(50) {
106 out.push_str(&format!("- `{:?}` {}\n", e.kind, e.iri));
107 }
108 if diff.entity_changes.len() > 50 {
109 out.push_str(&format!(
110 "\n_…and {} more entity changes_\n",
111 diff.entity_changes.len() - 50
112 ));
113 }
114 out.push('\n');
115 }
116 if !diff.axiom_changes.is_empty() {
117 out.push_str("### Axioms\n\n");
118 for a in diff.axiom_changes.iter().take(30) {
119 out.push_str(&format!(
120 "- **{}** `{}` — {} {} {}\n",
121 a.change, a.axiom_kind, a.subject, a.predicate, a.object
122 ));
123 }
124 if diff.axiom_changes.len() > 30 {
125 out.push_str(&format!(
126 "\n_…and {} more axiom changes_\n",
127 diff.axiom_changes.len() - 30
128 ));
129 }
130 out.push('\n');
131 }
132 if !diff.import_changes.is_empty() {
133 out.push_str("### Imports\n\n");
134 for i in &diff.import_changes {
135 out.push_str(&format!("- **{}** {}\n", i.change, i.import_iri));
136 }
137 out.push('\n');
138 }
139 out.push_str("---\n_Generated by `ontocore diff --pr-summary`_\n");
140 out
141}
142
143pub fn format_diff_json(diff: &DiffResult) -> String {
144 serde_json::to_string_pretty(diff).unwrap_or_else(|_| "{}".to_string())
145}
146
147fn reason_label(reason: crate::model::BreakingReason) -> &'static str {
148 use crate::model::BreakingReason::*;
149 match reason {
150 RemovedEntity => "removed_entity",
151 RenamedIri => "renamed_iri",
152 RemovedSuperclass => "removed_superclass",
153 RemovedImport => "removed_import",
154 UnsatisfiableClass => "unsatisfiable_class",
155 DomainRangeChange => "domain_range_change",
156 }
157}
158
159#[cfg(test)]
160mod tests {
161 use super::*;
162 use crate::model::{AxiomChange, BreakingChange, DiffResult, EntityChange, EntityChangeKind};
163
164 #[test]
165 fn pr_summary_includes_breaking_callout() {
166 let diff = DiffResult {
167 entity_changes: vec![EntityChange {
168 iri: "http://ex#A".into(),
169 kind: EntityChangeKind::Removed,
170 previous_iri: None,
171 labels: vec![],
172 }],
173 axiom_changes: vec![],
174 annotation_changes: vec![],
175 import_changes: vec![],
176 inference_changes: vec![],
177 breaking_changes: vec![BreakingChange {
178 reason: crate::model::BreakingReason::RemovedEntity,
179 message: "class removed".into(),
180 entity_iri: Some("http://ex#A".into()),
181 }],
182 };
183 let md = format_diff_pr_summary(&diff);
184 assert!(md.contains("Breaking changes"));
185 assert!(md.contains("http://ex#A"));
186 assert!(md.contains("ontocore diff --pr-summary"));
187 }
188
189 #[test]
190 fn pr_summary_lists_axiom_rows() {
191 let diff = DiffResult {
192 entity_changes: vec![],
193 axiom_changes: vec![AxiomChange {
194 change: "added".into(),
195 axiom_kind: "SubClassOf".into(),
196 subject: "http://ex#Child".into(),
197 predicate: "rdfs:subClassOf".into(),
198 object: "http://ex#Parent".into(),
199 }],
200 annotation_changes: vec![],
201 import_changes: vec![],
202 inference_changes: vec![],
203 breaking_changes: vec![],
204 };
205 let md = format_diff_pr_summary(&diff);
206 assert!(md.contains("### Axioms"));
207 assert!(md.contains("SubClassOf"));
208 }
209}