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

Section block representation.

Example

use slack_messaging::blocks::Section;
use slack_messaging::blocks::elements::Image;
use serde_json::json;

let section = Section::new()
    .set_block_id("section_1")
    .set_text_mrkdwn("A message *with some bold text* and _some italicized text_.")
    .push_field_mrkdwn("High")
    .push_field_plain("String")
    .set_accessory(
        Image::new()
            .set_image_url("http://placekitten.com/700/500")
            .set_alt_text("Multiple cute kittens")
    );

let expected = json!({
    "type": "section",
    "block_id": "section_1",
    "text": {
        "type": "mrkdwn",
        "text": "A message *with some bold text* and _some italicized text_."
    },
    "fields": [
        {
            "type": "mrkdwn",
            "text": "High"
        },
        {
            "type": "plain_text",
            "text": "String",
            "emoji": true
        }
    ],
    "accessory": {
        "type": "image",
        "image_url": "http://placekitten.com/700/500",
        "alt_text": "Multiple cute kittens"
    }
});

let section_json = serde_json::to_value(section).unwrap();

assert_eq!(section_json, expected);

Implementations§

source§

impl Section

source

pub fn new() -> Self

Constructs a Section block.

use slack_messaging::blocks::Section;
use serde_json::json;

let section = Section::new();

let expected = json!({
    "type": "section",
});

let section_json = serde_json::to_value(section).unwrap();

assert_eq!(section_json, expected);
source

pub fn set_text(self, text: Text) -> Self

Sets text field.

use slack_messaging::blocks::Section;
use slack_messaging::blocks::elements::Text;
use serde_json::json;

let section = Section::new()
    .set_text(Text::mrkdwn("A message *with some bold text* and _some italicized text_."));

let expected = json!({
    "type": "section",
    "text": {
        "type": "mrkdwn",
        "text": "A message *with some bold text* and _some italicized text_."
    },
});

let section_json = serde_json::to_value(section).unwrap();

assert_eq!(section_json, expected);
source

pub fn set_text_plain<T: Into<String>>(self, text: T) -> Self

Sets text field as plain text object.

use slack_messaging::blocks::Section;
use serde_json::json;

let section = Section::new()
    .set_text_plain("A message *with some bold text* and _some italicized text_.");

let expected = json!({
    "type": "section",
    "text": {
        "type": "plain_text",
        "text": "A message *with some bold text* and _some italicized text_.",
        "emoji": true
    },
});

let section_json = serde_json::to_value(section).unwrap();

assert_eq!(section_json, expected);
source

pub fn set_text_mrkdwn<T: Into<String>>(self, text: T) -> Self

Sets text field as mrkdwn text object.

use slack_messaging::blocks::Section;
use serde_json::json;

let section = Section::new()
    .set_text_mrkdwn("A message *with some bold text* and _some italicized text_.");

let expected = json!({
    "type": "section",
    "text": {
        "type": "mrkdwn",
        "text": "A message *with some bold text* and _some italicized text_."
    },
});

let section_json = serde_json::to_value(section).unwrap();

assert_eq!(section_json, expected);
source

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

Sets block_id field.

use slack_messaging::blocks::Section;
use serde_json::json;

let section = Section::new()
    .set_block_id("section_1");

let expected = json!({
    "type": "section",
    "block_id": "section_1"
});

let section_json = serde_json::to_value(section).unwrap();

assert_eq!(section_json, expected);
source

pub fn set_fields(self, fields: Vec<Text>) -> Self

Sets fields field directly.

use slack_messaging::blocks::Section;
use slack_messaging::blocks::elements::Text;
use serde_json::json;

let section = Section::new()
    .set_fields(
        vec![
            Text::plain("hello"),
            Text::mrkdwn("*world*")
        ]
    );

let expected = json!({
    "type": "section",
    "fields": [
        {
            "type": "plain_text",
            "text": "hello",
            "emoji": true
        },
        {
            "type": "mrkdwn",
            "text": "*world*"
        },
    ]
});

let section_json = serde_json::to_value(section).unwrap();

assert_eq!(section_json, expected);
source

pub fn push_field(self, field: Text) -> Self

Adds Text object to fields field.

use slack_messaging::blocks::Section;
use slack_messaging::blocks::elements::Text;
use serde_json::json;

let section = Section::new()
    .push_field(
        Text::plain("hello world")
    );

let expected = json!({
    "type": "section",
    "fields": [
        {
            "type": "plain_text",
            "text": "hello world",
            "emoji": true
        }
    ]
});

let section_json = serde_json::to_value(section).unwrap();

assert_eq!(section_json, expected);
source

pub fn push_field_plain<T: Into<String>>(self, field: T) -> Self

Adds plain_text Text object to fields field.

use slack_messaging::blocks::Section;
use slack_messaging::blocks::elements::Text;
use serde_json::json;

let section = Section::new()
    .push_field_plain("hello world");

let expected = json!({
    "type": "section",
    "fields": [
        {
            "type": "plain_text",
            "text": "hello world",
            "emoji": true
        }
    ]
});

let section_json = serde_json::to_value(section).unwrap();

assert_eq!(section_json, expected);
source

pub fn push_field_mrkdwn<T: Into<String>>(self, field: T) -> Self

Adds mrkdwn Text object to fields field.

use slack_messaging::blocks::Section;
use slack_messaging::blocks::elements::Text;
use serde_json::json;

let section = Section::new()
    .push_field_mrkdwn("hello world");

let expected = json!({
    "type": "section",
    "fields": [
        {
            "type": "mrkdwn",
            "text": "hello world"
        }
    ]
});

let section_json = serde_json::to_value(section).unwrap();

assert_eq!(section_json, expected);
source

pub fn set_accessory<T: Into<Accessory>>(self, accessory: T) -> Self

Sets object to accessory field. The argument is an any object that can transform into the enum Accessory.

use slack_messaging::blocks::Section;
use slack_messaging::blocks::elements::Button;
use serde_json::json;

let section = Section::new()
    .set_accessory(
        Button::new()
            .text("Click Me")
            .set_action_id("button-0")
            .set_value("click_me_123")
            .set_primary()
    );

let expected = json!({
    "type": "section",
    "accessory": {
        "type": "button",
        "text": {
            "type": "plain_text",
            "text": "Click Me",
            "emoji": true
        },
        "value": "click_me_123",
        "action_id": "button-0",
        "style": "primary"
    }
});

let section_json = serde_json::to_value(section).unwrap();

assert_eq!(section_json, expected);

Trait Implementations§

source§

impl Clone for Section

source§

fn clone(&self) -> Section

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 Section

source§

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

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

impl Default for Section

source§

fn default() -> Self

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

impl From<Section> for Block

source§

fn from(value: Section) -> Self

Converts to this type from the input type.
source§

impl Serialize for Section

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.