Skip to main content

slack_blocks

Macro slack_blocks 

Source
macro_rules! slack_blocks {
    (@acc $v:ident;) => { ... };
    (@acc $v:ident; some_into($item:expr) $(, $($rest:tt)*)?) => { ... };
    (@acc $v:ident; some($item:expr) $(, $($rest:tt)*)?) => { ... };
    (@acc $v:ident; optionally($pred:expr => $item:expr) $(, $($rest:tt)*)?) => { ... };
    (@acc $v:ident; optionally_into($pred:expr => $item:expr) $(, $($rest:tt)*)?) => { ... };
    (@acc $v:ident; ..$iter:expr $(, $($rest:tt)*)?) => { ... };
    (@acc $v:ident; $kw:ident($pred:expr => $item:expr) $(, $($rest:tt)*)?) => { ... };
    (@acc $v:ident; $item:expr $(, $($rest:tt)*)?) => { ... };
    (@acc $v:ident; $($t:tt)+) => { ... };
    () => { ... };
    ($($t:tt)*) => { ... };
}
Expand description

Builds a Vec<T> of block-kit items from a comma-separated list, in one of two syntaxes that may be freely mixed within a single list:

  • Bare form: any expression is pushed via .into(). ..iter splices every element of iter in, each also converted via .into(). optionally(pred => item) converts and pushes item the same way a bare item does, only when pred is true, evaluating neither pred nor item otherwise. Into::into is the identity conversion for an already-typed item, so item may be a block struct or an already-built target type; the one thing it cannot be is an expression that itself ends in an untyped .into(), such as md!(..)/pt!(..) — the same limit a bare item has (see md!).
  • Legacy keyword form: some(item), some_into(item), optionally(pred => item), optionally_into(pred => item), matching slack_block_item!.

slack_blocks![] and a trailing comma on the last item are both allowed.

See md! for a caveat on placing a bare md!(..)/pt!(..) item directly in a list.

use slack_morphism::prelude::*;

let show_divider = true;

let items: Vec<SlackBlock> = slack_blocks![
    SlackHeaderBlock::new(pt!("Title")),
    optionally(show_divider => SlackDividerBlock::new()),
    ..vec![SlackDividerBlock::new()],
];
assert_eq!(items.len(), 3);

// Legacy keyword form, still supported:
let legacy_items: Vec<SlackBlock> = slack_blocks![
    some_into(SlackHeaderBlock::new(pt!("Title"))),
    optionally_into(true => SlackDividerBlock::new()),
];
assert_eq!(legacy_items.len(), 2);