Skip to main content

atlassian_rust_api/jira/endpoints/components/
create_component.rs

1use std::sync::Arc;
2
3use crate::{Jira, Result, rest_client::RestClient, web::{Endpoint, JsonFormParams}};
4
5#[derive(Debug, Clone)]
6pub struct CreateComponentBuilder {
7	client: Arc<RestClient>,
8	request: CreateComponentRequest,
9}
10
11#[derive(Debug, Clone, Default)]
12struct CreateComponentRequest {
13	name: String,
14	description: Option<String>,
15	lead_user_name: Option<String>,
16	assignee_type: Option<String>,
17	is_assignee_type_valid: bool,
18	project_key: String,
19	project_id: u64,
20}
21
22impl Endpoint for CreateComponentRequest {
23	fn endpoint(&self) -> std::borrow::Cow<'static, str> {
24		"component".into()
25	}
26
27	fn body(&self) -> Result<Option<(&'static str, Vec<u8>)>> {
28		let mut body = JsonFormParams::default();
29		body
30			.push("name", &self.name)
31			.push("project", &self.project_key)
32			.push("projectId", self.project_id)
33			.push("isAssigneeTypeValue", self.is_assignee_type_valid)
34			.push_opt("description", self.description.as_ref())
35			.push_opt("leadUserName", self.lead_user_name.as_ref())
36			.push_opt("assigneeType", self.assignee_type.as_ref());
37		body.into_body()
38	}
39}
40
41impl CreateComponentBuilder {
42	fn new(client: Arc<RestClient>) -> CreateComponentBuilder {
43		CreateComponentBuilder { client, request: CreateComponentRequest::default() }
44	}
45
46	fn name(mut self, name: impl Into<String>) -> CreateComponentBuilder {
47		self.request.name = name.into();
48		self
49	}
50
51	/// Set a description on the new component.
52	pub fn description(mut self, description: impl Into<String>) -> CreateComponentBuilder {
53		self.request.description = Some(description.into());
54		self
55	}
56
57	/// Set the lead of the component.
58	pub fn lead_user_name(mut self, lead_user_name: impl Into<String>) -> CreateComponentBuilder {
59		self.request.lead_user_name = Some(lead_user_name.into());
60		self
61	}
62
63	/// Set the assignee type of the component.
64	pub fn assignee_type(mut self, assignee_type: impl Into<String>) -> CreateComponentBuilder {
65		self.request.assignee_type = Some(assignee_type.into());
66		self
67	}
68
69	/// Set whether the assignee type is valid or not.
70	pub fn is_assignee_type_valid(mut self, is_assignee_type_valid: bool) -> CreateComponentBuilder {
71		self.request.is_assignee_type_valid = is_assignee_type_valid;
72		self
73	}
74
75	fn project_key(mut self, project_key: impl Into<String>) -> CreateComponentBuilder {
76		self.request.project_key = project_key.into();
77		self
78	}
79
80	fn project_id(mut self, project_id: u64) -> CreateComponentBuilder {
81		self.request.project_id = project_id;
82		self
83	}
84
85
86	pub async fn send(self) -> Result<serde_json::Value> {
87		self.client.post(self.request).await
88	}
89}
90
91impl Jira {
92	/// Create a component.
93	pub fn create_component(
94		&self,
95		name: impl Into<String>,
96		project: impl Into<String>,
97		project_id: u64
98	) -> CreateComponentBuilder {
99		CreateComponentBuilder::new(Arc::clone(&self.client)).name(name).project_key(project).project_id(project_id)
100	}
101}