pub struct Actions { /* private fields */ }
Expand description

Actions block representation.

Example

The following is reproduction of the 1st sample actions.

use slack_messaging::blocks::Actions;
use slack_messaging::blocks::elements::{Button, SelectStaticOptions, Opt};
use serde_json::json;

let actions = Actions::new()
    .set_block_id("actions1")
    .push_element(
        SelectStaticOptions::new()
            .set_action_id("select_2")
            .placeholder("Which witch is the witchiest witch?")
            .push_option(Opt::plain("Matilda").set_value("matilda"))
            .push_option(Opt::plain("Glinda").set_value("glinda"))
            .push_option(Opt::plain("Granny Weatherwax").set_value("grannyWeatherwax"))
            .push_option(Opt::plain("Hermione").set_value("hermione"))
    )
    .push_element(
        Button::new()
            .set_action_id("button_1")
            .set_value("cancel")
            .text("Cancel")
    );

let expected = json!({
    "type": "actions",
    "block_id": "actions1",
    "elements": [
        {
            "type": "static_select",
            "action_id": "select_2",
            "placeholder": {
                "type": "plain_text",
                "text": "Which witch is the witchiest witch?",
                "emoji": true
            },
            "options": [
                {
                    "text": {
                        "type": "plain_text",
                        "text": "Matilda",
                        "emoji": true
                    },
                    "value": "matilda"
                },
                {
                    "text": {
                        "type": "plain_text",
                        "text": "Glinda",
                        "emoji": true
                    },
                    "value": "glinda"
                },
                {
                    "text": {
                        "type": "plain_text",
                        "text": "Granny Weatherwax",
                        "emoji": true
                    },
                    "value": "grannyWeatherwax"
                },
                {
                    "text": {
                        "type": "plain_text",
                        "text": "Hermione",
                        "emoji": true
                    },
                    "value": "hermione"
                }
            ]
        },
        {
            "type": "button",
            "text": {
                "type": "plain_text",
                "text": "Cancel",
                "emoji": true
            },
            "value": "cancel",
            "action_id": "button_1"
        }
    ]
});

let actions_json = serde_json::to_value(actions).unwrap();

assert_eq!(actions_json, expected);

And the following is the 2nd sample actions.

use slack_messaging::blocks::Actions;
use slack_messaging::blocks::elements::{Button, DatePicker, Opt, OverflowMenu};
use serde_json::json;

let actions = Actions::new()
    .set_block_id("actionblock789")
    .push_element(
        DatePicker::new()
            .set_action_id("datepicker123")
            .set_initial_date("1990-04-28")
            .placeholder("Select a date")
    )
    .push_element(
        OverflowMenu::new()
            .set_action_id("overflow")
            .push_option(Opt::plain("*this is plain_text text*").set_value("value-0"))
            .push_option(Opt::plain("*this is plain_text text*").set_value("value-1"))
            .push_option(Opt::plain("*this is plain_text text*").set_value("value-2"))
            .push_option(Opt::plain("*this is plain_text text*").set_value("value-3"))
            .push_option(Opt::plain("*this is plain_text text*").set_value("value-4"))
    )
    .push_element(
        Button::new()
            .set_action_id("button")
            .set_value("click_me_123")
            .text("Click Me")
    );

let expected = json!({
    "type": "actions",
    "block_id": "actionblock789",
    "elements": [
        {
            "type": "datepicker",
            "action_id": "datepicker123",
            "initial_date": "1990-04-28",
            "placeholder": {
                "type": "plain_text",
                "text": "Select a date",
                "emoji": true
            }
        },
        {
            "type": "overflow",
            "action_id": "overflow",
            "options": [
                {
                    "text": {
                        "type": "plain_text",
                        "text": "*this is plain_text text*",
                        "emoji": true
                    },
                    "value": "value-0"
                },
                {
                    "text": {
                        "type": "plain_text",
                        "text": "*this is plain_text text*",
                        "emoji": true
                    },
                    "value": "value-1"
                },
                {
                    "text": {
                        "type": "plain_text",
                        "text": "*this is plain_text text*",
                        "emoji": true
                    },
                    "value": "value-2"
                },
                {
                    "text": {
                        "type": "plain_text",
                        "text": "*this is plain_text text*",
                        "emoji": true
                    },
                    "value": "value-3"
                },
                {
                    "text": {
                        "type": "plain_text",
                        "text": "*this is plain_text text*",
                        "emoji": true
                    },
                    "value": "value-4"
                }
            ]
        },
        {
            "type": "button",
            "text": {
                "type": "plain_text",
                "text": "Click Me",
                "emoji": true
            },
            "value": "click_me_123",
            "action_id": "button"
        }
    ]
});

let actions_json = serde_json::to_value(actions).unwrap();

assert_eq!(actions_json, expected);

Implementations§

source§

impl Actions

source

pub fn new() -> Self

Constructs an Actions block.

use slack_messaging::blocks::Actions;
use serde_json::json;

let actions = Actions::new();

let expected = json!({
    "type": "actions",
    "elements": []
});

let actions_json = serde_json::to_value(actions).unwrap();

assert_eq!(actions_json, expected);
source

pub fn set_elements(self, elements: Vec<ActionsElement>) -> Self

Sets elements field directly. The argument is a vector composed from any objects that can transform into the enum ActionsElement.

use slack_messaging::blocks::Actions;
use slack_messaging::blocks::elements::{Button, SelectStaticOptions, Opt};
use serde_json::json;

let actions = Actions::new()
    .set_elements(
        vec![
            SelectStaticOptions::new()
                .set_action_id("select_2")
                .placeholder("Which witch is the witchiest witch?")
                .push_option(Opt::plain("Matilda").set_value("matilda"))
                .push_option(Opt::plain("Glinda").set_value("glinda"))
                .into(),
            Button::new()
                .set_action_id("button_1")
                .set_value("cancel")
                .text("Cancel")
                .into(),
        ]
    );

let expected = json!({
    "type": "actions",
    "elements": [
        {
            "type": "static_select",
            "action_id": "select_2",
            "placeholder": {
                "type": "plain_text",
                "text": "Which witch is the witchiest witch?",
                "emoji": true
            },
            "options": [
                {
                    "text": {
                        "type": "plain_text",
                        "text": "Matilda",
                        "emoji": true
                    },
                    "value": "matilda"
                },
                {
                    "text": {
                        "type": "plain_text",
                        "text": "Glinda",
                        "emoji": true
                    },
                    "value": "glinda"
                }
            ]
        },
        {
            "type": "button",
            "text": {
                "type": "plain_text",
                "text": "Cancel",
                "emoji": true
            },
            "value": "cancel",
            "action_id": "button_1"
        }
    ]
});

let actions_json = serde_json::to_value(actions).unwrap();

assert_eq!(actions_json, expected);
source

pub fn push_element<T: Into<ActionsElement>>(self, element: T) -> Self

Adds an object to elements field. The argument is an any object that can transform into the enum ActionsElement.

use slack_messaging::blocks::Actions;
use slack_messaging::blocks::elements::{Button, DatePicker};
use serde_json::json;

let actions = Actions::new()
    .push_element(
        DatePicker::new()
            .set_action_id("datepicker123")
            .set_initial_date("1990-04-28")
            .placeholder("Select a date")
    )
    .push_element(
        Button::new()
            .set_action_id("button")
            .set_value("click_me_123")
            .text("Click Me")
    );

let expected = json!({
    "type": "actions",
    "elements": [
        {
            "type": "datepicker",
            "action_id": "datepicker123",
            "initial_date": "1990-04-28",
            "placeholder": {
                "type": "plain_text",
                "text": "Select a date",
                "emoji": true
            }
        },
        {
            "type": "button",
            "text": {
                "type": "plain_text",
                "text": "Click Me",
                "emoji": true
            },
            "value": "click_me_123",
            "action_id": "button"
        }
    ]
});

let actions_json = serde_json::to_value(actions).unwrap();

assert_eq!(actions_json, expected);
source

pub fn set_block_id<T: Into<String>>(self, block_id: T) -> Self

Sets block_id field.

use slack_messaging::blocks::Actions;
use serde_json::json;

let actions = Actions::new().set_block_id("actions_block_1");

let expected = json!({
    "type": "actions",
    "elements": [],
    "block_id": "actions_block_1"
});

let actions_json = serde_json::to_value(actions).unwrap();

assert_eq!(actions_json, expected);

Trait Implementations§

source§

impl Clone for Actions

source§

fn clone(&self) -> Actions

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Actions

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Actions

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl From<Actions> for Block

source§

fn from(value: Actions) -> Self

Converts to this type from the input type.
source§

impl Serialize for Actions

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.