Skip to main content

trello/
board.rs

1use crate::client::TrelloClient;
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// https://developer.atlassian.com/cloud/trello/guides/rest-api/object-definitions/#board-object
13#[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, headers: bool) -> String {
39        let mut result = match headers {
40            true => vec![title(&self.name).bold().to_string()],
41            false => vec![],
42        };
43        if let Some(lists) = &self.lists {
44            for list in lists {
45                result.push(String::from(""));
46                result.push(list.render(headers));
47            }
48        }
49        result.join("\n")
50    }
51
52    fn simple_render(&self) -> String {
53        self.name.clone()
54    }
55}
56
57impl Board {
58    pub fn new(id: &str, name: &str, lists: Option<Vec<List>>, url: &str) -> Board {
59        Board {
60            id: String::from(id),
61            name: String::from(name),
62            url: String::from(url),
63            lists,
64            closed: false,
65        }
66    }
67
68    pub fn filter(&self, filter_name: &str) -> Board {
69        let mut result = self.clone();
70
71        result.lists = result
72            .lists
73            .map(|lists| lists.into_iter().map(|l| l.filter(filter_name)).collect());
74        result
75    }
76
77    /// Retrieves any missing nested content for the given board. This potentially
78    /// means one or more network requests in order to retrieve the data. The Board
79    /// will be mutated to include all its associated lists. The lists will also in turn
80    /// contain the associated card resources.
81    pub fn retrieve_nested(&mut self, client: &TrelloClient) -> Result<()> {
82        if self.lists.is_none() {
83            debug!("Retrieving nested data for board: {}", self.id);
84            self.lists = Some(List::get_all(client, &self.id, true)?);
85        } else {
86            debug!("No need to retrieve nested data");
87        }
88        Ok(())
89    }
90
91    pub fn create(client: &TrelloClient, name: &str) -> Result<Board> {
92        let url = client.config.get_trello_url("/1/boards/", &[])?;
93
94        let params = [("name", name)];
95
96        Ok(client
97            .client
98            .post(url)
99            .form(&params)
100            .send()?
101            .error_for_status()?
102            .json()?)
103    }
104
105    pub fn open(client: &TrelloClient, board_id: &str) -> Result<Board> {
106        let url = client
107            .config
108            .get_trello_url(&format!("/1/boards/{}", &board_id), &[])?;
109
110        let params = [("closed", "false")];
111
112        Ok(client
113            .client
114            .put(url)
115            .form(&params)
116            .send()?
117            .error_for_status()?
118            .json()?)
119    }
120
121    pub fn update(client: &TrelloClient, board: &Board) -> Result<Board> {
122        let url = client
123            .config
124            .get_trello_url(&format!("/1/boards/{}/", &board.id), &[])?;
125
126        let params = [("name", &board.name), ("closed", &board.closed.to_string())];
127
128        Ok(client
129            .client
130            .put(url)
131            .form(&params)
132            .send()?
133            .error_for_status()?
134            .json()?)
135    }
136
137    pub fn get_all(client: &TrelloClient) -> Result<Vec<Board>> {
138        let url = client.config.get_trello_url(
139            "/1/members/me/boards/",
140            &[
141                ("filter", "open"),
142                ("fields", &Board::get_fields().join(",")),
143            ],
144        )?;
145
146        Ok(client.client.get(url).send()?.error_for_status()?.json()?)
147    }
148
149    pub fn get(client: &TrelloClient, board_id: &str) -> Result<Board> {
150        let url = client.config.get_trello_url(
151            &format!("/1/boards/{}", board_id),
152            &[("fields", &Board::get_fields().join(","))],
153        )?;
154
155        Ok(client.client.get(url).send()?.error_for_status()?.json()?)
156    }
157}