use std::fmt;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
#[schemars(
title = "FocusedReview",
description = "Structured focused-review result. Project impact describes the overall effect \
of the diff, while suggestions contains only actionable high- or \
medium-severity findings."
)]
pub struct FocusedReview {
#[schemars(
title = "project_impact",
description = "Concise statements about the diff's effects on behavior, reliability, \
maintainability, performance, security, or developer workflow. Use an \
empty array when there is no notable impact."
)]
pub project_impact: Vec<String>,
#[schemars(
title = "suggestions",
description = "Actionable findings scoped to the reviewed diff, ordered by severity with \
high severity first. Use an empty array when there are no suggestions."
)]
pub suggestions: Vec<FocusedReviewSuggestion>,
}
impl FocusedReview {
#[must_use]
pub fn to_markdown(&self) -> String {
let project_impact = markdown_bullets(&self.project_impact);
let suggestions = if self.suggestions.is_empty() {
"- None".to_string()
} else {
self.suggestions
.iter()
.map(|suggestion| {
format!("- [{}]: {}", suggestion.severity, suggestion.details.trim())
})
.collect::<Vec<_>>()
.join("\n")
};
format!(
"## Review\n\n### Project Impact\n\n{project_impact}\n\n### \
Suggestions\n\n{suggestions}"
)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
#[schemars(
title = "FocusedReviewSuggestion",
description = "One actionable high- or medium-severity finding scoped to the reviewed diff."
)]
pub struct FocusedReviewSuggestion {
#[schemars(
title = "details",
description = "Concise issue details, including relevant repository-root-relative file \
and line references when available, plus the practical impact."
)]
pub details: String,
#[schemars(
title = "severity",
description = "Finding severity: high for correctness, security, data-loss, or \
build-breaking risks; medium for concrete reliability, maintainability, \
performance, or workflow risks."
)]
pub severity: FocusedReviewSeverity,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "lowercase")]
#[schemars(
title = "FocusedReviewSeverity",
description = "Supported severity for an actionable focused-review finding."
)]
pub enum FocusedReviewSeverity {
High,
Medium,
}
impl fmt::Display for FocusedReviewSeverity {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(match self {
Self::High => "High",
Self::Medium => "Medium",
})
}
}
fn markdown_bullets(items: &[String]) -> String {
if items.is_empty() {
return "- None".to_string();
}
items
.iter()
.map(|item| format!("- {}", item.trim()))
.collect::<Vec<_>>()
.join("\n")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn focused_review_formats_structured_fields_as_markdown() {
let review = FocusedReview {
project_impact: vec!["Improves review reliability.".to_string()],
suggestions: vec![
FocusedReviewSuggestion {
details: "Fix the stale cache check.".to_string(),
severity: FocusedReviewSeverity::High,
},
FocusedReviewSuggestion {
details: "Deduplicate the parsing path.".to_string(),
severity: FocusedReviewSeverity::Medium,
},
],
};
let markdown = review.to_markdown();
assert_eq!(
markdown,
"## Review\n\n### Project Impact\n\n- Improves review reliability.\n\n### \
Suggestions\n\n- [High]: Fix the stale cache check.\n- [Medium]: Deduplicate the \
parsing path."
);
}
#[test]
fn focused_review_formats_empty_arrays_with_none_sentinels() {
let review = FocusedReview {
project_impact: Vec::new(),
suggestions: Vec::new(),
};
let markdown = review.to_markdown();
assert_eq!(
markdown,
"## Review\n\n### Project Impact\n\n- None\n\n### Suggestions\n\n- None"
);
}
#[test]
fn focused_review_severity_serializes_as_lowercase() {
let severity = FocusedReviewSeverity::High;
let serialized = serde_json::to_string(&severity).expect("severity should serialize");
let deserialized = serde_json::from_str::<FocusedReviewSeverity>(&serialized)
.expect("severity should deserialize");
assert_eq!(serialized, "\"high\"");
assert_eq!(deserialized, severity);
}
}