gitlab/api/projects/
project.rs1use derive_builder::Builder;
8
9use crate::api::common::NameOrId;
10use crate::api::endpoint_prelude::*;
11
12#[derive(Debug, Builder, Clone)]
14#[builder(setter(strip_option))]
15pub struct Project<'a> {
16 #[builder(setter(into))]
18 project: NameOrId<'a>,
19
20 #[builder(default)]
22 statistics: Option<bool>,
23 #[builder(default)]
25 license: Option<bool>,
26 #[builder(default)]
28 with_custom_attributes: Option<bool>,
29}
30
31impl<'a> Project<'a> {
32 pub fn builder() -> ProjectBuilder<'a> {
34 ProjectBuilder::default()
35 }
36}
37
38impl Endpoint for Project<'_> {
39 fn method(&self) -> Method {
40 Method::GET
41 }
42
43 fn endpoint(&self) -> Cow<'static, str> {
44 format!("projects/{}", self.project).into()
45 }
46
47 fn parameters(&self) -> QueryParams {
48 let mut params = QueryParams::default();
49
50 params
51 .push_opt("statistics", self.statistics)
52 .push_opt("license", self.license)
53 .push_opt("with_custom_attributes", self.with_custom_attributes);
54
55 params
56 }
57}
58
59#[cfg(test)]
60mod tests {
61 use crate::api::projects::{Project, ProjectBuilderError};
62 use crate::api::{self, Query};
63 use crate::test::client::{ExpectedUrl, SingleTestClient};
64
65 #[test]
66 fn project_is_necessary() {
67 let err = Project::builder().build().unwrap_err();
68 crate::test::assert_missing_field!(err, ProjectBuilderError, "project");
69 }
70
71 #[test]
72 fn project_is_sufficient() {
73 Project::builder().project(1).build().unwrap();
74 }
75
76 #[test]
77 fn endpoint() {
78 let endpoint = ExpectedUrl::builder()
79 .endpoint("projects/simple%2Fproject")
80 .build()
81 .unwrap();
82 let client = SingleTestClient::new_raw(endpoint, "");
83
84 let endpoint = Project::builder()
85 .project("simple/project")
86 .build()
87 .unwrap();
88 api::ignore(endpoint).query(&client).unwrap();
89 }
90
91 #[test]
92 fn endpoint_statistics() {
93 let endpoint = ExpectedUrl::builder()
94 .endpoint("projects/simple%2Fproject")
95 .add_query_params(&[("statistics", "true")])
96 .build()
97 .unwrap();
98 let client = SingleTestClient::new_raw(endpoint, "");
99
100 let endpoint = Project::builder()
101 .project("simple/project")
102 .statistics(true)
103 .build()
104 .unwrap();
105 api::ignore(endpoint).query(&client).unwrap();
106 }
107
108 #[test]
109 fn endpoint_license() {
110 let endpoint = ExpectedUrl::builder()
111 .endpoint("projects/simple%2Fproject")
112 .add_query_params(&[("license", "true")])
113 .build()
114 .unwrap();
115 let client = SingleTestClient::new_raw(endpoint, "");
116
117 let endpoint = Project::builder()
118 .project("simple/project")
119 .license(true)
120 .build()
121 .unwrap();
122 api::ignore(endpoint).query(&client).unwrap();
123 }
124
125 #[test]
126 fn endpoint_with_custom_attributes() {
127 let endpoint = ExpectedUrl::builder()
128 .endpoint("projects/simple%2Fproject")
129 .add_query_params(&[("with_custom_attributes", "true")])
130 .build()
131 .unwrap();
132 let client = SingleTestClient::new_raw(endpoint, "");
133
134 let endpoint = Project::builder()
135 .project("simple/project")
136 .with_custom_attributes(true)
137 .build()
138 .unwrap();
139 api::ignore(endpoint).query(&client).unwrap();
140 }
141}