Skip to main content

gitea_sdk_rs/options/
oauth2.rs

1// Copyright 2026 infinitete. All rights reserved.
2// Use of this source code is governed by a MIT-style
3// license that can be found in the LICENSE file.
4
5//! Request option types for OAuth2 application API endpoints.
6
7use crate::{Deserialize, Serialize};
8
9#[derive(Debug, Clone, Serialize, Deserialize)]
10/// Options for Create OAuth2 Option.
11pub struct CreateOauth2Option {
12    pub name: String,
13    #[serde(rename = "confidential_client", default)]
14    pub confidential_client: bool,
15    #[serde(
16        rename = "redirect_uris",
17        default,
18        skip_serializing_if = "Vec::is_empty"
19    )]
20    pub redirect_uris: Vec<String>,
21}
22
23#[derive(Debug, Clone, Default)]
24/// Options for List OAuth2 Option.
25pub struct ListOauth2Option {
26    pub list_options: crate::pagination::ListOptions,
27}
28
29impl crate::pagination::QueryEncode for ListOauth2Option {
30    fn query_encode(&self) -> String {
31        self.list_options.query_encode()
32    }
33}
34
35impl CreateOauth2Option {
36    /// Validate this `CreateOauth2Option` payload.
37    pub fn validate(&self) -> crate::Result<()> {
38        if self.name.trim().is_empty() {
39            return Err(crate::Error::Validation("name is required".to_string()));
40        }
41        Ok(())
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48
49    #[test]
50    fn test_create_oauth2_option_validate_success() {
51        let opt = CreateOauth2Option {
52            name: "my-app".to_string(),
53            confidential_client: false,
54            redirect_uris: Vec::new(),
55        };
56        assert!(opt.validate().is_ok());
57    }
58
59    #[test]
60    fn test_create_oauth2_option_validate_empty_name() {
61        let opt = CreateOauth2Option {
62            name: String::new(),
63            confidential_client: false,
64            redirect_uris: Vec::new(),
65        };
66        assert!(opt.validate().is_err());
67    }
68}