1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
//! # XML macro builder support pub use elems::{button::Style::{Danger as btn_danger, Primary as btn_primary}, select::build::{choose::{multi, single}, data_source::{conversations, external, public_channels, static_, users}}}; pub use mox::mox as blox; pub use text::build::kind::{mrkdwn, plain}; use crate::{blocks, compose, elems, text}; /// Identity trait to appease the mox macro pub trait IntoChild: Sized { /// Identity function fn into_child(self) -> Self { self } } impl<T> IntoChild for T {} pub use blox_blocks::*; pub use blox_compose::*; pub use blox_elems::*; mod blox_blocks { use super::*; /// # Build an actions block /// /// ## Children /// Accepts at least one, and up to 5 supported block elements as children. /// /// ## Example /// ``` /// use slack_blocks::{blocks::Actions, blox::*, elems::Button, text}; /// /// let xml = blox! { /// <actions_block> /// <button action_id="foo">"Foo"</button> /// <button action_id="bar">"Bar"</button> /// </actions_block> /// }; /// /// let equivalent = /// Actions::builder().element(Button::builder().action_id("foo") /// .text("Foo") /// .build()) /// .element(Button::builder().action_id("bar") /// .text("Bar") /// .build()) /// .build(); /// /// assert_eq!(xml, equivalent); /// ``` pub fn actions_block( ) -> blocks::actions::build::ActionsBuilderInit<'static> { blocks::Actions::builder() } /// # Build a section block /// /// ## Children /// Accepts at least one, and up to 10 text objects as children. /// /// ## Example /// ``` /// use slack_blocks::{blocks::Section, blox::*, text}; /// /// let xml = blox! { /// <section_block text=blox!{<text kind=plain>"Foo"</text>} /> /// }; /// /// let equivalent = Section::builder().text(text::Plain::from("Foo")).build(); /// /// assert_eq!(xml, equivalent); /// ``` pub fn section_block( ) -> blocks::section::build::SectionBuilderInit<'static> { blocks::Section::builder() } /// # Build an input block /// /// ## Children /// Input requires a single child of a supported block element. /// /// For the list of supported elements, see `slack_blocks::blocks::input::SupportedElement`. /// /// ## Example /// ``` /// use slack_blocks::{blocks::Input, blox::*, elems::TextInput, text}; /// /// let xml = blox! { /// <input_block label="foo"> /// <text_input action_id="input" /> /// </input_block> /// }; /// /// let equivalent = /// Input::builder().label("foo") /// .element(TextInput::builder().action_id("input").build()) /// .build(); /// /// assert_eq!(xml, equivalent); /// ``` pub fn input_block() -> blocks::input::build::InputBuilderInit<'static> { blocks::Input::builder() } /// # Build a context block /// /// ## Children /// Allows at least one, up to 10 text or img elements. /// /// ## Example /// ``` /// use slack_blocks::{blocks::Context, blox::*, elems::Image, text}; /// /// let xml = blox! { /// <context_block> /// <text kind=plain>"Enjoy this picture of bar"</text> /// <img src="https://foo.com/bar.png" alt="a pic of bar" /> /// </context_block> /// }; /// /// let equivalent = /// Context::builder().element(text::Plain::from("Enjoy this picture of bar")) /// .element(Image::builder().src("https://foo.com/bar.png") /// .alt("a pic of bar") /// .build()) /// .build(); /// /// assert_eq!(xml, equivalent); /// ``` pub fn context_block( ) -> blocks::context::build::ContextBuilderInit<'static> { blocks::Context::builder() } /// # Build a file block /// /// ## Children /// None /// /// ## Example /// ``` /// use slack_blocks::{blocks::File, blox::*}; /// /// let xml = blox! { /// <file_block external_id="foo" /> /// }; /// /// let equivalent = File::builder().external_id("foo").build(); /// /// assert_eq!(xml, equivalent); /// ``` pub fn file_block() -> blocks::file::build::FileBuilderInit<'static> { blocks::File::builder() } /// # Build an image block /// /// ## Children /// Allows at least one, up to 10 text or img elements. /// /// ## Example /// ``` /// use slack_blocks::{blocks::Image, blox::*}; /// /// let xml = blox! { /// <img_block src="https://foo.com/bar.png" alt="a pic of bar" /> /// }; /// /// let equivalent = Image::builder().src("https://foo.com/bar.png") /// .alt("a pic of bar") /// .build(); /// /// assert_eq!(xml, equivalent); /// ``` pub fn img_block() -> blocks::image::build::ImageBuilderInit<'static> { blocks::Image::builder() } } mod blox_elems { use super::*; /// # Build an text input element /// /// ## Children /// None. /// /// ## Example /// ``` /// use slack_blocks::{blox::*, elems::TextInput}; /// /// let xml: TextInput = blox! { /// <text_input action_id="name_input" /// multiline=true /// placeholder="Type your name" /// length={1..=1000} /// /> /// }; /// /// let equiv = TextInput::builder().action_id("name_input") /// .multiline(true) /// .placeholder("Type your name") /// .length(1..=1000) /// .build(); /// /// assert_eq!(xml, equiv) /// ``` pub fn text_input( ) -> elems::text_input::build::TextInputBuilderInit<'static> { elems::TextInput::builder() } /// # Build an image element /// /// ## Children /// None. /// /// ## Example /// ``` /// use slack_blocks::{blox::*, elems::Image}; /// /// let xml: Image = blox! { /// <img src="https://foo.com/bar.png" alt="a pic of bar" /> /// }; /// /// let equiv = Image::builder().src("https://foo.com/bar.png") /// .alt("a pic of bar") /// .build(); /// /// assert_eq!(xml, equiv) /// ``` pub fn img() -> elems::image::build::ImageBuilderInit<'static> { elems::Image::builder() } /// # Build a button /// /// ## Children /// The text to display in the button /// /// ## Example /// ``` /// use slack_blocks::{blox::*, elems::Button}; /// /// let xml: Button = blox! { /// <button action_id="click_me">"Click me!"</button> /// }; /// /// let equiv = Button::builder().action_id("click_me") /// .text("Click me!") /// .build(); /// /// assert_eq!(xml, equiv) /// ``` pub fn button() -> elems::button::build::ButtonBuilderInit<'static> { elems::Button::builder() } /// # Build a checkbox group /// /// ## Children /// Options to populate the checkbox group with. Min 1, max 10 /// /// ## Example /// ``` /// use slack_blocks::{blox::*, compose::Opt, elems::Checkboxes}; /// /// let xml: Checkboxes = blox! { /// <checkboxes action_id="chex"> /// <option value="check_1"> /// <text kind=plain>"Foo"</text> /// </option> /// <option value="check_2"> /// <text kind=mrkdwn>"Bar"</text> /// </option> /// <option value="check_3"> /// <text kind=plain>"Zoo"</text> /// </option> /// </checkboxes> /// }; /// /// let equiv = Checkboxes::builder().action_id("chex") /// .option(Opt::builder().value("check_1") /// .text_plain("Foo") /// .build()) /// .option(Opt::builder().value("check_2") /// .text_md("Bar") /// .build()) /// .option(Opt::builder().value("check_3") /// .text_plain("Zoo") /// .build()) /// .build(); /// /// assert_eq!(xml, equiv) /// ``` pub fn checkboxes( ) -> elems::checkboxes::build::CheckboxesBuilderInit<'static> { elems::Checkboxes::builder() } /// # Build a date picker /// /// ## Children /// None /// /// ## Example /// ``` /// use slack_blocks::{blox::*, elems::DatePicker}; /// /// let xml = blox! { /// <date_picker action_id="pick_birthday" placeholder="Pick your birthday!" /> /// }; /// /// let equiv = DatePicker::builder().action_id("pick_birthday") /// .placeholder("Pick your birthday!") /// .build(); /// /// assert_eq!(xml, equiv) /// ``` pub fn date_picker( ) -> elems::date_picker::build::DatePickerBuilderInit<'static> { elems::DatePicker::builder() } /// # Build an overflow menu /// /// ## Children /// Options contained in the overflow menu. Min 2, max 5. /// /// ## Example /// ``` /// use slack_blocks::{blox::*, compose::Opt, elems::Overflow}; /// /// let xml = blox! { /// <overflow action_id="menu"> /// <option value="foo"><text kind=plain>"Foo"</text></option> /// <option value="url" text_plain="Open link" url="foo.com" /> /// </overflow> /// }; /// /// let equiv = Overflow::builder().action_id("menu") /// .option(Opt::builder().value("foo") /// .text_plain("Foo") /// .build()) /// .option(Opt::builder().value("url") /// .text_plain("Open link") /// .url("foo.com") /// .build()) /// .build(); /// /// assert_eq!(xml, equiv) /// ``` pub fn overflow() -> elems::overflow::build::OverflowBuilderInit<'static> { elems::Overflow::builder() } /// # Build a radio button group /// /// ## Children /// Options contained in the radio button group /// /// ## Example /// ``` /// use slack_blocks::{blocks::Input, blox::*, compose::Opt, elems::Radio}; /// /// let xml = blox! { /// <input_block label="Pick your favorite cheese!"> /// <radio_buttons action_id="cheese_picker"> /// <option value="feta"><text kind=plain>"Feta"</text></option> /// <option value="gouda"><text kind=plain>"Gouda"</text></option> /// <option value="cheddar"><text kind=plain>"Cheddar"</text></option> /// </radio_buttons> /// </input_block> /// }; /// /// let radio = Radio::builder().action_id("cheese_picker") /// .option(Opt::builder().value("feta") /// .text_plain("Feta") /// .build()) /// .option(Opt::builder().value("gouda") /// .text_plain("Gouda") /// .build()) /// .option(Opt::builder().value("cheddar") /// .text_plain("Cheddar") /// .build()) /// .build(); /// /// let equiv = Input::builder().label("Pick your favorite cheese!") /// .element(radio) /// .build(); /// /// assert_eq!(xml, equiv) /// ``` pub fn radio_buttons() -> elems::radio::build::RadioBuilderInit<'static> { elems::Radio::builder() } /// # Build a select menu /// /// # Attributes /// - `kind` (Optional): `single` or `multi` from `slack_blocks::blox`. Default is `single`. /// - `choose_from` (Required): `users`, `public_channels`, `static_`, `external`, `conversations` from `slack_blocks::blox` /// /// # Children /// For `static_`, 1-100 `<option>` children are allowed. /// /// For all others, none are allowed. /// /// # Errors you may encounter /// - `Into<MyOpt> is not implemented for ...`: Make sure the `option` or `option_group` passed has the right `kind` of text for the select you're using. /// - `AppendOptOrOptGroup<MyOpt> is not implemented for ...`: Make sure the `option` or `option_group` has no `url` and contains **plain** text. /// - `Method `build` not found for...`: Make sure you've called all required methods. If you dig into the error message, you'll see `RequiredMethodNotCalled<method::foo>`, meaning you need to call `foo`. /// /// # Example - Select many Users /// ``` /// use slack_blocks::elems::select; /// use slack_blocks::blox::*; /// /// let xml = blox! { /// <select kind=multi choose_from=users placeholder="Pick some users!" action_id="foo" /> /// }; /// /// let equiv = select::multi::User::builder().placeholder("Pick some users!").action_id("foo").build(); /// /// assert_eq!(xml, equiv) /// ``` /// /// # Example - Select an option from a list defined by your app /// ``` /// use slack_blocks::{elems::select, compose::Opt, blox::*}; /// /// let xml = blox! { /// <select choose_from=static_ placeholder="Pick your favorite cheese!" action_id="foo"> /// <option value="gouda"><text kind=plain>"Gouda"</text></option> /// <option value="feta"><text kind=plain>"Feta"</text></option> /// <option value="cheddar"><text kind=plain>"Cheddar"</text></option> /// <option value="fontina"><text kind=plain>"Fontina"</text></option> /// </select> /// }; /// /// let equiv = select::Static::builder().placeholder("Pick your favorite cheese!") /// .action_id("foo") /// .option(Opt::builder().value("gouda").text_plain("Gouda").build()) /// .option(Opt::builder().value("feta").text_plain("Feta").build()) /// .option(Opt::builder().value("cheddar").text_plain("Cheddar").build()) /// .option(Opt::builder().value("fontina").text_plain("Fontina").build()) /// .build(); /// /// assert_eq!(xml, equiv) /// ``` pub fn select() -> elems::select::build::SelectBuilderInit { elems::select::build::SelectBuilderInit::new() } } mod blox_compose { use super::*; /// # Text /// /// ## Children /// Accepts a `String`, `&str` or anything else that implements /// `AsRef<str>` as a child, representing the text that will be /// rendered in Slack. /// /// ## Example /// ``` /// use slack_blocks::{blocks::Section, blox::*, text}; /// /// let xml = blox! { /// <text kind=plain>"Foo"</text> /// }; /// /// let equivalent = text::Plain::from("Foo"); /// /// assert_eq!(xml, equivalent); /// ``` pub fn text() -> text::build::TextBuilderInit { text::Text::builder() } /// # Option /// /// ## Children /// The text to be displayed to the user. /// /// Note that text can't be provided as a child if you're setting `url` /// within an overflow menu - you need to set the text via a `text_plain` attribute. /// /// ## Example /// ``` /// use slack_blocks::{blox::*, compose::Opt}; /// /// let xml = blox! { /// <option value="foo"> /// <text kind=plain>"Foo"</text> /// </option> /// }; /// /// let equivalent = Opt::builder().text_plain("Foo").value("foo").build(); /// /// assert_eq!(xml, equivalent); /// ``` pub fn option() -> compose::opt::build::OptBuilderInit<'static> { compose::Opt::builder() } /// # Option Group /// /// ## Children /// Up to 100 option objects of the same type. Note that `syn-rsx` (and `mox` by extension) /// do not support using iterables as children. /// /// This means that if you have a `Vec<Opt>`, /// you'll need to pass it to an `options` attribute instead. /// /// See Examples for an example of compile-time child opts, and runtime (Vec) opts. /// /// ## Example - Options known at compile-time /// ``` /// use slack_blocks::{blox::*, /// compose::{Opt, OptGroup}}; /// /// let xml = blox! { /// <option_group label="foos_and_bars"> /// <option value="foo"><text kind=plain>"Foo"</text></option> /// <option value="bar"><text kind=plain>"Bar"</text></option> /// </option_group> /// }; /// /// let equivalent = OptGroup::builder().label("foos_and_bars") /// .option(Opt::builder().value("foo") /// .text_plain("Foo") /// .build()) /// .option(Opt::builder().value("bar") /// .text_plain("Bar") /// .build()) /// .build(); /// /// assert_eq!(xml, equivalent); /// ``` /// /// ## Example - Dynamic vec of options /// ``` /// use slack_blocks::{blox::*, /// compose::{Opt, OptGroup}}; /// /// # fn uuid() -> String {"foo".to_string()} /// # fn random_word() -> String {"foo".to_string()} /// let generate_option = || { /// blox! { /// <option value={uuid()}> /// <text kind=plain>{random_word()}</text> /// </option> /// } /// }; /// /// // Generate 80 random options /// let options = std::iter::repeat(()).take(80) /// .map(|_| generate_option()) /// .collect::<Vec<_>>(); /// /// let xml = blox! { /// <option_group label="foos_and_bars" options={options.clone()} /> /// }; /// /// let equivalent = OptGroup::builder().label("foos_and_bars") /// .options(options.clone()) /// .build(); /// /// assert_eq!(xml, equivalent); /// ``` pub fn option_group( ) -> compose::opt_group::build::OptGroupBuilderInit<'static> { compose::OptGroup::builder() } /// # Confirm /// /// ## Children /// No children. /// /// ## Example /// ``` /// use slack_blocks::{blox::*, compose::Confirm, text::ToSlackPlaintext}; /// /// let xml = blox! { /// <confirm title="Title" /// text=blox!{<text kind=plain>"Body"</text>} /// confirm="Yes" /// deny="No" /// /> /// }; /// /// let equivalent = Confirm::builder().title("Title") /// .text("Body".plaintext()) /// .confirm("Yes") /// .deny("No") /// .build(); /// /// assert_eq!(xml, equivalent); /// ``` pub fn confirm() -> compose::confirm::build::ConfirmBuilderInit { compose::Confirm::builder() } }