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
//! # Radio Buttons //! //! A radio button group that allows a user to choose one item from a list of possible options. //! //! [slack api docs 🔗] //! //! Works in [blocks 🔗]: Section, Actions, Input //! Works in [app surfaces 🔗]: Home tabs, Modals, Messages //! //! [slack api docs 🔗]: https://api.slack.com/reference/block-kit/block-elements#radio //! [blocks 🔗]: https://api.slack.com/reference/block-kit/blocks //! [app surfaces 🔗]: https://api.slack.com/surfaces use std::borrow::Cow; use serde::{Deserialize as De, Serialize as Ser}; use validator::Validate; use crate::{compose::{opt::{AnyText, NoUrl}, Confirm, Opt}, text, val_helpr::ValidationResult}; /// Opt state supported by radio buttons pub type RadioButtonOpt<'a> = Opt<'a, AnyText, NoUrl>; /// # Radio Buttons /// /// A radio button group that allows a user to choose one item from a list of possible options. /// /// [slack api docs 🔗] /// /// Works in [blocks 🔗]: Section, Actions, Input /// Works in [app surfaces 🔗]: Home tabs, Modals, Messages /// /// [slack api docs 🔗]: https://api.slack.com/reference/block-kit/block-elements#radio /// [blocks 🔗]: https://api.slack.com/reference/block-kit/blocks /// [app surfaces 🔗]: https://api.slack.com/surfaces #[derive(Clone, Debug, Hash, PartialEq, Ser, De, Validate)] pub struct Radio<'a> { #[validate(length(max = 255))] action_id: Cow<'a, str>, // max 255 #[validate(length(max = 10))] #[validate] options: Vec<RadioButtonOpt<'a>>, // max 10, plain or md #[serde(skip_serializing_if = "Option::is_none")] #[validate] initial_option: Option<RadioButtonOpt<'a>>, #[serde(skip_serializing_if = "Option::is_none")] #[validate] confirm: Option<Confirm>, } impl<'a> Radio<'a> { /// Build a new Radio Button Group /// /// # Example /// See docs for `RadioBuilder`. pub fn builder() -> build::RadioBuilderInit<'a> { build::RadioBuilderInit::new() } /// Validate that this select element agrees with Slack's model requirements /// /// # Errors /// - length of `action_id` greater than 255 /// - length of `options` greater than 10 /// - one or more of `options` is invalid /// - `initial_option` is set and an invalid `Opt` /// - `confirm` is set and an invalid `Confirm` /// /// # Example /// ``` /// use slack_blocks::{compose::Opt, elems::Radio}; /// /// fn repeat<T: Copy>(el: T, n: usize) -> impl Iterator<Item = T> { /// std::iter::repeat(el).take(n) /// } /// /// let long_string: String = repeat('a', 256).collect(); /// let opt = Opt::builder().text_md("foo").value("bar").build(); /// /// let opts = repeat(&opt, 11).map(|o| o.clone()).collect::<Vec<_>>(); /// /// let input = Radio::builder().action_id(long_string) /// .options(opts) /// .build(); /// /// assert!(matches!(input.validate(), Err(_))) /// ``` pub fn validate(&self) -> ValidationResult { Validate::validate(self) } } /// Radio button group builder pub mod build { use std::marker::PhantomData; use super::*; use crate::build::*; /// Required builder methods #[allow(non_camel_case_types)] mod method { /// RadioBuilder.action_id #[derive(Copy, Clone, Debug)] pub struct action_id; /// RadioBuilder.options #[derive(Copy, Clone, Debug)] pub struct options; } /// Initial state for radio button builder pub type RadioBuilderInit<'a> = RadioBuilder<'a, AnyText, RequiredMethodNotCalled<method::action_id>, RequiredMethodNotCalled<method::options>>; /// Radio Button builder /// /// Allows you to construct a radio button safely, with compile-time checks /// on required setter methods. /// /// # Required Methods /// `RadioButton::build()` is only available if these methods have been called: /// - `options` /// - `action_id` /// /// # Example /// ``` /// use slack_blocks::{blocks::{Actions, Block}, /// compose::Opt, /// elems::Radio}; /// /// let options = vec![Opt::builder().text_md(":joy:").value("joy").build(), /// Opt::builder().text_md(":smirk:").value("smirk").build(),]; /// /// let radio = Radio::builder().options(options) /// .action_id("emoji_picker") /// .build(); /// /// let block: Block = /// Actions::from_action_elements(std::iter::once(radio.into())).into(); /// /// // <send block to slack API> /// ``` #[derive(Debug)] pub struct RadioBuilder<'a, T, A, O> { action_id: Option<Cow<'a, str>>, options: Option<Vec<RadioButtonOpt<'a>>>, initial_option: Option<RadioButtonOpt<'a>>, confirm: Option<Confirm>, state: PhantomData<(T, A, O)>, } impl<'a, T, A, O> RadioBuilder<'a, T, A, O> { /// Construct a new RadioBuilder pub fn new() -> Self { Self { action_id: None, options: None, initial_option: None, confirm: None, state: PhantomData::<_> } } fn cast_state<A2, O2>(self) -> RadioBuilder<'a, T, A2, O2> { RadioBuilder { action_id: self.action_id, options: self.options, initial_option: self.initial_option, confirm: self.confirm, state: PhantomData::<_> } } /// Sets `action_id` (**Required**) /// /// An identifier for the action triggered when the radio button group is changed. /// /// You can use this when you receive an interaction payload to [identify the source of the action 🔗]. /// /// Should be unique among all other `action_id`s in the containing block. /// Maximum length for this field is 255 characters. /// [identify the source of the action 🔗]: https://api.slack.com/interactivity/handling#payloads pub fn action_id<S>(mut self, action_id: S) -> RadioBuilder<'a, T, Set<method::action_id>, O> where S: Into<Cow<'a, str>> { self.action_id = Some(action_id.into()); self.cast_state() } /// Sets `options` (**Required**) /// /// An array of [option objects 🔗]. /// /// A maximum of 10 options are allowed. /// [option objects 🔗]: https://api.slack.com/reference/block-kit/composition-objects#option pub fn options<I, T2: Into<text::Text>>( self, options: I) -> RadioBuilder<'a, T2, A, Set<method::options>> where I: IntoIterator<Item = Opt<'a, T2, NoUrl>> { let options = options.into_iter().map(|o| o.into()).collect(); RadioBuilder { action_id: self.action_id, options: Some(options), initial_option: self.initial_option, confirm: self.confirm, state: PhantomData::<_> } } /// Sets `confirm` (Optional) /// /// A [confirm object 🔗] that defines an optional confirmation dialog that appears /// after clicking one of the radio buttons in this element. /// /// [confirm object 🔗]: https://api.slack.com/reference/block-kit/composition-objects#confirm pub fn confirm(mut self, confirm: Confirm) -> Self { self.confirm = Some(confirm); self } } impl<'a, A> RadioBuilder<'a, text::Plain, A, Set<method::options>> { /// Sets `initial_option` (Optional) /// /// An [option object 🔗] that exactly matches one of the options within `options`. /// /// This option will be selected when the radio button group initially loads. /// /// [option object 🔗]: https://api.slack.com/reference/messaging/composition-objects#option pub fn initial_option(mut self, option: Opt<'a, text::Plain, NoUrl>) -> Self { self.initial_option = Some(option.into()); self } } impl<'a, A> RadioBuilder<'a, text::Mrkdwn, A, Set<method::options>> { /// Sets `initial_option` (Optional) /// /// An [option object 🔗] that exactly matches one of the options within `options`. /// /// This option will be selected when the radio button group initially loads. /// /// [option object 🔗]: https://api.slack.com/reference/messaging/composition-objects#option pub fn initial_option(mut self, option: Opt<'a, text::Mrkdwn, NoUrl>) -> Self { self.initial_option = Some(option.into()); self } } impl<'a, T> RadioBuilder<'a, T, Set<method::action_id>, Set<method::options>> { /// All done building, now give me a darn radio button group! /// /// > `no method name 'build' found for struct 'RadioBuilder<...>'`? /// Make sure all required setter methods have been called. See docs for `RadioBuilder`. /// /// ```compile_fail /// use slack_blocks::elems::Radio; /// /// let foo = Radio::builder().build(); // Won't compile! /// ``` /// /// ``` /// use slack_blocks::{compose::Opt, elems::Radio}; /// /// let foo = Radio::builder().action_id("bar") /// .options(vec![Opt::builder().text_md("foo") /// .value("bar") /// .build()]) /// .build(); /// ``` pub fn build(self) -> Radio<'a> { Radio { action_id: self.action_id.unwrap(), options: self.options.unwrap(), initial_option: self.initial_option, confirm: self.confirm } } } }