Skip to main content

gitea_sdk_rs/options/org/
label.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
5use crate::pagination::{ListOptions, QueryEncode};
6use crate::{Deserialize, Serialize};
7
8// ── org_label.go ────────────────────────────────────────────────────────
9
10#[derive(Debug, Clone, Default)]
11pub struct ListOrgLabelsOptions {
12    pub list_options: ListOptions,
13}
14
15impl QueryEncode for ListOrgLabelsOptions {
16    fn query_encode(&self) -> String {
17        self.list_options.query_encode()
18    }
19}
20
21#[derive(Debug, Clone, Default, Serialize, Deserialize)]
22pub struct CreateOrgLabelOption {
23    pub name: String,
24    pub color: String,
25    #[serde(default, skip_serializing_if = "Option::is_none")]
26    pub description: Option<String>,
27    #[serde(default, skip_serializing_if = "Option::is_none")]
28    pub exclusive: Option<bool>,
29}
30
31impl CreateOrgLabelOption {
32    pub fn validate(&self) -> crate::Result<()> {
33        let color = self.color.strip_prefix('#').unwrap_or(&self.color);
34        if color.len() != 6 || !color.chars().all(|c| c.is_ascii_hexdigit()) {
35            return Err(crate::Error::Validation("invalid color format".to_string()));
36        }
37        if self.name.trim().is_empty() {
38            return Err(crate::Error::Validation(
39                "empty name not allowed".to_string(),
40            ));
41        }
42        Ok(())
43    }
44}
45
46#[derive(Debug, Clone, Serialize, Deserialize, Default)]
47pub struct EditOrgLabelOption {
48    #[serde(skip_serializing_if = "Option::is_none")]
49    pub name: Option<String>,
50    #[serde(skip_serializing_if = "Option::is_none")]
51    pub color: Option<String>,
52    #[serde(skip_serializing_if = "Option::is_none")]
53    pub description: Option<String>,
54    #[serde(skip_serializing_if = "Option::is_none")]
55    pub exclusive: Option<bool>,
56}
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    #[test]
63    fn test_create_org_label_option_validate_success() {
64        let opt = CreateOrgLabelOption {
65            name: "bug".to_string(),
66            color: "ff0000".to_string(),
67            description: None,
68            exclusive: None,
69        };
70        assert!(opt.validate().is_ok());
71    }
72
73    #[test]
74    fn test_create_org_label_option_validate_invalid_color() {
75        let opt = CreateOrgLabelOption {
76            name: "bug".to_string(),
77            color: "red".to_string(),
78            description: None,
79            exclusive: None,
80        };
81        assert!(opt.validate().is_err());
82    }
83
84    #[test]
85    fn test_create_org_label_option_validate_empty_name() {
86        let opt = CreateOrgLabelOption {
87            name: String::new(),
88            color: "ff0000".to_string(),
89            description: None,
90            exclusive: None,
91        };
92        assert!(opt.validate().is_err());
93    }
94}