Skip to main content

slack_messaging/blocks/data_visualization/charts/
axis_config.rs

1use crate::validators::*;
2
3use serde::Serialize;
4use slack_messaging_derive::Builder;
5
6/// [Axis Config](https://docs.slack.dev/reference/block-kit/blocks/data-visualization-block#axis-config)
7/// representation.
8///
9/// # Fields and Validations
10///
11/// For more details, see the [official
12/// documentation](https://docs.slack.dev/reference/block-kit/blocks/data-visualization-block#axis-config).
13///
14/// | Field | Type | Required | Validation |
15/// |-------|------|----------|------------|
16/// | categories | `Vec<String>` | Yes | Each category label has a maximum of 20 characters |
17/// | x_label | String | No | Max length: 50 characters |
18/// | y_label | String | No | Max length: 50 characters |
19///
20/// # Example
21///
22/// ```
23/// use slack_messaging::blocks::data_visualization::charts::AxisConfig;
24/// # use std::error::Error;
25///
26/// # fn try_main() -> Result<(), Box<dyn Error>> {
27/// let config = AxisConfig::builder()
28///     .categories(vec!["Mon", "Tue", "Wed", "Thu", "Fri"])
29///     .x_label("Days of the Week")
30///     .y_label("Sales")
31///     .build()?;
32///
33/// let expected = serde_json::json!({
34///     "categories": ["Mon", "Tue", "Wed", "Thu", "Fri"],
35///     "x_label": "Days of the Week",
36///     "y_label": "Sales"
37/// });
38///
39/// let json = serde_json::to_value(config).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 AxisConfig {
50    #[builder(push_item = "category", validate("required", "list::each_max_20_chars"))]
51    pub(crate) categories: Option<Vec<String>>,
52
53    #[serde(skip_serializing_if = "Option::is_none")]
54    #[builder(validate("text::max_50"))]
55    pub(crate) x_label: Option<String>,
56
57    #[serde(skip_serializing_if = "Option::is_none")]
58    #[builder(validate("text::max_50"))]
59    pub(crate) y_label: Option<String>,
60}
61
62#[cfg(test)]
63mod tests {
64    use super::*;
65    use crate::errors::*;
66
67    #[test]
68    fn it_implements_builder() {
69        let expected = AxisConfig {
70            categories: Some(vec![
71                "Mon".to_string(),
72                "Tue".to_string(),
73                "Wed".to_string(),
74                "Thu".to_string(),
75                "Fri".to_string(),
76            ]),
77            x_label: Some("Days of the Week".to_string()),
78            y_label: Some("Sales".to_string()),
79        };
80
81        let val = AxisConfig::builder()
82            .set_categories(Some(vec!["Mon", "Tue", "Wed", "Thu", "Fri"]))
83            .set_x_label(Some("Days of the Week"))
84            .set_y_label(Some("Sales"))
85            .build()
86            .unwrap();
87
88        assert_eq!(val, expected);
89
90        let val = AxisConfig::builder()
91            .categories(vec!["Mon", "Tue", "Wed", "Thu", "Fri"])
92            .x_label("Days of the Week")
93            .y_label("Sales")
94            .build()
95            .unwrap();
96
97        assert_eq!(val, expected);
98    }
99
100    #[test]
101    fn it_implements_push_item_method() {
102        let expected = AxisConfig {
103            categories: Some(vec![
104                "Mon".to_string(),
105                "Tue".to_string(),
106                "Wed".to_string(),
107                "Thu".to_string(),
108                "Fri".to_string(),
109            ]),
110            x_label: None,
111            y_label: None,
112        };
113
114        let val = AxisConfig::builder()
115            .category("Mon")
116            .category("Tue")
117            .category("Wed")
118            .category("Thu")
119            .category("Fri")
120            .build()
121            .unwrap();
122
123        assert_eq!(val, expected);
124    }
125
126    #[test]
127    fn it_requires_categories_field() {
128        let err = AxisConfig::builder().build().unwrap_err();
129        assert_eq!(err.object(), "AxisConfig");
130
131        let errors = err.field("categories");
132        assert!(errors.includes(ValidationErrorKind::Required));
133    }
134
135    #[test]
136    fn it_requires_categories_each_max_20_chars() {
137        let err = AxisConfig::builder()
138            .categories(vec!["a".repeat(21), "foo".into()])
139            .build()
140            .unwrap_err();
141        assert_eq!(err.object(), "AxisConfig");
142
143        let errors = err.field("categories");
144        assert!(errors.includes(ValidationErrorKind::MaxTextLength(20)));
145    }
146
147    #[test]
148    fn it_requires_x_label_max_50_chars() {
149        let err = AxisConfig::builder()
150            .categories(vec!["foo"])
151            .x_label("a".repeat(51))
152            .build()
153            .unwrap_err();
154        assert_eq!(err.object(), "AxisConfig");
155
156        let errors = err.field("x_label");
157        assert!(errors.includes(ValidationErrorKind::MaxTextLength(50)));
158    }
159
160    #[test]
161    fn it_requires_y_label_max_50_chars() {
162        let err = AxisConfig::builder()
163            .categories(vec!["foo"])
164            .y_label("a".repeat(51))
165            .build()
166            .unwrap_err();
167        assert_eq!(err.object(), "AxisConfig");
168
169        let errors = err.field("y_label");
170        assert!(errors.includes(ValidationErrorKind::MaxTextLength(50)));
171    }
172}