notionrs_types/object/block/table.rs
1use serde::{Deserialize, Serialize};
2
3/// <https://developers.notion.com/reference/block#table>
4///
5/// Table block objects are parent blocks for table row children.
6/// Table block objects contain the following fields within the table property:
7#[derive(Deserialize, Serialize, Debug, Default, Clone, notionrs_macro::Setter)]
8pub struct TableBlock {
9    /// The number of columns in the table.
10    /// Note that this cannot be changed via the public API once a table is created.
11    pub table_width: u16,
12
13    /// Whether the table has a column header. If true,
14    /// then the first row in the table appears visually distinct from the other rows.
15    pub has_column_header: bool,
16
17    /// Whether the table has a header row. If true,
18    /// then the first column in the table appears visually distinct from the other columns.
19    pub has_row_header: bool,
20
21    /// Only `table_row` can be specified.
22    /// It can only be specified when making a block creation request.
23    /// If you need to retrieve the child blocks, you will have to send a request to this block again.
24    #[serde(skip_serializing_if = "Option::is_none")]
25    pub children: Option<Vec<super::Block>>,
26}
27
28impl From<u16> for TableBlock {
29    fn from(table_width: u16) -> Self {
30        Self::default().table_width(table_width)
31    }
32}
33
34impl std::fmt::Display for TableBlock {
35    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
36        write!(
37            f,
38            "TableBlock {{ table_width: {}, has_column_header: {}, has_row_header: {} }}",
39            self.table_width, self.has_column_header, self.has_row_header
40        )
41    }
42}
43
44// # --------------------------------------------------------------------------------
45//
46// unit test
47//
48// # --------------------------------------------------------------------------------
49
50#[cfg(test)]
51mod unit_tests {
52
53    use super::TableBlock;
54
55    #[test]
56    fn deserialize_block_table() {
57        let json_data = r#"
58        {
59            "table_width": 2,
60            "has_column_header": false,
61            "has_row_header": false
62        }
63        "#;
64
65        let table = serde_json::from_str::<TableBlock>(json_data).unwrap();
66
67        assert_eq!(table.table_width, 2);
68        assert!(!table.has_column_header);
69        assert!(!table.has_row_header);
70    }
71}