1use crate::client::Client;
2use crate::formatting::title;
3use crate::list::List;
4use crate::trello_error::TrelloError;
5use crate::trello_object::{Renderable, TrelloObject};
6
7use colored::*;
8use serde::Deserialize;
9
10type Result<T> = std::result::Result<T, TrelloError>;
11
12#[derive(Deserialize, Debug, Eq, PartialEq, Clone)]
14#[serde(rename_all = "camelCase")]
15pub struct Board {
16 pub id: String,
17 pub name: String,
18 pub closed: bool,
19 pub url: String,
20 pub lists: Option<Vec<List>>,
21}
22
23impl TrelloObject for Board {
24 fn get_type() -> String {
25 String::from("Board")
26 }
27
28 fn get_name(&self) -> &str {
29 &self.name
30 }
31
32 fn get_fields() -> &'static [&'static str] {
33 &["id", "name", "closed", "url"]
34 }
35}
36
37impl Renderable for Board {
38 fn render(&self) -> String {
39 let mut result = vec![title(&self.name).bold().to_string()];
40 if let Some(lists) = &self.lists {
41 for list in lists {
42 result.push(String::from(""));
43 result.push(list.render());
44 }
45 }
46 result.join("\n")
47 }
48}
49
50impl Board {
51 pub fn new(id: &str, name: &str, lists: Option<Vec<List>>, url: &str) -> Board {
52 Board {
53 id: String::from(id),
54 name: String::from(name),
55 url: String::from(url),
56 lists,
57 closed: false,
58 }
59 }
60
61 pub fn filter(&self, filter_name: &str) -> Board {
62 let mut result = self.clone();
63
64 result.lists = if let Some(lists) = result.lists {
65 Some(lists.into_iter().map(|l| l.filter(filter_name)).collect())
66 } else {
67 None
68 };
69 result
70 }
71
72 pub fn retrieve_nested(&mut self, client: &Client) -> Result<()> {
77 self.lists = Some(List::get_all(client, &self.id, true)?);
78
79 Ok(())
80 }
81
82 pub fn create(client: &Client, name: &str) -> Result<Board> {
83 let url = client.get_trello_url("/1/boards/", &[])?;
84
85 let params = [("name", name)];
86
87 Ok(reqwest::Client::new()
88 .post(url)
89 .form(¶ms)
90 .send()?
91 .error_for_status()?
92 .json()?)
93 }
94
95 pub fn open(client: &Client, board_id: &str) -> Result<Board> {
96 let url = client.get_trello_url(&format!("/1/boards/{}", &board_id), &[])?;
97
98 let params = [("closed", "false")];
99
100 Ok(reqwest::Client::new()
101 .put(url)
102 .form(¶ms)
103 .send()?
104 .error_for_status()?
105 .json()?)
106 }
107
108 pub fn update(client: &Client, board: &Board) -> Result<Board> {
109 let url = client.get_trello_url(&format!("/1/boards/{}/", &board.id), &[])?;
110
111 let params = [("name", &board.name), ("closed", &board.closed.to_string())];
112
113 Ok(reqwest::Client::new()
114 .put(url)
115 .form(¶ms)
116 .send()?
117 .error_for_status()?
118 .json()?)
119 }
120
121 pub fn get_all(client: &Client) -> Result<Vec<Board>> {
122 let url = client.get_trello_url(
123 "/1/members/me/boards/",
124 &[
125 ("filter", "open"),
126 ("fields", &Board::get_fields().join(",")),
127 ],
128 )?;
129
130 Ok(reqwest::get(url)?.error_for_status()?.json()?)
131 }
132
133 pub fn get(client: &Client, board_id: &str) -> Result<Board> {
134 let url = client.get_trello_url(
135 &format!("/1/boards/{}", board_id),
136 &[("fields", &Board::get_fields().join(","))],
137 )?;
138
139 Ok(reqwest::get(url)?.error_for_status()?.json()?)
140 }
141}