sfr_types/element.rs
1//! The type that represents a block element.
2//!
3//! <https://api.slack.com/reference/block-kit/block-elements>
4//!
5//! This crate only supports `Button` now.
6
7use crate::ConfirmObect;
8use crate::TextObject;
9use serde::Serialize;
10
11/// A unique identifier for an action.
12#[derive(Serialize, Debug, Clone)]
13pub struct ActionId(pub String);
14
15/// The type that represents a block element.
16///
17/// <https://api.slack.com/reference/block-kit/block-elements>
18///
19/// This crate only supports `Button` now.
20#[derive(Serialize, Debug, Clone)]
21#[serde(tag = "type", rename_all = "snake_case")]
22pub enum Element {
23 /// Button element.
24 ///
25 /// <https://api.slack.com/reference/block-kit/block-elements#button>
26 Button(ButtonElement),
27}
28
29/// Button element.
30///
31/// <https://api.slack.com/reference/block-kit/block-elements#button>
32#[derive(Serialize, Debug, Clone)]
33#[serde(rename_all = "snake_case")]
34pub struct ButtonElement {
35 /// A [text object](https://api.slack.com/reference/block-kit/composition-objects#text) that defines the button's text.
36 pub text: TextObject,
37
38 /// An identifier for this action.
39 #[serde(skip_serializing_if = "Option::is_none")]
40 pub action_id: Option<ActionId>,
41
42 /// A URL to load in the user's browser when the button is clicked.
43 #[serde(skip_serializing_if = "Option::is_none")]
44 pub url: Option<String>,
45
46 /// The value to send along with the [interaction payload](https://api.slack.com/interactivity/handling#payloads).
47 #[serde(skip_serializing_if = "Option::is_none")]
48 pub value: Option<String>,
49
50 /// `primary` gives buttons a green outline and text, ideal for affirmation or confirmation actions.
51 #[serde(skip_serializing_if = "ButtonStyle::is_default")]
52 pub style: ButtonStyle,
53
54 /// A [confirm object](https://api.slack.com/reference/block-kit/composition-objects#confirm) that defines an optional confirmation dialog after the button is clicked.
55 #[serde(skip_serializing_if = "Option::is_none")]
56 pub confirm: Option<ConfirmObect>,
57 // accessibility_label: Option<String>,
58}
59
60/// The enumerated value that represents the button style.
61#[derive(Serialize, Debug, Clone)]
62#[serde(rename_all = "lowercase")]
63pub enum ButtonStyle {
64 /// No value in the field.
65 Default,
66
67 /// `primary` gives buttons a green outline and text, ideal for affirmation or confirmation actions.
68 Primary,
69
70 /// `danger` gives buttons a red outline and text, and should be used when the action is destructive.
71 Danger,
72}
73
74impl ButtonStyle {
75 /// Returns true if the [`ButtonStyle`] is [`ButtonStyle::Default`].
76 pub fn is_default(&self) -> bool {
77 matches!(self, ButtonStyle::Default)
78 }
79}