Skip to main content

SectionBlockBuilder

Struct SectionBlockBuilder 

Source
pub struct SectionBlockBuilder { /* private fields */ }
Expand description

Consuming builder for SectionBlock. Validation runs in Self::build.

Implementations§

Source§

impl SectionBlockBuilder

Source

pub fn text(self, value: impl Into<TextInput>) -> Self

Sets the main text of the section.

Examples found in repository?
examples/api_checkpoint.rs (line 14)
10fn main() -> Result<(), Box<dyn std::error::Error>> {
11    let accordion = Accordion::new([AccordionSection::builder("Details")
12        .block(
13            SectionBlock::builder()
14                .text("Iterator composition")
15                .build()?,
16        )
17        .build()?])?;
18    let expanded = MessagePayload::builder("C0123456")
19        .blocks(accordion)
20        .build()?;
21    assert_eq!(expanded.blocks().map(<[Block]>::len), Some(1));
22    let option = SelectOption::builder()
23        .text("First option")
24        .value("first")
25        .build()?;
26    assert_eq!(option.value(), "first");
27    let section = SectionBlock::builder()
28        .text("Hello, Rust!")
29        .accessory(
30            ButtonElement::builder()
31                .text("Read more")
32                .action_id("read")
33                .build()?,
34        )
35        .block_id("hello")
36        .build()?;
37    let edited = section
38        .clone()
39        .into_builder()
40        .text(PlainText::new("Updated")?)
41        .build()?;
42    assert_ne!(edited, section);
43    let message = MessagePayload::builder("C0123456").block(section).build()?;
44    assert_eq!(message.blocks().map(<[Block]>::len), Some(1));
45    let encoded = serde_json::to_string(&message)?;
46    assert_eq!(serde_json::from_str::<MessagePayload>(&encoded)?, message);
47    let omitted = MessagePayload::try_from(json!({"channel":"C0123456"}))?;
48    assert_eq!(
49        serde_json::to_value(omitted.into_builder().build()?)?,
50        json!({"channel":"C0123456"})
51    );
52
53    let rich = RichTextBlock::builder()
54        .element(
55            RichTextList::builder()
56                .style(RichTextListStyle::Bullet)
57                .element(
58                    RichTextSection::builder()
59                        .element(
60                            RichTextText::new("Native ownership")?
61                                .into_builder()
62                                .style(RichTextStyle::new().bold(true))
63                                .build()?,
64                        )
65                        .build()?,
66                )
67                .build()?,
68        )
69        .build()?;
70    let table = TableBlock::builder()
71        .row([
72            TableCell::from(RawText::new("Heading")?),
73            rich.clone().into(),
74        ])
75        .row([TableCell::from(RawText::new("Ragged rows are valid")?)])
76        .build()?;
77    assert_eq!(table.rows()[1].len(), 1);
78    let pending = TaskCardBlock::builder()
79        .task_id("review")
80        .title("Review")
81        .status(TaskStatus::Pending)
82        .details(rich)
83        .build()?;
84    let error = MessagePayload::builder("C0123456")
85        .block(pending.clone())
86        .build()
87        .unwrap_err();
88    assert_eq!(error.category(), ErrorCategory::TypeMismatch);
89    assert_eq!(error.path(), "MessagePayload.blocks[0].status");
90    let plan = PlanBlock::builder()
91        .title("Release")
92        .task(pending)
93        .build()?;
94    let wire = serde_json::to_value(&plan)?;
95    assert!(wire["tasks"][0].get("type").is_none());
96    assert_eq!(PlanBlock::try_from(wire)?, plan);
97    let count = JsonNumber::from(9_007_199_254_740_993_u64);
98    assert_eq!(serde_json::to_string(&count)?, "9007199254740993");
99    let data = DataTableBlock::builder()
100        .caption("Exact counts")
101        .row([RawNumber::builder().value(0_i64).text("Count").build()?])
102        .row([RawNumber::builder()
103            .value(count)
104            .text("9007199254740993")
105            .build()?])
106        .build()?;
107    assert_eq!(data.rows().len(), 2);
108    assert!(PlainText::new("").is_err());
109    assert_eq!(
110        serde_json::to_value(MarkdownText::new("*native*")?)?["text"],
111        Value::String("*native*".into())
112    );
113    assert!(std::mem::size_of::<Block>() <= 32);
114    fn send_sync<T: Send + Sync>() {}
115    send_sync::<MessagePayload>();
116    println!("{encoded}");
117    Ok(())
118}
Source

pub fn clear_text(self) -> Self

Omits text, including any model default.

Source

pub fn fields<I, T>(self, values: I) -> Self
where I: IntoIterator<Item = T>, T: Into<TextInput>,

Adds section fields, shown in a two-column grid, in display order. Replaces the collection; order is preserved.

Source

pub fn field(self, value: impl Into<TextInput>) -> Self

Appends one item to the collection.

Source

pub fn clear_fields(self) -> Self

Omits fields, including any model default.

Source

pub fn accessory(self, value: impl Into<Element>) -> Self

Sets the element displayed beside the section text.

Examples found in repository?
examples/api_checkpoint.rs (lines 29-34)
10fn main() -> Result<(), Box<dyn std::error::Error>> {
11    let accordion = Accordion::new([AccordionSection::builder("Details")
12        .block(
13            SectionBlock::builder()
14                .text("Iterator composition")
15                .build()?,
16        )
17        .build()?])?;
18    let expanded = MessagePayload::builder("C0123456")
19        .blocks(accordion)
20        .build()?;
21    assert_eq!(expanded.blocks().map(<[Block]>::len), Some(1));
22    let option = SelectOption::builder()
23        .text("First option")
24        .value("first")
25        .build()?;
26    assert_eq!(option.value(), "first");
27    let section = SectionBlock::builder()
28        .text("Hello, Rust!")
29        .accessory(
30            ButtonElement::builder()
31                .text("Read more")
32                .action_id("read")
33                .build()?,
34        )
35        .block_id("hello")
36        .build()?;
37    let edited = section
38        .clone()
39        .into_builder()
40        .text(PlainText::new("Updated")?)
41        .build()?;
42    assert_ne!(edited, section);
43    let message = MessagePayload::builder("C0123456").block(section).build()?;
44    assert_eq!(message.blocks().map(<[Block]>::len), Some(1));
45    let encoded = serde_json::to_string(&message)?;
46    assert_eq!(serde_json::from_str::<MessagePayload>(&encoded)?, message);
47    let omitted = MessagePayload::try_from(json!({"channel":"C0123456"}))?;
48    assert_eq!(
49        serde_json::to_value(omitted.into_builder().build()?)?,
50        json!({"channel":"C0123456"})
51    );
52
53    let rich = RichTextBlock::builder()
54        .element(
55            RichTextList::builder()
56                .style(RichTextListStyle::Bullet)
57                .element(
58                    RichTextSection::builder()
59                        .element(
60                            RichTextText::new("Native ownership")?
61                                .into_builder()
62                                .style(RichTextStyle::new().bold(true))
63                                .build()?,
64                        )
65                        .build()?,
66                )
67                .build()?,
68        )
69        .build()?;
70    let table = TableBlock::builder()
71        .row([
72            TableCell::from(RawText::new("Heading")?),
73            rich.clone().into(),
74        ])
75        .row([TableCell::from(RawText::new("Ragged rows are valid")?)])
76        .build()?;
77    assert_eq!(table.rows()[1].len(), 1);
78    let pending = TaskCardBlock::builder()
79        .task_id("review")
80        .title("Review")
81        .status(TaskStatus::Pending)
82        .details(rich)
83        .build()?;
84    let error = MessagePayload::builder("C0123456")
85        .block(pending.clone())
86        .build()
87        .unwrap_err();
88    assert_eq!(error.category(), ErrorCategory::TypeMismatch);
89    assert_eq!(error.path(), "MessagePayload.blocks[0].status");
90    let plan = PlanBlock::builder()
91        .title("Release")
92        .task(pending)
93        .build()?;
94    let wire = serde_json::to_value(&plan)?;
95    assert!(wire["tasks"][0].get("type").is_none());
96    assert_eq!(PlanBlock::try_from(wire)?, plan);
97    let count = JsonNumber::from(9_007_199_254_740_993_u64);
98    assert_eq!(serde_json::to_string(&count)?, "9007199254740993");
99    let data = DataTableBlock::builder()
100        .caption("Exact counts")
101        .row([RawNumber::builder().value(0_i64).text("Count").build()?])
102        .row([RawNumber::builder()
103            .value(count)
104            .text("9007199254740993")
105            .build()?])
106        .build()?;
107    assert_eq!(data.rows().len(), 2);
108    assert!(PlainText::new("").is_err());
109    assert_eq!(
110        serde_json::to_value(MarkdownText::new("*native*")?)?["text"],
111        Value::String("*native*".into())
112    );
113    assert!(std::mem::size_of::<Block>() <= 32);
114    fn send_sync<T: Send + Sync>() {}
115    send_sync::<MessagePayload>();
116    println!("{encoded}");
117    Ok(())
118}
Source

pub fn clear_accessory(self) -> Self

Omits accessory, including any model default.

Source

pub fn block_id(self, value: impl Into<String>) -> Self

Sets a unique identifier for this block. Slack returns it in interaction payloads, so use a stable value when you need to find the block again.

Examples found in repository?
examples/api_checkpoint.rs (line 35)
10fn main() -> Result<(), Box<dyn std::error::Error>> {
11    let accordion = Accordion::new([AccordionSection::builder("Details")
12        .block(
13            SectionBlock::builder()
14                .text("Iterator composition")
15                .build()?,
16        )
17        .build()?])?;
18    let expanded = MessagePayload::builder("C0123456")
19        .blocks(accordion)
20        .build()?;
21    assert_eq!(expanded.blocks().map(<[Block]>::len), Some(1));
22    let option = SelectOption::builder()
23        .text("First option")
24        .value("first")
25        .build()?;
26    assert_eq!(option.value(), "first");
27    let section = SectionBlock::builder()
28        .text("Hello, Rust!")
29        .accessory(
30            ButtonElement::builder()
31                .text("Read more")
32                .action_id("read")
33                .build()?,
34        )
35        .block_id("hello")
36        .build()?;
37    let edited = section
38        .clone()
39        .into_builder()
40        .text(PlainText::new("Updated")?)
41        .build()?;
42    assert_ne!(edited, section);
43    let message = MessagePayload::builder("C0123456").block(section).build()?;
44    assert_eq!(message.blocks().map(<[Block]>::len), Some(1));
45    let encoded = serde_json::to_string(&message)?;
46    assert_eq!(serde_json::from_str::<MessagePayload>(&encoded)?, message);
47    let omitted = MessagePayload::try_from(json!({"channel":"C0123456"}))?;
48    assert_eq!(
49        serde_json::to_value(omitted.into_builder().build()?)?,
50        json!({"channel":"C0123456"})
51    );
52
53    let rich = RichTextBlock::builder()
54        .element(
55            RichTextList::builder()
56                .style(RichTextListStyle::Bullet)
57                .element(
58                    RichTextSection::builder()
59                        .element(
60                            RichTextText::new("Native ownership")?
61                                .into_builder()
62                                .style(RichTextStyle::new().bold(true))
63                                .build()?,
64                        )
65                        .build()?,
66                )
67                .build()?,
68        )
69        .build()?;
70    let table = TableBlock::builder()
71        .row([
72            TableCell::from(RawText::new("Heading")?),
73            rich.clone().into(),
74        ])
75        .row([TableCell::from(RawText::new("Ragged rows are valid")?)])
76        .build()?;
77    assert_eq!(table.rows()[1].len(), 1);
78    let pending = TaskCardBlock::builder()
79        .task_id("review")
80        .title("Review")
81        .status(TaskStatus::Pending)
82        .details(rich)
83        .build()?;
84    let error = MessagePayload::builder("C0123456")
85        .block(pending.clone())
86        .build()
87        .unwrap_err();
88    assert_eq!(error.category(), ErrorCategory::TypeMismatch);
89    assert_eq!(error.path(), "MessagePayload.blocks[0].status");
90    let plan = PlanBlock::builder()
91        .title("Release")
92        .task(pending)
93        .build()?;
94    let wire = serde_json::to_value(&plan)?;
95    assert!(wire["tasks"][0].get("type").is_none());
96    assert_eq!(PlanBlock::try_from(wire)?, plan);
97    let count = JsonNumber::from(9_007_199_254_740_993_u64);
98    assert_eq!(serde_json::to_string(&count)?, "9007199254740993");
99    let data = DataTableBlock::builder()
100        .caption("Exact counts")
101        .row([RawNumber::builder().value(0_i64).text("Count").build()?])
102        .row([RawNumber::builder()
103            .value(count)
104            .text("9007199254740993")
105            .build()?])
106        .build()?;
107    assert_eq!(data.rows().len(), 2);
108    assert!(PlainText::new("").is_err());
109    assert_eq!(
110        serde_json::to_value(MarkdownText::new("*native*")?)?["text"],
111        Value::String("*native*".into())
112    );
113    assert!(std::mem::size_of::<Block>() <= 32);
114    fn send_sync<T: Send + Sync>() {}
115    send_sync::<MessagePayload>();
116    println!("{encoded}");
117    Ok(())
118}
Source

pub fn clear_block_id(self) -> Self

Omits block_id, including any model default.

Source

pub fn extension(self, key: impl Into<String>, value: impl Into<Value>) -> Self

Adds or replaces an unmodeled extension; reserved names fail at build time.

Source

pub fn build(self) -> Result<SectionBlock, ValidationError>

Validates all fields and their receiving context.

§Errors

Returns a ValidationError for missing fields, invalid values, reserved extension names, or unsupported field combinations and child contexts.

Examples found in repository?
examples/api_checkpoint.rs (line 15)
10fn main() -> Result<(), Box<dyn std::error::Error>> {
11    let accordion = Accordion::new([AccordionSection::builder("Details")
12        .block(
13            SectionBlock::builder()
14                .text("Iterator composition")
15                .build()?,
16        )
17        .build()?])?;
18    let expanded = MessagePayload::builder("C0123456")
19        .blocks(accordion)
20        .build()?;
21    assert_eq!(expanded.blocks().map(<[Block]>::len), Some(1));
22    let option = SelectOption::builder()
23        .text("First option")
24        .value("first")
25        .build()?;
26    assert_eq!(option.value(), "first");
27    let section = SectionBlock::builder()
28        .text("Hello, Rust!")
29        .accessory(
30            ButtonElement::builder()
31                .text("Read more")
32                .action_id("read")
33                .build()?,
34        )
35        .block_id("hello")
36        .build()?;
37    let edited = section
38        .clone()
39        .into_builder()
40        .text(PlainText::new("Updated")?)
41        .build()?;
42    assert_ne!(edited, section);
43    let message = MessagePayload::builder("C0123456").block(section).build()?;
44    assert_eq!(message.blocks().map(<[Block]>::len), Some(1));
45    let encoded = serde_json::to_string(&message)?;
46    assert_eq!(serde_json::from_str::<MessagePayload>(&encoded)?, message);
47    let omitted = MessagePayload::try_from(json!({"channel":"C0123456"}))?;
48    assert_eq!(
49        serde_json::to_value(omitted.into_builder().build()?)?,
50        json!({"channel":"C0123456"})
51    );
52
53    let rich = RichTextBlock::builder()
54        .element(
55            RichTextList::builder()
56                .style(RichTextListStyle::Bullet)
57                .element(
58                    RichTextSection::builder()
59                        .element(
60                            RichTextText::new("Native ownership")?
61                                .into_builder()
62                                .style(RichTextStyle::new().bold(true))
63                                .build()?,
64                        )
65                        .build()?,
66                )
67                .build()?,
68        )
69        .build()?;
70    let table = TableBlock::builder()
71        .row([
72            TableCell::from(RawText::new("Heading")?),
73            rich.clone().into(),
74        ])
75        .row([TableCell::from(RawText::new("Ragged rows are valid")?)])
76        .build()?;
77    assert_eq!(table.rows()[1].len(), 1);
78    let pending = TaskCardBlock::builder()
79        .task_id("review")
80        .title("Review")
81        .status(TaskStatus::Pending)
82        .details(rich)
83        .build()?;
84    let error = MessagePayload::builder("C0123456")
85        .block(pending.clone())
86        .build()
87        .unwrap_err();
88    assert_eq!(error.category(), ErrorCategory::TypeMismatch);
89    assert_eq!(error.path(), "MessagePayload.blocks[0].status");
90    let plan = PlanBlock::builder()
91        .title("Release")
92        .task(pending)
93        .build()?;
94    let wire = serde_json::to_value(&plan)?;
95    assert!(wire["tasks"][0].get("type").is_none());
96    assert_eq!(PlanBlock::try_from(wire)?, plan);
97    let count = JsonNumber::from(9_007_199_254_740_993_u64);
98    assert_eq!(serde_json::to_string(&count)?, "9007199254740993");
99    let data = DataTableBlock::builder()
100        .caption("Exact counts")
101        .row([RawNumber::builder().value(0_i64).text("Count").build()?])
102        .row([RawNumber::builder()
103            .value(count)
104            .text("9007199254740993")
105            .build()?])
106        .build()?;
107    assert_eq!(data.rows().len(), 2);
108    assert!(PlainText::new("").is_err());
109    assert_eq!(
110        serde_json::to_value(MarkdownText::new("*native*")?)?["text"],
111        Value::String("*native*".into())
112    );
113    assert!(std::mem::size_of::<Block>() <= 32);
114    fn send_sync<T: Send + Sync>() {}
115    send_sync::<MessagePayload>();
116    println!("{encoded}");
117    Ok(())
118}

Trait Implementations§

Source§

impl Clone for SectionBlockBuilder

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for SectionBlockBuilder

Source§

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

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

impl Default for SectionBlockBuilder

Source§

fn default() -> Self

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

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

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 T
where T: Clone,

Source§

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 T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.