1use crate::client::Client;
2use crate::trello_error::TrelloError;
3use crate::trello_object::{Renderable, TrelloObject};
4
5use colored::*;
6use serde::Deserialize;
7
8type Result<T> = std::result::Result<T, TrelloError>;
9
10#[derive(Deserialize, Debug, Eq, PartialEq, Clone)]
12#[serde(rename_all = "camelCase")]
13pub struct Label {
14 pub id: String,
15 pub name: String,
16 pub color: String,
17}
18
19impl TrelloObject for Label {
20 fn get_type() -> String {
21 String::from("Label")
22 }
23
24 fn get_name(&self) -> &str {
25 &self.name
26 }
27
28 fn get_fields() -> &'static [&'static str] {
29 &["id", "name", "color"]
30 }
31}
32
33impl Renderable for Label {
34 fn render(&self) -> String {
35 format!("[{}]", self.colored_name())
36 }
37}
38
39impl Label {
40 pub fn new(id: &str, name: &str, color: &str) -> Label {
41 Label {
42 id: String::from(id),
43 name: String::from(name),
44 color: String::from(color),
45 }
46 }
47
48 pub fn colored_name(&self) -> ColoredString {
49 self.name.color(map_color(&self.color))
50 }
51
52 pub fn get_all(client: &Client, board_id: &str) -> Result<Vec<Label>> {
53 let fields = Label::get_fields().join(",");
54
55 let url = client.get_trello_url(
56 &format!("/1/boards/{}/labels", board_id),
57 &[("fields", &fields)],
58 )?;
59
60 Ok(reqwest::get(url)?.error_for_status()?.json()?)
61 }
62
63 pub fn remove(client: &Client, card_id: &str, label_id: &str) -> Result<()> {
64 let url =
65 client.get_trello_url(&format!("/1/cards/{}/idLabels/{}", card_id, label_id), &[])?;
66
67 reqwest::Client::new()
68 .delete(url)
69 .send()?
70 .error_for_status()?;
71
72 Ok(())
73 }
74
75 pub fn apply(client: &Client, card_id: &str, label_id: &str) -> Result<()> {
76 let url = client.get_trello_url(&format!("/1/cards/{}/idLabels", card_id), &[])?;
77
78 let params = [("value", label_id)];
79
80 reqwest::Client::new()
81 .post(url)
82 .form(¶ms)
83 .send()?
84 .error_for_status()?;
85
86 Ok(())
87 }
88}
89
90fn map_color(color: &str) -> &str {
91 match color {
92 "sky" => "cyan",
93 "lime" => "green",
94 "orange" => "yellow",
95 "black" => "bright black",
97 _ => color,
98 }
99}