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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use crate::client::Client;
use crate::formatting::header;
use crate::label::Label;
use crate::trello_error::TrelloError;
use crate::trello_object::{Renderable, TrelloObject};
use serde::Deserialize;
use std::str::FromStr;
type Result<T> = std::result::Result<T, TrelloError>;
#[derive(Deserialize, Debug, Eq, PartialEq, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Card {
pub id: String,
pub name: String,
pub desc: String,
pub closed: bool,
pub url: String,
pub labels: Option<Vec<Label>>,
}
impl TrelloObject for Card {
fn get_type() -> String {
String::from("Card")
}
fn get_name(&self) -> &str {
&self.name
}
fn get_fields() -> &'static [&'static str] {
&["id", "name", "desc", "labels", "closed", "url"]
}
}
impl Renderable for Card {
fn render(&self) -> String {
[header(&self.name, "=").as_str(), &self.desc].join("\n")
}
}
#[derive(Debug, PartialEq, Eq)]
pub struct CardContents {
pub name: String,
pub desc: String,
}
impl FromStr for CardContents {
type Err = TrelloError;
fn from_str(value: &str) -> Result<CardContents> {
let mut contents = value.split('\n').collect::<Vec<&str>>();
trace!("{:?}", contents);
let mut name = vec![contents.remove(0)];
let mut found = false;
while !contents.is_empty() {
let line = contents.remove(0);
if line.chars().take_while(|c| c == &'=').collect::<String>() != line {
name.push(line);
} else {
found = true;
break;
}
}
if !found {
return Err(TrelloError::CardParse(
"Unable to find name delimiter '===='".to_owned(),
));
}
let name = name.join("\n");
let desc = contents.join("\n");
Ok(CardContents { name, desc })
}
}
impl Card {
pub fn new(id: &str, name: &str, desc: &str, labels: Option<Vec<Label>>, url: &str) -> Card {
Card {
id: String::from(id),
name: String::from(name),
desc: String::from(desc),
url: String::from(url),
labels,
closed: false,
}
}
pub fn get(client: &Client, card_id: &str) -> Result<Card> {
let url = client.get_trello_url(&format!("/1/cards/{}", card_id), &[])?;
Ok(reqwest::get(url)?.error_for_status()?.json()?)
}
pub fn create(client: &Client, list_id: &str, card: &Card) -> Result<Card> {
let url = client.get_trello_url("/1/cards/", &[])?;
let params: [(&str, &str); 3] = [
("name", &card.name),
("desc", &card.desc),
("idList", list_id),
];
Ok(reqwest::Client::new()
.post(url)
.form(¶ms)
.send()?
.error_for_status()?
.json()?)
}
pub fn open(client: &Client, card_id: &str) -> Result<Card> {
let url = client.get_trello_url(&format!("/1/cards/{}", &card_id), &[])?;
let params = [("closed", "false")];
Ok(reqwest::Client::new()
.put(url)
.form(¶ms)
.send()?
.error_for_status()?
.json()?)
}
pub fn update(client: &Client, card: &Card) -> Result<Card> {
let url = client.get_trello_url(&format!("/1/cards/{}/", &card.id), &[])?;
let params = [
("name", &card.name),
("desc", &card.desc),
("closed", &card.closed.to_string()),
];
Ok(reqwest::Client::new()
.put(url)
.form(¶ms)
.send()?
.error_for_status()?
.json()?)
}
pub fn get_all(client: &Client, list_id: &str) -> Result<Vec<Card>> {
let url = client.get_trello_url(
&format!("/1/lists/{}/cards/", list_id),
&[("fields", &Card::get_fields().join(","))],
)?;
Ok(reqwest::get(url)?.error_for_status()?.json()?)
}
}