slack_messaging/blocks/table/
setting.rs

1use serde::Serialize;
2use slack_messaging_derive::Builder;
3
4/// Value being set to the align field in [`ColumnSetting`] object.
5#[derive(Debug, Copy, Clone, Serialize, PartialEq)]
6#[serde(rename_all = "lowercase")]
7pub enum ColumnAlignment {
8    Left,
9    Center,
10    Right,
11}
12
13/// Object as an element of the column_settings field in [`Table`](crate::blocks::Table) object.
14///
15/// # Fields and Validations
16///
17/// | Field | Type | Required | Validation |
18/// |-------|------|----------|------------|
19/// | align | [ColumnAlignment] | No | N/A |
20/// | is_wrapped | bool | No | N/A |
21///
22/// # Example
23///
24/// ```
25/// use slack_messaging::blocks::table::{ColumnAlignment, ColumnSetting};
26/// # use std::error::Error;
27///
28/// # fn try_main() -> Result<(), Box<dyn Error>> {
29/// let setting = ColumnSetting::builder()
30///    .align(ColumnAlignment::Center)
31///    .is_wrapped(true)
32///    .build()?;
33///
34/// let expected = serde_json::json!({
35///     "align": "center",
36///     "is_wrapped": true
37/// });
38///
39/// let json = serde_json::to_value(setting).unwrap();
40///
41/// assert_eq!(json, expected);
42/// #     Ok(())
43/// # }
44/// # fn main() {
45/// #     try_main().unwrap()
46/// # }
47/// ```
48#[derive(Debug, Clone, Serialize, PartialEq, Builder)]
49pub struct ColumnSetting {
50    #[serde(skip_serializing_if = "Option::is_none")]
51    pub(crate) align: Option<ColumnAlignment>,
52
53    #[serde(skip_serializing_if = "Option::is_none")]
54    pub(crate) is_wrapped: Option<bool>,
55}
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    #[test]
62    fn it_implements_builder() {
63        let expected = ColumnSetting {
64            align: Some(ColumnAlignment::Left),
65            is_wrapped: Some(true),
66        };
67
68        let val = ColumnSetting::builder()
69            .set_align(Some(ColumnAlignment::Left))
70            .set_is_wrapped(Some(true))
71            .build()
72            .unwrap();
73
74        assert_eq!(val, expected);
75
76        let val = ColumnSetting::builder()
77            .align(ColumnAlignment::Left)
78            .is_wrapped(true)
79            .build()
80            .unwrap();
81
82        assert_eq!(val, expected);
83    }
84}