gitea_sdk_rs/options/
release.rs1use crate::pagination::{ListOptions, QueryEncode};
8use crate::{Deserialize, Serialize};
9
10#[derive(Debug, Clone, Default)]
14pub struct ListReleasesOptions {
16 pub list_options: ListOptions,
17 pub is_draft: Option<bool>,
18 pub is_pre_release: Option<bool>,
19}
20
21impl QueryEncode for ListReleasesOptions {
22 fn query_encode(&self) -> String {
23 let mut out = self.list_options.query_encode();
24 if let Some(draft) = self.is_draft {
25 out.push_str(&format!("&draft={draft}"));
26 }
27 if let Some(pre) = self.is_pre_release {
28 out.push_str(&format!("&pre-release={pre}"));
29 }
30 out
31 }
32}
33
34#[derive(Debug, Clone, Serialize, Deserialize)]
38pub struct CreateReleaseOption {
40 #[serde(rename = "tag_name")]
42 pub tag_name: String,
43 #[serde(rename = "target_commitish", skip_serializing_if = "Option::is_none")]
45 pub target: Option<String>,
46 #[serde(rename = "name", skip_serializing_if = "Option::is_none")]
48 pub title: Option<String>,
49 #[serde(rename = "body", skip_serializing_if = "Option::is_none")]
51 pub note: Option<String>,
52 #[serde(default)]
54 pub is_draft: bool,
55 #[serde(default)]
57 pub is_prerelease: bool,
58}
59
60impl CreateReleaseOption {
61 pub fn validate(&self) -> crate::Result<()> {
63 if self.tag_name.trim().is_empty() {
64 return Err(crate::Error::Validation("tag_name is required".to_string()));
65 }
66 if let Some(ref title) = self.title
67 && title.trim().is_empty()
68 {
69 return Err(crate::Error::Validation("title is empty".to_string()));
70 }
71 Ok(())
72 }
73}
74
75#[derive(Debug, Clone, Default, Serialize, Deserialize)]
79pub struct EditReleaseOption {
81 #[serde(rename = "tag_name", skip_serializing_if = "Option::is_none")]
83 pub tag_name: Option<String>,
84 #[serde(rename = "target_commitish", skip_serializing_if = "Option::is_none")]
86 pub target: Option<String>,
87 #[serde(rename = "name", skip_serializing_if = "Option::is_none")]
89 pub title: Option<String>,
90 #[serde(rename = "body", skip_serializing_if = "Option::is_none")]
92 pub note: Option<String>,
93 #[serde(skip_serializing_if = "Option::is_none")]
95 pub is_draft: Option<bool>,
96 #[serde(skip_serializing_if = "Option::is_none")]
98 pub is_prerelease: Option<bool>,
99}
100
101#[derive(Debug, Clone, Default)]
105pub struct ListReleaseAttachmentsOptions {
107 pub list_options: ListOptions,
108}
109
110impl QueryEncode for ListReleaseAttachmentsOptions {
111 fn query_encode(&self) -> String {
112 self.list_options.query_encode()
113 }
114}
115
116#[derive(Debug, Clone, Serialize, Deserialize)]
120pub struct EditAttachmentOption {
122 pub name: String,
124}
125
126impl EditAttachmentOption {
127 pub fn validate(&self) -> crate::Result<()> {
129 if self.name.trim().is_empty() {
130 return Err(crate::Error::Validation("name is required".to_string()));
131 }
132 Ok(())
133 }
134}
135
136#[cfg(test)]
137mod tests {
138 use super::*;
139
140 #[test]
141 fn test_create_release_option_validate_success() {
142 let opt = CreateReleaseOption {
143 tag_name: "v1.0.0".to_string(),
144 target: None,
145 title: None,
146 note: None,
147 is_draft: false,
148 is_prerelease: false,
149 };
150 assert!(opt.validate().is_ok());
151 }
152
153 #[test]
154 fn test_create_release_option_validate_empty_tag_name() {
155 let opt = CreateReleaseOption {
156 tag_name: String::new(),
157 target: None,
158 title: None,
159 note: None,
160 is_draft: false,
161 is_prerelease: false,
162 };
163 assert!(opt.validate().is_err());
164 }
165
166 #[test]
167 fn test_create_release_option_validate_empty_title() {
168 let opt = CreateReleaseOption {
169 tag_name: "v1.0.0".to_string(),
170 target: None,
171 title: Some(" ".to_string()),
172 note: None,
173 is_draft: false,
174 is_prerelease: false,
175 };
176 assert!(opt.validate().is_err());
177 }
178
179 #[test]
180 fn test_edit_attachment_option_validate_success() {
181 let opt = EditAttachmentOption {
182 name: "file.zip".to_string(),
183 };
184 assert!(opt.validate().is_ok());
185 }
186
187 #[test]
188 fn test_edit_attachment_option_validate_empty_name() {
189 let opt = EditAttachmentOption {
190 name: String::new(),
191 };
192 assert!(opt.validate().is_err());
193 }
194}