1use std::fmt;
4
5use schemars::JsonSchema;
6use serde::{Deserialize, Serialize};
7
8#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
10#[schemars(
11 title = "FocusedReview",
12 description = "Structured focused-review result. Project impact describes the overall effect \
13 of the diff, while suggestions contains only actionable high- or \
14 medium-severity findings."
15)]
16pub struct FocusedReview {
17 #[schemars(
19 title = "project_impact",
20 description = "Concise statements about the diff's effects on behavior, reliability, \
21 maintainability, performance, security, or developer workflow. Use an \
22 empty array when there is no notable impact."
23 )]
24 pub project_impact: Vec<String>,
25 #[schemars(
27 title = "suggestions",
28 description = "Actionable findings scoped to the reviewed diff, ordered by severity with \
29 high severity first. Use an empty array when there are no suggestions."
30 )]
31 pub suggestions: Vec<FocusedReviewSuggestion>,
32}
33
34impl FocusedReview {
35 #[must_use]
37 pub fn to_markdown(&self) -> String {
38 let project_impact = markdown_bullets(&self.project_impact);
39 let suggestions = if self.suggestions.is_empty() {
40 "- None".to_string()
41 } else {
42 self.suggestions
43 .iter()
44 .map(|suggestion| {
45 format!("- [{}]: {}", suggestion.severity, suggestion.details.trim())
46 })
47 .collect::<Vec<_>>()
48 .join("\n")
49 };
50
51 format!(
52 "## Review\n\n### Project Impact\n\n{project_impact}\n\n### \
53 Suggestions\n\n{suggestions}"
54 )
55 }
56}
57
58#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
60#[schemars(
61 title = "FocusedReviewSuggestion",
62 description = "One actionable high- or medium-severity finding scoped to the reviewed diff."
63)]
64pub struct FocusedReviewSuggestion {
65 #[schemars(
67 title = "details",
68 description = "Concise issue details, including relevant repository-root-relative file \
69 and line references when available, plus the practical impact."
70 )]
71 pub details: String,
72 #[schemars(
74 title = "severity",
75 description = "Finding severity: high for correctness, security, data-loss, or \
76 build-breaking risks; medium for concrete reliability, maintainability, \
77 performance, or workflow risks."
78 )]
79 pub severity: FocusedReviewSeverity,
80}
81
82#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
84#[serde(rename_all = "lowercase")]
85#[schemars(
86 title = "FocusedReviewSeverity",
87 description = "Supported severity for an actionable focused-review finding."
88)]
89pub enum FocusedReviewSeverity {
90 High,
92 Medium,
94}
95
96impl fmt::Display for FocusedReviewSeverity {
97 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
98 formatter.write_str(match self {
99 Self::High => "High",
100 Self::Medium => "Medium",
101 })
102 }
103}
104
105fn markdown_bullets(items: &[String]) -> String {
108 if items.is_empty() {
109 return "- None".to_string();
110 }
111
112 items
113 .iter()
114 .map(|item| format!("- {}", item.trim()))
115 .collect::<Vec<_>>()
116 .join("\n")
117}
118
119#[cfg(test)]
120mod tests {
121 use super::*;
122
123 #[test]
124 fn focused_review_formats_structured_fields_as_markdown() {
125 let review = FocusedReview {
127 project_impact: vec!["Improves review reliability.".to_string()],
128 suggestions: vec![
129 FocusedReviewSuggestion {
130 details: "Fix the stale cache check.".to_string(),
131 severity: FocusedReviewSeverity::High,
132 },
133 FocusedReviewSuggestion {
134 details: "Deduplicate the parsing path.".to_string(),
135 severity: FocusedReviewSeverity::Medium,
136 },
137 ],
138 };
139
140 let markdown = review.to_markdown();
142
143 assert_eq!(
145 markdown,
146 "## Review\n\n### Project Impact\n\n- Improves review reliability.\n\n### \
147 Suggestions\n\n- [High]: Fix the stale cache check.\n- [Medium]: Deduplicate the \
148 parsing path."
149 );
150 }
151
152 #[test]
153 fn focused_review_formats_empty_arrays_with_none_sentinels() {
154 let review = FocusedReview {
156 project_impact: Vec::new(),
157 suggestions: Vec::new(),
158 };
159
160 let markdown = review.to_markdown();
162
163 assert_eq!(
165 markdown,
166 "## Review\n\n### Project Impact\n\n- None\n\n### Suggestions\n\n- None"
167 );
168 }
169
170 #[test]
171 fn focused_review_severity_serializes_as_lowercase() {
172 let severity = FocusedReviewSeverity::High;
174
175 let serialized = serde_json::to_string(&severity).expect("severity should serialize");
177 let deserialized = serde_json::from_str::<FocusedReviewSeverity>(&serialized)
178 .expect("severity should deserialize");
179
180 assert_eq!(serialized, "\"high\"");
182 assert_eq!(deserialized, severity);
183 }
184}