1use crate::client::TrelloClient;
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, _: bool) -> String {
35 self.simple_render()
36 }
37
38 fn simple_render(&self) -> String {
39 format!(" {} ", self.name)
40 .color(Color::White)
41 .on_color(map_color(&self.color))
42 .to_string()
43 }
44}
45
46impl Label {
47 pub fn new(id: &str, name: &str, color: &str) -> Label {
48 Label {
49 id: String::from(id),
50 name: String::from(name),
51 color: String::from(color),
52 }
53 }
54
55 pub fn get_all(client: &TrelloClient, board_id: &str) -> Result<Vec<Label>> {
56 let fields = Label::get_fields().join(",");
57
58 let url = client.config.get_trello_url(
59 &format!("/1/boards/{}/labels", board_id),
60 &[("fields", &fields)],
61 )?;
62
63 Ok(client.client.get(url).send()?.error_for_status()?.json()?)
64 }
65
66 pub fn remove(client: &TrelloClient, card_id: &str, label_id: &str) -> Result<()> {
67 let url = client
68 .config
69 .get_trello_url(&format!("/1/cards/{}/idLabels/{}", card_id, label_id), &[])?;
70
71 client.client.delete(url).send()?.error_for_status()?;
72
73 Ok(())
74 }
75
76 pub fn apply(client: &TrelloClient, card_id: &str, label_id: &str) -> Result<()> {
77 let url = client
78 .config
79 .get_trello_url(&format!("/1/cards/{}/idLabels", card_id), &[])?;
80
81 let params = [("value", label_id)];
82
83 client
84 .client
85 .post(url)
86 .form(¶ms)
87 .send()?
88 .error_for_status()?;
89
90 Ok(())
91 }
92}
93
94fn map_color(color: &str) -> Color {
95 match color {
96 "sky" => Color::TrueColor {
99 r: 0x00,
100 g: 0xc2,
101 b: 0xe0,
102 },
103 "lime" => Color::TrueColor {
104 r: 0x51,
105 g: 0xe8,
106 b: 0x98,
107 },
108 "green" => Color::TrueColor {
109 r: 0x61,
110 g: 0xbd,
111 b: 0x4f,
112 },
113 "purple" => Color::TrueColor {
114 r: 0xc3,
115 g: 0x77,
116 b: 0xe0,
117 },
118 "orange" => Color::TrueColor {
119 r: 0xff,
120 g: 0x9f,
121 b: 0x1a,
122 },
123 "yellow" => Color::TrueColor {
124 r: 0xf2,
125 g: 0xd6,
126 b: 0x00,
127 },
128 "red" => Color::TrueColor {
129 r: 0xeb,
130 g: 0x5a,
131 b: 0x46,
132 },
133 "blue" => Color::TrueColor {
134 r: 0x00,
135 g: 0x79,
136 b: 0xbf,
137 },
138 "pink" => Color::TrueColor {
139 r: 0xff,
140 g: 0x78,
141 b: 0xcb,
142 },
143 "black" => Color::TrueColor {
144 r: 0x34,
145 g: 0x45,
146 b: 0x63,
147 },
148 value => {
149 println!("Unknown color: {}", value);
150 Color::from(color)
151 }
152 }
153}