Mercurial > crates > nonstick
comparison src/conv.rs @ 76:e58d24849e82
Add Message::set_error to quickly answer a question with an error.
| author | Paul Fisher <paul@pfish.zone> |
|---|---|
| date | Sat, 07 Jun 2025 18:55:27 -0400 |
| parents | c7c596e6388f |
| children | 351bdc13005e |
comparison
equal
deleted
inserted
replaced
| 75:c30811b4afae | 76:e58d24849e82 |
|---|---|
| 10 | 10 |
| 11 /// The types of message and request that can be sent to a user. | 11 /// The types of message and request that can be sent to a user. |
| 12 /// | 12 /// |
| 13 /// The data within each enum value is the prompt (or other information) | 13 /// The data within each enum value is the prompt (or other information) |
| 14 /// that will be presented to the user. | 14 /// that will be presented to the user. |
| 15 #[non_exhaustive] | |
| 15 pub enum Message<'a> { | 16 pub enum Message<'a> { |
| 16 MaskedPrompt(&'a MaskedPrompt<'a>), | 17 MaskedPrompt(&'a MaskedPrompt<'a>), |
| 17 Prompt(&'a Prompt<'a>), | 18 Prompt(&'a Prompt<'a>), |
| 18 RadioPrompt(&'a RadioPrompt<'a>), | 19 RadioPrompt(&'a RadioPrompt<'a>), |
| 19 BinaryPrompt(&'a BinaryPrompt<'a>), | 20 BinaryPrompt(&'a BinaryPrompt<'a>), |
| 20 InfoMsg(&'a InfoMsg<'a>), | 21 InfoMsg(&'a InfoMsg<'a>), |
| 21 ErrorMsg(&'a ErrorMsg<'a>), | 22 ErrorMsg(&'a ErrorMsg<'a>), |
| 23 } | |
| 24 | |
| 25 impl Message<'_> { | |
| 26 /// Sets an error answer on this question, without having to inspect it. | |
| 27 /// | |
| 28 /// Use this as a default match case: | |
| 29 /// | |
| 30 /// ``` | |
| 31 /// use nonstick::conv::{Message, QAndA}; | |
| 32 /// use nonstick::ErrorCode; | |
| 33 /// | |
| 34 /// fn cant_respond(message: Message) { | |
| 35 /// match message { | |
| 36 /// Message::InfoMsg(i) => { | |
| 37 /// eprintln!("fyi, {}", i.question()); | |
| 38 /// i.set_answer(Ok(())) | |
| 39 /// } | |
| 40 /// Message::ErrorMsg(e) => { | |
| 41 /// eprintln!("ERROR: {}", e.question()); | |
| 42 /// e.set_answer(Ok(())) | |
| 43 /// } | |
| 44 /// // We can't answer any questions. | |
| 45 /// other => other.set_error(ErrorCode::ConversationError), | |
| 46 /// } | |
| 47 /// } | |
| 48 pub fn set_error(&self, err: ErrorCode) { | |
| 49 match self { | |
| 50 Message::MaskedPrompt(m) => m.set_answer(Err(err)), | |
| 51 Message::Prompt(m) => m.set_answer(Err(err)), | |
| 52 Message::RadioPrompt(m) => m.set_answer(Err(err)), | |
| 53 Message::BinaryPrompt(m) => m.set_answer(Err(err)), | |
| 54 Message::InfoMsg(m) => m.set_answer(Err(err)), | |
| 55 Message::ErrorMsg(m) => m.set_answer(Err(err)), | |
| 56 } | |
| 57 } | |
| 22 } | 58 } |
| 23 | 59 |
| 24 /// A question-and-answer pair that can be communicated in a [`Conversation`]. | 60 /// A question-and-answer pair that can be communicated in a [`Conversation`]. |
| 25 /// | 61 /// |
| 26 /// The asking side creates a `QAndA`, then converts it to a [`Message`] | 62 /// The asking side creates a `QAndA`, then converts it to a [`Message`] |
