1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use crate::client::TrelloClient;
use crate::trello_error::TrelloError;
use crate::trello_object::{Renderable, TrelloObject};
use colored::*;
use serde::Deserialize;
type Result<T> = std::result::Result<T, TrelloError>;
#[derive(Deserialize, Debug, Eq, PartialEq, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Label {
pub id: String,
pub name: String,
pub color: String,
}
impl TrelloObject for Label {
fn get_type() -> String {
String::from("Label")
}
fn get_name(&self) -> &str {
&self.name
}
fn get_fields() -> &'static [&'static str] {
&["id", "name", "color"]
}
}
impl Renderable for Label {
fn render(&self) -> String {
self.simple_render()
}
fn simple_render(&self) -> String {
format!(" {} ", self.name)
.color(Color::White)
.on_color(map_color(&self.color))
.to_string()
}
}
impl Label {
pub fn new(id: &str, name: &str, color: &str) -> Label {
Label {
id: String::from(id),
name: String::from(name),
color: String::from(color),
}
}
pub fn get_all(client: &TrelloClient, board_id: &str) -> Result<Vec<Label>> {
let fields = Label::get_fields().join(",");
let url = client.config.get_trello_url(
&format!("/1/boards/{}/labels", board_id),
&[("fields", &fields)],
)?;
Ok(client.client.get(url).send()?.error_for_status()?.json()?)
}
pub fn remove(client: &TrelloClient, card_id: &str, label_id: &str) -> Result<()> {
let url = client
.config
.get_trello_url(&format!("/1/cards/{}/idLabels/{}", card_id, label_id), &[])?;
client.client.delete(url).send()?.error_for_status()?;
Ok(())
}
pub fn apply(client: &TrelloClient, card_id: &str, label_id: &str) -> Result<()> {
let url = client
.config
.get_trello_url(&format!("/1/cards/{}/idLabels", card_id), &[])?;
let params = [("value", label_id)];
client
.client
.post(url)
.form(¶ms)
.send()?
.error_for_status()?;
Ok(())
}
}
fn map_color(color: &str) -> Color {
match color {
"sky" => Color::TrueColor {
r: 0x00,
g: 0xc2,
b: 0xe0,
},
"lime" => Color::TrueColor {
r: 0x51,
g: 0xe8,
b: 0x98,
},
"green" => Color::TrueColor {
r: 0x61,
g: 0xbd,
b: 0x4f,
},
"purple" => Color::TrueColor {
r: 0xc3,
g: 0x77,
b: 0xe0,
},
"orange" => Color::TrueColor {
r: 0xff,
g: 0x9f,
b: 0x1a,
},
"yellow" => Color::TrueColor {
r: 0xf2,
g: 0xd6,
b: 0x00,
},
"red" => Color::TrueColor {
r: 0xeb,
g: 0x5a,
b: 0x46,
},
"blue" => Color::TrueColor {
r: 0x00,
g: 0x79,
b: 0xbf,
},
"pink" => Color::TrueColor {
r: 0xff,
g: 0x78,
b: 0xcb,
},
"black" => Color::TrueColor {
r: 0x34,
g: 0x45,
b: 0x63,
},
value => {
println!("Unknown color: {}", value);
Color::from(color)
}
}
}