Skip to main content

wekan_cli/client/boards/
labels.rs

1use reqwest::header::ACCEPT;
2use secrecy::{ExposeSecret, SecretString};
3use serde::Serialize;
4
5use super::super::{ClientError, WekanClient, transport::decode_success};
6
7pub const LABEL_COLORS: &[&str] = &[
8    "white",
9    "green",
10    "yellow",
11    "orange",
12    "red",
13    "purple",
14    "blue",
15    "sky",
16    "lime",
17    "pink",
18    "black",
19    "silver",
20    "peachpuff",
21    "crimson",
22    "plum",
23    "darkgreen",
24    "slateblue",
25    "magenta",
26    "gold",
27    "navy",
28    "gray",
29    "saddlebrown",
30    "paleturquoise",
31    "mistyrose",
32    "indigo",
33];
34
35#[derive(Debug, Serialize)]
36pub struct AddBoardLabelRequest {
37    #[serde(skip_serializing_if = "Option::is_none")]
38    pub name: Option<String>,
39    #[serde(skip_serializing_if = "Option::is_none")]
40    pub color: Option<String>,
41}
42
43impl WekanClient {
44    pub async fn add_board_label(
45        &self,
46        board: &str,
47        label: &AddBoardLabelRequest,
48        token: &SecretString,
49    ) -> Result<String, ClientError> {
50        super::members::validate_id(board)?;
51        let body = self
52            .execute_success_request(
53                self.http
54                    .put(self.board_endpoint(board, Some("labels"))?)
55                    .header(ACCEPT, "application/json")
56                    .bearer_auth(token.expose_secret())
57                    .json(&serde_json::json!({"label": label})),
58            )
59            .await?;
60        let id: String = decode_success(&body, "board label addition", true)?;
61        if id.is_empty() {
62            return Err(ClientError::Protocol {
63                diagnostic: None,
64                message: "Wekan returned an empty label ID".into(),
65                success_status_received: true,
66            });
67        }
68        Ok(id)
69    }
70}