gitea-sdk-rs 0.1.0

Rust SDK for the Gitea API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
// Copyright 2026 infinitete. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

//! Request option types for user API endpoints.

use crate::internal::request::urlencoding;
use crate::pagination::{ListOptions, QueryEncode};
use crate::types::enums::AccessTokenScope;
use crate::{Deserialize, Serialize};

#[derive(Debug, Clone, Default)]
/// Options for List Emails Option.
pub struct ListEmailsOptions {
    pub list_options: ListOptions,
}

impl QueryEncode for ListEmailsOptions {
    fn query_encode(&self) -> String {
        self.list_options.query_encode()
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
/// Options for Create Email Option.
pub struct CreateEmailOption {
    /// email addresses to add
    pub emails: Vec<String>,
}

impl CreateEmailOption {
    /// Validate this `CreateEmailOption` payload.
    pub fn validate(&self) -> crate::Result<()> {
        if self.emails.is_empty() {
            return Err(crate::Error::Validation(
                "at least one email is required".to_string(),
            ));
        }
        for email in &self.emails {
            if email.is_empty() {
                return Err(crate::Error::Validation(
                    "email addresses must not be empty".to_string(),
                ));
            }
        }
        Ok(())
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
/// Options for Delete Email Option.
pub struct DeleteEmailOption {
    /// email addresses to delete
    pub emails: Vec<String>,
}

impl DeleteEmailOption {
    /// Validate this `DeleteEmailOption` payload.
    pub fn validate(&self) -> crate::Result<()> {
        if self.emails.is_empty() {
            return Err(crate::Error::Validation(
                "at least one email is required".to_string(),
            ));
        }
        for email in &self.emails {
            if email.is_empty() {
                return Err(crate::Error::Validation(
                    "email addresses must not be empty".to_string(),
                ));
            }
        }
        Ok(())
    }
}

#[derive(Debug, Clone, Default)]
/// Options for List Public Keys Option.
pub struct ListPublicKeysOptions {
    pub list_options: ListOptions,
}

impl QueryEncode for ListPublicKeysOptions {
    fn query_encode(&self) -> String {
        self.list_options.query_encode()
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
/// Options for Create Key Option.
pub struct CreateKeyOption {
    /// Title of the key to add
    pub title: String,
    /// An armored SSH key to add
    pub key: String,
    /// Describe if the key has only read access or read/write
    #[serde(default)]
    pub read_only: bool,
}

impl CreateKeyOption {
    /// Validate this `CreateKeyOption` payload.
    pub fn validate(&self) -> crate::Result<()> {
        if self.key.is_empty() {
            return Err(crate::Error::Validation("key is required".to_string()));
        }
        if self.title.is_empty() {
            return Err(crate::Error::Validation("title is required".to_string()));
        }
        Ok(())
    }
}

#[derive(Debug, Clone, Default)]
/// Options for List Followers Option.
pub struct ListFollowersOptions {
    pub list_options: ListOptions,
}

impl QueryEncode for ListFollowersOptions {
    fn query_encode(&self) -> String {
        self.list_options.query_encode()
    }
}

#[derive(Debug, Clone, Default)]
/// Options for List Following Option.
pub struct ListFollowingOptions {
    pub list_options: ListOptions,
}

impl QueryEncode for ListFollowingOptions {
    fn query_encode(&self) -> String {
        self.list_options.query_encode()
    }
}

#[derive(Debug, Clone, Default)]
/// Options for List Access Tokens Option.
pub struct ListAccessTokensOptions {
    pub list_options: ListOptions,
}

impl QueryEncode for ListAccessTokensOptions {
    fn query_encode(&self) -> String {
        self.list_options.query_encode()
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
/// Options for Create Access Token Option.
pub struct CreateAccessTokenOption {
    pub name: String,
    #[serde(default)]
    pub scopes: Vec<AccessTokenScope>,
}

impl CreateAccessTokenOption {
    /// Validate this `CreateAccessTokenOption` payload.
    pub fn validate(&self) -> crate::Result<()> {
        if self.name.is_empty() {
            return Err(crate::Error::Validation("name is required".to_string()));
        }
        Ok(())
    }
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
/// Options for User Settings Option.
pub struct UserSettingsOptions {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub full_name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub website: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub location: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub language: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub theme: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none", rename = "diff_view_style")]
    pub diff_view_style: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none", rename = "hide_email")]
    pub hide_email: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none", rename = "hide_activity")]
    pub hide_activity: Option<bool>,
}

#[derive(Debug, Clone, Default)]
/// Options for List User Blocks Option.
pub struct ListUserBlocksOptions {
    pub list_options: ListOptions,
}

impl QueryEncode for ListUserBlocksOptions {
    fn query_encode(&self) -> String {
        self.list_options.query_encode()
    }
}

#[derive(Debug, Clone, Default)]
/// Options for Search Users Option.
pub struct SearchUsersOption {
    pub list_options: ListOptions,
    pub key_word: String,
    pub uid: i64,
}

impl QueryEncode for SearchUsersOption {
    fn query_encode(&self) -> String {
        let mut out = String::new();
        let defaulted = self.list_options.with_defaults();
        if defaulted.page == Some(0) {
            out.push_str("page=0&limit=0");
        } else if let Some(page) = defaulted.page {
            out.push_str(&format!("page={page}"));
            if let Some(size) = defaulted.page_size {
                out.push_str(&format!("&limit={size}"));
            }
        }
        if !self.key_word.is_empty() {
            out.push_str(&format!("&q={}", urlencoding(&self.key_word)));
        }
        if self.uid > 0 {
            out.push_str(&format!("&uid={}", self.uid));
        }
        out
    }
}

#[derive(Debug, Clone, Default)]
/// Options for List User Activity Feeds Option.
pub struct ListUserActivityFeedsOptions {
    pub list_options: ListOptions,
    pub only_performed_by: bool,
    pub date: String,
}

impl QueryEncode for ListUserActivityFeedsOptions {
    fn query_encode(&self) -> String {
        let mut query = self.list_options.query_encode();
        if self.only_performed_by {
            query.push_str("&only-performed-by=true");
        }
        if !self.date.is_empty() {
            query.push_str("&date=");
            query.push_str(&urlencoding(&self.date));
        }
        query
    }
}

#[derive(Debug, Clone, Default)]
/// Options for List GPGKeys Option.
pub struct ListGPGKeysOptions {
    pub list_options: ListOptions,
}

impl QueryEncode for ListGPGKeysOptions {
    fn query_encode(&self) -> String {
        self.list_options.query_encode()
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
/// Options for Create GPGKey Option.
pub struct CreateGPGKeyOption {
    /// An armored GPG key to add
    #[serde(rename = "armored_public_key")]
    pub armored_key: String,
    /// An optional armored signature for the GPG key
    #[serde(skip_serializing_if = "Option::is_none")]
    pub signature: Option<String>,
}

impl CreateGPGKeyOption {
    /// Validate this `CreateGPGKeyOption` payload.
    pub fn validate(&self) -> crate::Result<()> {
        if self.armored_key.is_empty() {
            return Err(crate::Error::Validation(
                "armored_public_key is required".to_string(),
            ));
        }
        Ok(())
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
/// Options for Verify GPGKey Option.
pub struct VerifyGPGKeyOption {
    #[serde(rename = "key_id")]
    pub key_id: String,
    #[serde(rename = "armored_signature")]
    pub signature: String,
}

impl VerifyGPGKeyOption {
    /// Validate this `VerifyGPGKeyOption` payload.
    pub fn validate(&self) -> crate::Result<()> {
        if self.key_id.is_empty() {
            return Err(crate::Error::Validation("key_id is required".to_string()));
        }
        if self.signature.is_empty() {
            return Err(crate::Error::Validation(
                "armored_signature is required".to_string(),
            ));
        }
        Ok(())
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
/// Options for Update User Avatar Option.
pub struct UpdateUserAvatarOption {
    /// base64 encoded image
    pub image: String,
}

impl UpdateUserAvatarOption {
    /// Validate this `UpdateUserAvatarOption` payload.
    pub fn validate(&self) -> crate::Result<()> {
        if self.image.is_empty() {
            return Err(crate::Error::Validation("image is required".to_string()));
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_create_email_option_validate_success() {
        let opt = CreateEmailOption {
            emails: vec!["user@example.com".to_string()],
        };
        assert!(opt.validate().is_ok());
    }

    #[test]
    fn test_create_email_option_validate_empty_list() {
        let opt = CreateEmailOption { emails: vec![] };
        assert!(opt.validate().is_err());
    }

    #[test]
    fn test_create_email_option_validate_empty_email() {
        let opt = CreateEmailOption {
            emails: vec![String::new()],
        };
        assert!(opt.validate().is_err());
    }

    #[test]
    fn test_delete_email_option_validate_success() {
        let opt = DeleteEmailOption {
            emails: vec!["user@example.com".to_string()],
        };
        assert!(opt.validate().is_ok());
    }

    #[test]
    fn test_delete_email_option_validate_empty_list() {
        let opt = DeleteEmailOption { emails: vec![] };
        assert!(opt.validate().is_err());
    }

    #[test]
    fn test_delete_email_option_validate_empty_email() {
        let opt = DeleteEmailOption {
            emails: vec![String::new()],
        };
        assert!(opt.validate().is_err());
    }

    #[test]
    fn test_create_key_option_validate_success() {
        let opt = CreateKeyOption {
            title: "my-key".to_string(),
            key: "ssh-rsa AAAAB3...".to_string(),
            read_only: false,
        };
        assert!(opt.validate().is_ok());
    }

    #[test]
    fn test_create_key_option_validate_empty_key() {
        let opt = CreateKeyOption {
            title: "my-key".to_string(),
            key: String::new(),
            read_only: false,
        };
        assert!(opt.validate().is_err());
    }

    #[test]
    fn test_create_key_option_validate_empty_title() {
        let opt = CreateKeyOption {
            title: String::new(),
            key: "ssh-rsa AAAAB3...".to_string(),
            read_only: false,
        };
        assert!(opt.validate().is_err());
    }

    #[test]
    fn test_create_access_token_option_validate_success() {
        let opt = CreateAccessTokenOption {
            name: "my-token".to_string(),
            scopes: Vec::new(),
        };
        assert!(opt.validate().is_ok());
    }

    #[test]
    fn test_create_access_token_option_validate_empty_name() {
        let opt = CreateAccessTokenOption {
            name: String::new(),
            scopes: Vec::new(),
        };
        assert!(opt.validate().is_err());
    }

    #[test]
    fn test_create_gpg_key_option_validate_success() {
        let opt = CreateGPGKeyOption {
            armored_key: "-----BEGIN PGP PUBLIC KEY BLOCK-----".to_string(),
            signature: None,
        };
        assert!(opt.validate().is_ok());
    }

    #[test]
    fn test_create_gpg_key_option_validate_empty_key() {
        let opt = CreateGPGKeyOption {
            armored_key: String::new(),
            signature: None,
        };
        assert!(opt.validate().is_err());
    }

    #[test]
    fn test_verify_gpg_key_option_validate_success() {
        let opt = VerifyGPGKeyOption {
            key_id: "ABCDEF".to_string(),
            signature: "-----BEGIN PGP SIGNATURE-----".to_string(),
        };
        assert!(opt.validate().is_ok());
    }

    #[test]
    fn test_verify_gpg_key_option_validate_empty_key_id() {
        let opt = VerifyGPGKeyOption {
            key_id: String::new(),
            signature: "sig".to_string(),
        };
        assert!(opt.validate().is_err());
    }

    #[test]
    fn test_verify_gpg_key_option_validate_empty_signature() {
        let opt = VerifyGPGKeyOption {
            key_id: "ABCDEF".to_string(),
            signature: String::new(),
        };
        assert!(opt.validate().is_err());
    }

    #[test]
    fn test_update_user_avatar_option_validate_success() {
        let opt = UpdateUserAvatarOption {
            image: "base64image".to_string(),
        };
        assert!(opt.validate().is_ok());
    }

    #[test]
    fn test_update_user_avatar_option_validate_empty_image() {
        let opt = UpdateUserAvatarOption {
            image: String::new(),
        };
        assert!(opt.validate().is_err());
    }
}