use log::debug;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Serialize, Deserialize, JsonSchema, Debug)]
pub struct ChangelogResponse {
pub version: Option<String>,
pub release_date: Option<String>,
pub sections: HashMap<ChangelogType, Vec<ChangeEntry>>,
pub breaking_changes: Vec<BreakingChange>,
pub metrics: ChangeMetrics,
}
#[derive(Clone, Serialize, Deserialize, JsonSchema, Debug, PartialEq, Eq, Hash)]
pub enum ChangelogType {
Added,
Changed,
Deprecated,
Removed,
Fixed,
Security,
}
#[derive(Serialize, Deserialize, JsonSchema, Debug)]
pub struct ChangeEntry {
pub description: String,
pub commit_hashes: Vec<String>,
pub associated_issues: Vec<String>,
pub pull_request: Option<String>,
}
#[derive(Serialize, Deserialize, JsonSchema, Debug)]
pub struct BreakingChange {
pub description: String,
pub commit_hash: String,
}
#[derive(Clone, Serialize, Deserialize, JsonSchema, Debug)]
pub struct ChangeMetrics {
pub total_commits: usize,
pub files_changed: usize,
pub insertions: usize,
pub deletions: usize,
pub total_lines_changed: usize,
}
#[derive(Serialize, Deserialize, JsonSchema, Debug)]
pub struct ReleaseNotesResponse {
pub version: Option<String>,
pub release_date: Option<String>,
pub summary: String,
pub highlights: Vec<Highlight>,
pub sections: Vec<Section>,
pub breaking_changes: Vec<BreakingChange>,
pub upgrade_notes: Vec<String>,
pub metrics: ChangeMetrics,
}
#[derive(Serialize, Deserialize, JsonSchema, Debug)]
pub struct Highlight {
pub title: String,
pub description: String,
}
#[derive(Serialize, Deserialize, JsonSchema, Debug)]
pub struct Section {
pub title: String,
pub items: Vec<SectionItem>,
}
#[derive(Serialize, Deserialize, JsonSchema, Debug)]
pub struct SectionItem {
pub description: String,
pub associated_issues: Vec<String>,
pub pull_request: Option<String>,
}
impl From<String> for ChangelogResponse {
fn from(value: String) -> Self {
serde_json::from_str(&value).unwrap_or_else(|e| {
debug!("Failed to parse ChangelogResponse: {e}");
Self {
version: Some("Error".to_string()),
release_date: Some("Error".to_string()),
sections: HashMap::new(),
breaking_changes: Vec::new(),
metrics: ChangeMetrics {
total_commits: 0,
files_changed: 0,
insertions: 0,
deletions: 0,
total_lines_changed: 0,
},
}
})
}
}
impl From<String> for ReleaseNotesResponse {
fn from(value: String) -> Self {
serde_json::from_str(&value).unwrap_or_else(|e| {
debug!("Failed to parse ReleaseNotesResponse: {e}");
Self {
version: Some("Error".to_string()),
release_date: Some("Error".to_string()),
summary: format!("Error parsing response: {e}"),
highlights: Vec::new(),
sections: Vec::new(),
breaking_changes: Vec::new(),
upgrade_notes: Vec::new(),
metrics: ChangeMetrics {
total_commits: 0,
files_changed: 0,
insertions: 0,
deletions: 0,
total_lines_changed: 0,
},
}
})
}
}