git_iris/changes/
models.rs

1use crate::log_debug;
2use schemars::JsonSchema;
3use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5
6/// Represents the structured response for a changelog
7#[derive(Serialize, Deserialize, JsonSchema, Debug)]
8pub struct ChangelogResponse {
9    /// The version number of the release
10    pub version: Option<String>,
11    /// The date of the release
12    pub release_date: Option<String>,
13    /// Categorized changes, grouped by type
14    pub sections: HashMap<ChangelogType, Vec<ChangeEntry>>,
15    /// List of breaking changes in this release
16    pub breaking_changes: Vec<BreakingChange>,
17    /// Metrics summarizing the changes in this release
18    pub metrics: ChangeMetrics,
19}
20
21/// Enumeration of possible change types for changelog entries
22#[derive(Clone, Serialize, Deserialize, JsonSchema, Debug, PartialEq, Eq, Hash)]
23pub enum ChangelogType {
24    Added,
25    Changed,
26    Deprecated,
27    Removed,
28    Fixed,
29    Security,
30}
31
32/// Represents a single change entry in the changelog
33#[derive(Serialize, Deserialize, JsonSchema, Debug)]
34pub struct ChangeEntry {
35    /// Description of the change
36    pub description: String,
37    /// List of commit hashes associated with this change
38    pub commit_hashes: Vec<String>,
39    /// List of issue numbers associated with this change
40    pub associated_issues: Vec<String>,
41    /// Pull request number associated with this change, if any
42    pub pull_request: Option<String>,
43}
44
45/// Represents a breaking change in the release
46#[derive(Serialize, Deserialize, JsonSchema, Debug)]
47pub struct BreakingChange {
48    /// Description of the breaking change
49    pub description: String,
50    /// Commit hash associated with this breaking change
51    pub commit_hash: String,
52}
53
54/// Metrics summarizing the changes in a release
55#[derive(Clone, Serialize, Deserialize, JsonSchema, Debug)]
56pub struct ChangeMetrics {
57    /// Total number of commits in this release
58    pub total_commits: usize,
59    /// Number of files changed in this release
60    pub files_changed: usize,
61    /// Number of lines inserted in this release
62    pub insertions: usize,
63    /// Number of lines deleted in this release
64    pub deletions: usize,
65    /// Total lines changed in this release
66    pub total_lines_changed: usize,
67}
68
69/// Represents the structured response for release notes
70#[derive(Serialize, Deserialize, JsonSchema, Debug)]
71pub struct ReleaseNotesResponse {
72    /// The version number of the release
73    pub version: Option<String>,
74    /// The date of the release
75    pub release_date: Option<String>,
76    /// A brief summary of the release
77    pub summary: String,
78    /// List of highlighted changes or features in this release
79    pub highlights: Vec<Highlight>,
80    /// Detailed sections of changes
81    pub sections: Vec<Section>,
82    /// List of breaking changes in this release
83    pub breaking_changes: Vec<BreakingChange>,
84    /// Notes for upgrading to this version
85    pub upgrade_notes: Vec<String>,
86    /// Metrics summarizing the changes in this release
87    pub metrics: ChangeMetrics,
88}
89
90/// Represents a highlight in the release notes
91#[derive(Serialize, Deserialize, JsonSchema, Debug)]
92pub struct Highlight {
93    /// Title of the highlight
94    pub title: String,
95    /// Detailed description of the highlight
96    pub description: String,
97}
98
99/// Represents a section in the release notes
100#[derive(Serialize, Deserialize, JsonSchema, Debug)]
101pub struct Section {
102    /// Title of the section
103    pub title: String,
104    /// List of items in this section
105    pub items: Vec<SectionItem>,
106}
107
108/// Represents an item in a section of the release notes
109#[derive(Serialize, Deserialize, JsonSchema, Debug)]
110pub struct SectionItem {
111    /// Description of the change
112    pub description: String,
113    /// List of issue numbers associated with this change
114    pub associated_issues: Vec<String>,
115    /// Pull request number associated with this change, if any
116    pub pull_request: Option<String>,
117}
118
119impl From<String> for ChangelogResponse {
120    /// Converts a JSON string to a `ChangelogResponse`
121    fn from(value: String) -> Self {
122        serde_json::from_str(&value).unwrap_or_else(|e| {
123            log_debug!("Failed to parse ChangelogResponse: {}", e);
124            Self {
125                version: Some("Error".to_string()),
126                release_date: Some("Error".to_string()),
127                sections: HashMap::new(),
128                breaking_changes: Vec::new(),
129                metrics: ChangeMetrics {
130                    total_commits: 0,
131                    files_changed: 0,
132                    insertions: 0,
133                    deletions: 0,
134                    total_lines_changed: 0,
135                },
136            }
137        })
138    }
139}
140
141impl From<String> for ReleaseNotesResponse {
142    /// Converts a JSON string to a `ReleaseNotesResponse`
143    fn from(value: String) -> Self {
144        serde_json::from_str(&value).unwrap_or_else(|e| {
145            log_debug!("Failed to parse ReleaseNotesResponse: {}", e);
146            Self {
147                version: Some("Error".to_string()),
148                release_date: Some("Error".to_string()),
149                summary: format!("Error parsing response: {e}"),
150                highlights: Vec::new(),
151                sections: Vec::new(),
152                breaking_changes: Vec::new(),
153                upgrade_notes: Vec::new(),
154                metrics: ChangeMetrics {
155                    total_commits: 0,
156                    files_changed: 0,
157                    insertions: 0,
158                    deletions: 0,
159                    total_lines_changed: 0,
160                },
161            }
162        })
163    }
164}