bitbucket_cli/models/
commit.rs1use chrono::{DateTime, Utc};
4use serde::{Deserialize, Serialize};
5
6use super::pr::CommitAuthor;
10
11#[derive(Debug, Clone, Serialize, Deserialize)]
15pub struct BranchDetail {
16 pub name: String,
17 pub target: Option<CommitSummary>,
18 #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
19 pub ref_type: Option<String>,
20}
21
22#[derive(Debug, Clone, Serialize, Deserialize)]
24pub struct TagDetail {
25 pub name: String,
26 pub message: Option<String>,
28 pub target: Option<CommitSummary>,
29 pub date: Option<DateTime<Utc>>,
31}
32
33#[derive(Debug, Clone, Serialize, Deserialize)]
36pub struct CommitSummary {
37 pub hash: String,
38 pub message: Option<String>,
39 pub date: Option<DateTime<Utc>>,
40 pub author: Option<CommitAuthor>,
41}
42
43#[derive(Debug, Clone, Serialize, Deserialize)]
45pub struct SourceEntry {
46 pub path: String,
47 #[serde(rename = "type")]
49 pub entry_type: String,
50 pub size: Option<u64>,
52}
53
54#[derive(Debug, Clone, Serialize, Deserialize)]
57pub struct FileHistoryEntry {
58 pub commit: Option<CommitSummary>,
59 pub path: Option<String>,
60}
61
62#[derive(Debug, Clone, Serialize, Deserialize)]
64pub struct RefTarget {
65 pub hash: String,
66}
67
68#[derive(Debug, Clone, Serialize, Deserialize)]
70#[non_exhaustive]
71pub struct CreateBranchRequest {
72 pub name: String,
73 pub target: RefTarget,
74}
75
76#[derive(Debug, Clone, Serialize, Deserialize)]
79#[non_exhaustive]
80pub struct CreateTagRequest {
81 pub name: String,
82 pub target: RefTarget,
83 #[serde(skip_serializing_if = "Option::is_none")]
84 pub message: Option<String>,
85}
86
87#[cfg(test)]
88mod tests {
89 use super::*;
90
91 #[test]
92 fn create_branch_request_serializes_name_and_target_hash() {
93 let request = CreateBranchRequest {
94 name: "feature/login".to_string(),
95 target: RefTarget {
96 hash: "a1b2c3d".to_string(),
97 },
98 };
99
100 let json = serde_json::to_value(&request).unwrap();
101 assert_eq!(
102 json,
103 serde_json::json!({
104 "name": "feature/login",
105 "target": { "hash": "a1b2c3d" }
106 })
107 );
108 }
109
110 #[test]
111 fn create_tag_request_omits_unset_message() {
112 let request = CreateTagRequest {
113 name: "v1.0.0".to_string(),
114 target: RefTarget {
115 hash: "a1b2c3d".to_string(),
116 },
117 message: None,
118 };
119
120 let json = serde_json::to_value(&request).unwrap();
121 assert_eq!(
122 json,
123 serde_json::json!({
124 "name": "v1.0.0",
125 "target": { "hash": "a1b2c3d" }
126 })
127 );
128 }
129
130 #[test]
131 fn create_tag_request_includes_message_when_set() {
132 let request = CreateTagRequest {
133 name: "v1.0.0".to_string(),
134 target: RefTarget {
135 hash: "a1b2c3d".to_string(),
136 },
137 message: Some("First release".to_string()),
138 };
139
140 let json = serde_json::to_value(&request).unwrap();
141 assert_eq!(json["message"], serde_json::json!("First release"));
142 }
143
144 #[test]
145 fn branch_detail_maps_type_field() {
146 let branch: BranchDetail = serde_json::from_str(
147 r#"{
148 "name": "main",
149 "type": "branch",
150 "target": {
151 "hash": "deadbeefcafe",
152 "message": "Initial commit\n",
153 "date": "2024-01-01T00:00:00+00:00",
154 "author": { "raw": "Jo <jo@example.com>" }
155 }
156 }"#,
157 )
158 .unwrap();
159
160 assert_eq!(branch.ref_type.as_deref(), Some("branch"));
161 let target = branch.target.unwrap();
162 assert_eq!(target.hash, "deadbeefcafe");
163 assert_eq!(
164 target.author.unwrap().raw.as_deref(),
165 Some("Jo <jo@example.com>")
166 );
167 }
168
169 #[test]
170 fn source_entry_maps_type_field() {
171 let entry: SourceEntry = serde_json::from_str(
172 r#"{ "path": "src/main.rs", "type": "commit_file", "size": 1024 }"#,
173 )
174 .unwrap();
175
176 assert_eq!(entry.entry_type, "commit_file");
177 assert_eq!(entry.size, Some(1024));
178 }
179}