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
//! Projects interface
use std::collections::HashMap;

use url::form_urlencoded;

use crate::{AzureClient, Future};
pub use new_project_options::{ProjectsOptions, ProjectsOptionsBuilder};
pub use new_project_response::ProjectStatus;
pub use project_list_response::ProjectsResponse;
pub use project_response::ProjectResponse;

pub struct Projects {
    ops: AzureClient,
}

impl Projects {
    #[doc(hidden)]
    pub fn new(ops: AzureClient) -> Self {
        Self { ops }
    }

    /// Create a new project
    pub fn create(&self, project: &ProjectsOptions) -> Future<ProjectStatus> {
        self.ops.post(&self.path(""), json!(project))
    }

    fn path(&self, more: &str) -> String {
        format!("/{}/{}/_apis/projects", self.ops.org, more)
    }

    /// List existing projects
    pub fn list(&self, options: &ProjectOptions) -> Future<ProjectsResponse> {
        let mut uri = vec![self.path("")];
        if let Some(query) = options.serialize() {
            uri.push(query);
        }

        self.ops.get(&uri.join("?"))
    }
}
mod new_project_response {
    use serde::*;
    #[derive(Debug, Default, Deserialize)]
    pub struct ProjectStatus {
        pub id: String,
        pub status: String,
        pub url: String,
    }
}

mod new_project_options {
    use serde::*;
    // #[serde(skip_serializing_if = "Option::is_none")]

    #[derive(Debug, Default, Serialize)]
    pub struct ProjectsOptions {
        pub name: String,
        pub description: String,
        pub capabilities: Capabilities,
    }
    #[derive(Debug, Default, Serialize)]
    pub struct Capabilities {
        pub versioncontrol: Versioncontrol,
        #[serde(rename = "processTemplate")]
        pub process_template: ProcessTemplate,
    }
    #[derive(Debug, Default, Serialize)]
    pub struct Versioncontrol {
        #[serde(rename = "sourceControlType")]
        pub source_control_type: String,
    }
    #[derive(Debug, Default, Serialize)]
    pub struct ProcessTemplate {
        #[serde(rename = "templateTypeId")]
        pub template_type_id: String,
    }

    pub struct ProjectsOptionsBuilder(ProjectsOptions);

    impl ProjectsOptionsBuilder {
        pub(crate) fn new<N>(name: N) -> Self
        where
            N: Into<String>,
        {
            ProjectsOptionsBuilder(ProjectsOptions {
                name: name.into(),
                ..Default::default()
            })
        }

        pub fn description<D>(&mut self, description: D) -> &mut Self
        where
            D: Into<String>,
        {
            self.0.description = description.into();
            self
        }

        pub fn source_control_type<H>(&mut self, source_control_type: H) -> &mut Self
        where
            H: Into<String>,
        {
            self.0.capabilities.versioncontrol.source_control_type = source_control_type.into();
            self
        }

        pub fn template_type_id(&mut self, template_type_id: String) -> &mut Self {
            self.0.capabilities.process_template.template_type_id = template_type_id;
            self
        }

        pub fn build(&self) -> ProjectsOptions {
            ProjectsOptions::new(
                self.0.name.as_str(),
                self.0.description.as_str(),
                self.0
                    .capabilities
                    .versioncontrol
                    .source_control_type
                    .clone(),
                self.0
                    .capabilities
                    .process_template
                    .template_type_id
                    .clone(),
            )
        }
    }

    impl ProjectsOptions {
        #[allow(clippy::too_many_arguments)] // exempted
        pub fn new<N, D, H, E>(
            name: N,
            description: D,
            source_control_type: H,
            template_type_id: E,
        ) -> Self
        where
            N: Into<String>,
            D: Into<String>,
            H: Into<String>,
            E: Into<String>,
        {
            ProjectsOptions {
                name: name.into(),
                description: description.into(),
                capabilities: Capabilities {
                    versioncontrol: Versioncontrol {
                        source_control_type: source_control_type.into(),
                    },
                    process_template: ProcessTemplate {
                        template_type_id: template_type_id.into(),
                    },
                },
            }
        }

        pub fn builder<N: Into<String>>(name: N) -> ProjectsOptionsBuilder {
            ProjectsOptionsBuilder::new(name)
        }
    }
}

mod project_list_response {
    use serde::Deserialize;
    #[serde(rename_all = "camelCase")]
    #[derive(Debug, Deserialize)]
    pub struct ProjectsResponse {
        pub value: Vec<Value>,
        pub count: i64,
    }
    #[serde(rename_all = "camelCase")]
    #[derive(Debug, Deserialize)]
    pub struct Value {
        pub id: String,
        pub name: String,
        pub url: String,
        pub state: String,
        pub revision: i64,
        pub visibility: String,
        pub last_update_time: String,
    }
}

#[derive(Default)]
pub struct ProjectOptions {
    params: HashMap<&'static str, String>,
}

impl ProjectOptions {
    /// serialize options as a string. returns None if no options are defined
    pub fn serialize(&self) -> Option<String> {
        if self.params.is_empty() {
            None
        } else {
            let encoded: String = form_urlencoded::Serializer::new(String::new())
                .extend_pairs(&self.params)
                .finish();
            Some(encoded)
        }
    }
}

pub struct Project {
    ops: AzureClient,
    project: String,
}

impl Project {
    #[doc(hidden)]
    pub fn new<P>(ops: AzureClient, project: P) -> Self
    where
        P: Into<String>,
    {
        Project {
            ops: ops,
            project: project.into(),
        }
    }

    pub fn get(&self) -> Future<ProjectResponse> {
        self.ops.get(&self.path(""))
    }

    pub fn delete(&self) -> Future<ProjectStatus> {
        self.ops.delete(&self.path(""))
    }

    // GET https://dev.azure.com/{organization}/_apis/projects/{projectId}?api-version=5.1
    fn path(&self, more: &str) -> String {
        format!("/{}/_apis/projects/{}{}", self.ops.org, self.project, more)
    }
}

mod project_response {
    use serde::Deserialize;
    #[serde(rename_all = "camelCase")]
    #[derive(Debug, Deserialize)]
    pub struct ProjectResponse {
        pub id: String,
        pub name: String,
        pub url: String,
        pub state: String,
        pub revision: i64,
        #[serde(rename = "_links")]
        pub links: Links,
        pub visibility: String,
        #[serde(rename = "defaultTeam")]
        pub default_team: DefaultTeam,
        #[serde(rename = "lastUpdateTime")]
        pub last_update_time: String,
    }

    #[derive(Debug, Deserialize)]
    pub struct Links {
        #[serde(rename = "self")]
        pub self_field: Href,
        pub collection: Collection,
        pub web: Web,
    }

    #[derive(Debug, Deserialize)]
    pub struct Href {
        pub href: String,
    }

    #[derive(Debug, Deserialize)]
    pub struct Collection {
        pub href: String,
    }

    #[derive(Debug, Deserialize)]
    pub struct Web {
        pub href: String,
    }

    #[derive(Debug, Deserialize)]
    pub struct DefaultTeam {
        pub id: String,
        pub name: String,
        pub url: String,
    }
}