Skip to main content

bitbucket_cli/models/
project.rs

1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3
4use super::user::{User, Workspace};
5
6/// A Bitbucket project as returned by the workspace projects endpoints.
7///
8/// Named `ProjectDetail` to avoid colliding with the lightweight
9/// [`Project`](super::repo::Project) reference embedded in repositories.
10#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct ProjectDetail {
12    pub uuid: Option<String>,
13    pub key: String,
14    pub name: String,
15    pub description: Option<String>,
16    pub is_private: Option<bool>,
17    pub created_on: Option<DateTime<Utc>>,
18    pub updated_on: Option<DateTime<Utc>>,
19}
20
21/// Body for creating a new project in a workspace.
22#[derive(Debug, Clone, Serialize)]
23#[non_exhaustive]
24pub struct CreateProjectRequest {
25    pub name: String,
26    pub key: String,
27    #[serde(skip_serializing_if = "Option::is_none")]
28    pub description: Option<String>,
29    #[serde(skip_serializing_if = "Option::is_none")]
30    pub is_private: Option<bool>,
31}
32
33/// Partial update for an existing project. Unset fields are omitted from the
34/// request body so the corresponding settings are left untouched.
35#[derive(Debug, Clone, Default, Serialize)]
36#[non_exhaustive]
37pub struct UpdateProjectRequest {
38    #[serde(skip_serializing_if = "Option::is_none")]
39    pub name: Option<String>,
40    #[serde(skip_serializing_if = "Option::is_none")]
41    pub description: Option<String>,
42    #[serde(skip_serializing_if = "Option::is_none")]
43    pub is_private: Option<bool>,
44}
45
46impl UpdateProjectRequest {
47    /// True when no field is set, i.e. the update would be a no-op.
48    pub fn is_empty(&self) -> bool {
49        self.name.is_none() && self.description.is_none() && self.is_private.is_none()
50    }
51}
52
53/// A user's membership in a workspace, as returned by
54/// `GET /workspaces/{workspace}/members`.
55#[derive(Debug, Clone, Serialize, Deserialize)]
56pub struct WorkspaceMembership {
57    pub user: Option<User>,
58    pub workspace: Option<Workspace>,
59}
60
61/// A permission grant on a workspace (or one of its repositories), as
62/// returned by `GET /workspaces/{workspace}/permissions`.
63#[derive(Debug, Clone, Serialize, Deserialize)]
64pub struct WorkspacePermission {
65    pub user: Option<User>,
66    pub permission: Option<String>,
67}
68
69#[cfg(test)]
70mod tests {
71    use super::{CreateProjectRequest, ProjectDetail, UpdateProjectRequest};
72
73    #[test]
74    fn project_detail_deserializes_with_minimal_fields() {
75        let payload = r#"{ "key": "PROJ", "name": "My Project" }"#;
76        let project: ProjectDetail = serde_json::from_str(payload).unwrap();
77        assert_eq!(project.key, "PROJ");
78        assert_eq!(project.name, "My Project");
79        assert!(project.uuid.is_none());
80        assert!(project.is_private.is_none());
81    }
82
83    #[test]
84    fn create_request_omits_unset_options() {
85        let request = CreateProjectRequest {
86            name: "My Project".to_string(),
87            key: "PROJ".to_string(),
88            description: None,
89            is_private: Some(true),
90        };
91
92        let json = serde_json::to_value(&request).unwrap();
93        assert_eq!(
94            json,
95            serde_json::json!({ "name": "My Project", "key": "PROJ", "is_private": true })
96        );
97    }
98
99    #[test]
100    fn update_request_omits_unset_fields() {
101        let request = UpdateProjectRequest {
102            description: Some("new description".to_string()),
103            ..Default::default()
104        };
105
106        let json = serde_json::to_value(&request).unwrap();
107        assert_eq!(
108            json,
109            serde_json::json!({ "description": "new description" })
110        );
111    }
112
113    #[test]
114    fn update_request_is_empty() {
115        assert!(UpdateProjectRequest::default().is_empty());
116
117        assert!(
118            !UpdateProjectRequest {
119                name: Some("Renamed".to_string()),
120                ..Default::default()
121            }
122            .is_empty()
123        );
124        assert!(
125            !UpdateProjectRequest {
126                description: Some(String::new()),
127                ..Default::default()
128            }
129            .is_empty()
130        );
131        assert!(
132            !UpdateProjectRequest {
133                is_private: Some(false),
134                ..Default::default()
135            }
136            .is_empty()
137        );
138    }
139}