Mercurial > crates > nonstick
annotate 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 |
rev | line source |
---|---|
69
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
1 //! The PAM conversation and associated Stuff. |
60
05cc2c27334f
The Big Refactor: clean up docs and exports.
Paul Fisher <paul@pfish.zone>
parents:
59
diff
changeset
|
2 |
70
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
3 // Temporarily allowed until we get the actual conversation functions hooked up. |
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
4 #![allow(dead_code)] |
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
5 |
71
58f9d2a4df38
Reorganize everything again???
Paul Fisher <paul@pfish.zone>
parents:
70
diff
changeset
|
6 use crate::constants::Result; |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
7 use crate::ErrorCode; |
70
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
8 use secure_string::SecureString; |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
9 use std::cell::Cell; |
70
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
10 |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
11 /// The types of message and request that can be sent to a user. |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
12 /// |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
13 /// The data within each enum value is the prompt (or other information) |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
14 /// that will be presented to the user. |
76
e58d24849e82
Add Message::set_error to quickly answer a question with an error.
Paul Fisher <paul@pfish.zone>
parents:
74
diff
changeset
|
15 #[non_exhaustive] |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
16 pub enum Message<'a> { |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
17 MaskedPrompt(&'a MaskedPrompt<'a>), |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
18 Prompt(&'a Prompt<'a>), |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
19 RadioPrompt(&'a RadioPrompt<'a>), |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
20 BinaryPrompt(&'a BinaryPrompt<'a>), |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
21 InfoMsg(&'a InfoMsg<'a>), |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
22 ErrorMsg(&'a ErrorMsg<'a>), |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
23 } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
24 |
76
e58d24849e82
Add Message::set_error to quickly answer a question with an error.
Paul Fisher <paul@pfish.zone>
parents:
74
diff
changeset
|
25 impl Message<'_> { |
e58d24849e82
Add Message::set_error to quickly answer a question with an error.
Paul Fisher <paul@pfish.zone>
parents:
74
diff
changeset
|
26 /// Sets an error answer on this question, without having to inspect it. |
e58d24849e82
Add Message::set_error to quickly answer a question with an error.
Paul Fisher <paul@pfish.zone>
parents:
74
diff
changeset
|
27 /// |
e58d24849e82
Add Message::set_error to quickly answer a question with an error.
Paul Fisher <paul@pfish.zone>
parents:
74
diff
changeset
|
28 /// Use this as a default match case: |
e58d24849e82
Add Message::set_error to quickly answer a question with an error.
Paul Fisher <paul@pfish.zone>
parents:
74
diff
changeset
|
29 /// |
e58d24849e82
Add Message::set_error to quickly answer a question with an error.
Paul Fisher <paul@pfish.zone>
parents:
74
diff
changeset
|
30 /// ``` |
e58d24849e82
Add Message::set_error to quickly answer a question with an error.
Paul Fisher <paul@pfish.zone>
parents:
74
diff
changeset
|
31 /// use nonstick::conv::{Message, QAndA}; |
e58d24849e82
Add Message::set_error to quickly answer a question with an error.
Paul Fisher <paul@pfish.zone>
parents:
74
diff
changeset
|
32 /// use nonstick::ErrorCode; |
e58d24849e82
Add Message::set_error to quickly answer a question with an error.
Paul Fisher <paul@pfish.zone>
parents:
74
diff
changeset
|
33 /// |
e58d24849e82
Add Message::set_error to quickly answer a question with an error.
Paul Fisher <paul@pfish.zone>
parents:
74
diff
changeset
|
34 /// fn cant_respond(message: Message) { |
e58d24849e82
Add Message::set_error to quickly answer a question with an error.
Paul Fisher <paul@pfish.zone>
parents:
74
diff
changeset
|
35 /// match message { |
e58d24849e82
Add Message::set_error to quickly answer a question with an error.
Paul Fisher <paul@pfish.zone>
parents:
74
diff
changeset
|
36 /// Message::InfoMsg(i) => { |
e58d24849e82
Add Message::set_error to quickly answer a question with an error.
Paul Fisher <paul@pfish.zone>
parents:
74
diff
changeset
|
37 /// eprintln!("fyi, {}", i.question()); |
e58d24849e82
Add Message::set_error to quickly answer a question with an error.
Paul Fisher <paul@pfish.zone>
parents:
74
diff
changeset
|
38 /// i.set_answer(Ok(())) |
e58d24849e82
Add Message::set_error to quickly answer a question with an error.
Paul Fisher <paul@pfish.zone>
parents:
74
diff
changeset
|
39 /// } |
e58d24849e82
Add Message::set_error to quickly answer a question with an error.
Paul Fisher <paul@pfish.zone>
parents:
74
diff
changeset
|
40 /// Message::ErrorMsg(e) => { |
e58d24849e82
Add Message::set_error to quickly answer a question with an error.
Paul Fisher <paul@pfish.zone>
parents:
74
diff
changeset
|
41 /// eprintln!("ERROR: {}", e.question()); |
e58d24849e82
Add Message::set_error to quickly answer a question with an error.
Paul Fisher <paul@pfish.zone>
parents:
74
diff
changeset
|
42 /// e.set_answer(Ok(())) |
e58d24849e82
Add Message::set_error to quickly answer a question with an error.
Paul Fisher <paul@pfish.zone>
parents:
74
diff
changeset
|
43 /// } |
e58d24849e82
Add Message::set_error to quickly answer a question with an error.
Paul Fisher <paul@pfish.zone>
parents:
74
diff
changeset
|
44 /// // We can't answer any questions. |
e58d24849e82
Add Message::set_error to quickly answer a question with an error.
Paul Fisher <paul@pfish.zone>
parents:
74
diff
changeset
|
45 /// other => other.set_error(ErrorCode::ConversationError), |
e58d24849e82
Add Message::set_error to quickly answer a question with an error.
Paul Fisher <paul@pfish.zone>
parents:
74
diff
changeset
|
46 /// } |
e58d24849e82
Add Message::set_error to quickly answer a question with an error.
Paul Fisher <paul@pfish.zone>
parents:
74
diff
changeset
|
47 /// } |
e58d24849e82
Add Message::set_error to quickly answer a question with an error.
Paul Fisher <paul@pfish.zone>
parents:
74
diff
changeset
|
48 pub fn set_error(&self, err: ErrorCode) { |
e58d24849e82
Add Message::set_error to quickly answer a question with an error.
Paul Fisher <paul@pfish.zone>
parents:
74
diff
changeset
|
49 match self { |
e58d24849e82
Add Message::set_error to quickly answer a question with an error.
Paul Fisher <paul@pfish.zone>
parents:
74
diff
changeset
|
50 Message::MaskedPrompt(m) => m.set_answer(Err(err)), |
e58d24849e82
Add Message::set_error to quickly answer a question with an error.
Paul Fisher <paul@pfish.zone>
parents:
74
diff
changeset
|
51 Message::Prompt(m) => m.set_answer(Err(err)), |
e58d24849e82
Add Message::set_error to quickly answer a question with an error.
Paul Fisher <paul@pfish.zone>
parents:
74
diff
changeset
|
52 Message::RadioPrompt(m) => m.set_answer(Err(err)), |
e58d24849e82
Add Message::set_error to quickly answer a question with an error.
Paul Fisher <paul@pfish.zone>
parents:
74
diff
changeset
|
53 Message::BinaryPrompt(m) => m.set_answer(Err(err)), |
e58d24849e82
Add Message::set_error to quickly answer a question with an error.
Paul Fisher <paul@pfish.zone>
parents:
74
diff
changeset
|
54 Message::InfoMsg(m) => m.set_answer(Err(err)), |
e58d24849e82
Add Message::set_error to quickly answer a question with an error.
Paul Fisher <paul@pfish.zone>
parents:
74
diff
changeset
|
55 Message::ErrorMsg(m) => m.set_answer(Err(err)), |
e58d24849e82
Add Message::set_error to quickly answer a question with an error.
Paul Fisher <paul@pfish.zone>
parents:
74
diff
changeset
|
56 } |
e58d24849e82
Add Message::set_error to quickly answer a question with an error.
Paul Fisher <paul@pfish.zone>
parents:
74
diff
changeset
|
57 } |
e58d24849e82
Add Message::set_error to quickly answer a question with an error.
Paul Fisher <paul@pfish.zone>
parents:
74
diff
changeset
|
58 } |
e58d24849e82
Add Message::set_error to quickly answer a question with an error.
Paul Fisher <paul@pfish.zone>
parents:
74
diff
changeset
|
59 |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
60 /// A question-and-answer pair that can be communicated in a [`Conversation`]. |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
61 /// |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
62 /// The asking side creates a `QAndA`, then converts it to a [`Message`] |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
63 /// and sends it via a [`Conversation`]. The Conversation then retrieves |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
64 /// the answer to the question (if needed) and sets the response. |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
65 /// Once control returns to the asker, the asker gets the answer from this |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
66 /// `QAndA` and uses it however it wants. |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
67 /// |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
68 /// For a more detailed explanation of how this works, |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
69 /// see [`Conversation::communicate`]. |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
70 pub trait QAndA<'a> { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
71 /// The type of the content of the question. |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
72 type Question: Copy; |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
73 /// The type of the answer to the question. |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
74 type Answer; |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
75 |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
76 /// Converts this Q-and-A pair into a [`Message`] for the [`Conversation`]. |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
77 fn message(&self) -> Message; |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
78 |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
79 /// The contents of the question being asked. |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
80 /// |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
81 /// For instance, this might say `"Username:"` to prompt the user |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
82 /// for their name. |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
83 fn question(&self) -> Self::Question; |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
84 |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
85 /// Sets the answer to the question. |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
86 /// |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
87 /// The [`Conversation`] implementation calls this to set the answer. |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
88 /// The conversation should *always call this function*, even for messages |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
89 /// that don't have "an answer" (like error or info messages). |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
90 fn set_answer(&self, answer: Result<Self::Answer>); |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
91 |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
92 /// Gets the answer to the question. |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
93 fn answer(self) -> Result<Self::Answer>; |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
94 } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
95 |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
96 macro_rules! q_and_a { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
97 ($name:ident<'a, Q=$qt:ty, A=$at:ty>, $($doc:literal)*) => { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
98 $( |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
99 #[doc = $doc] |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
100 )* |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
101 pub struct $name<'a> { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
102 q: $qt, |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
103 a: Cell<Result<$at>>, |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
104 } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
105 |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
106 impl<'a> QAndA<'a> for $name<'a> { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
107 type Question = $qt; |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
108 type Answer = $at; |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
109 |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
110 fn question(&self) -> Self::Question { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
111 self.q |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
112 } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
113 |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
114 fn set_answer(&self, answer: Result<Self::Answer>) { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
115 self.a.set(answer) |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
116 } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
117 |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
118 fn answer(self) -> Result<Self::Answer> { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
119 self.a.into_inner() |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
120 } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
121 |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
122 fn message(&self) -> Message { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
123 Message::$name(self) |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
124 } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
125 } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
126 }; |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
127 } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
128 |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
129 macro_rules! ask { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
130 ($t:ident) => { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
131 impl<'a> $t<'a> { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
132 #[doc = concat!("Creates a `", stringify!($t), "` to be sent to the user.")] |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
133 fn ask(question: &'a str) -> Self { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
134 Self { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
135 q: question, |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
136 a: Cell::new(Err(ErrorCode::ConversationError)), |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
137 } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
138 } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
139 } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
140 }; |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
141 } |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
142 |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
143 q_and_a!( |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
144 MaskedPrompt<'a, Q=&'a str, A=SecureString>, |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
145 "Asks the user for data and does not echo it back while being entered." |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
146 "" |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
147 "In other words, a password entry prompt." |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
148 ); |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
149 ask!(MaskedPrompt); |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
150 |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
151 q_and_a!( |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
152 Prompt<'a, Q=&'a str, A=String>, |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
153 "Asks the user for data." |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
154 "" |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
155 "This is the normal \"ask a person a question\" prompt." |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
156 "When the user types, their input will be shown to them." |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
157 "It can be used for things like usernames." |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
158 ); |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
159 ask!(Prompt); |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
160 |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
161 q_and_a!( |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
162 RadioPrompt<'a, Q=&'a str, A=String>, |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
163 "Asks the user for \"radio button\"–style data. (Linux-PAM extension)" |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
164 "" |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
165 "This message type is theoretically useful for \"yes/no/maybe\"" |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
166 "questions, but nowhere in the documentation is it specified" |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
167 "what the format of the answer will be, or how this should be shown." |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
168 ); |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
169 ask!(RadioPrompt); |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
170 |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
171 q_and_a!( |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
172 BinaryPrompt<'a, Q=BinaryQuestion<'a>, A=BinaryData>, |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
173 "Asks for binary data. (Linux-PAM extension)" |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
174 "" |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
175 "This sends a binary message to the client application." |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
176 "It can be used to communicate with non-human logins," |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
177 "or to enable things like security keys." |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
178 "" |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
179 "The `data_type` tag is a value that is simply passed through" |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
180 "to the application. PAM does not define any meaning for it." |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
181 ); |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
182 impl<'a> BinaryPrompt<'a> { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
183 /// Creates a prompt for the given binary data. |
70
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
184 /// |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
185 /// The `data_type` is a tag you can use for communication between |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
186 /// the module and the application. Its meaning is undefined by PAM. |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
187 fn ask(data: &'a [u8], data_type: u8) -> Self { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
188 Self { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
189 q: BinaryQuestion { data, data_type }, |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
190 a: Cell::new(Err(ErrorCode::ConversationError)), |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
191 } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
192 } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
193 } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
194 |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
195 /// The contents of a question requesting binary data. |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
196 /// |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
197 /// A borrowed version of [`BinaryData`]. |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
198 #[derive(Copy, Clone, Debug)] |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
199 pub struct BinaryQuestion<'a> { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
200 data: &'a [u8], |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
201 data_type: u8, |
70
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
202 } |
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
203 |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
204 impl BinaryQuestion<'_> { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
205 /// Gets the data of this question. |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
206 pub fn data(&self) -> &[u8] { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
207 self.data |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
208 } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
209 |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
210 /// Gets the "type" of this data. |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
211 pub fn data_type(&self) -> u8 { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
212 self.data_type |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
213 } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
214 } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
215 |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
216 /// Owned binary data. |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
217 /// |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
218 /// For borrowed data, see [`BinaryQuestion`]. |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
219 /// You can take ownership of the stored data with `.into::<Vec<u8>>()`. |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
220 #[derive(Debug, PartialEq)] |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
221 pub struct BinaryData { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
222 data: Vec<u8>, |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
223 data_type: u8, |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
224 } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
225 |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
226 impl BinaryData { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
227 /// Creates a `BinaryData` with the given contents and type. |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
228 pub fn new(data: Vec<u8>, data_type: u8) -> Self { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
229 Self { data, data_type } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
230 } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
231 /// A borrowed view of the data here. |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
232 pub fn data(&self) -> &[u8] { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
233 &self.data |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
234 } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
235 /// The type of the data stored in this. |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
236 pub fn data_type(&self) -> u8 { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
237 self.data_type |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
238 } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
239 } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
240 |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
241 impl From<BinaryData> for Vec<u8> { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
242 /// Takes ownership of the data stored herein. |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
243 fn from(value: BinaryData) -> Self { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
244 value.data |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
245 } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
246 } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
247 |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
248 q_and_a!( |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
249 InfoMsg<'a, Q = &'a str, A = ()>, |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
250 "A message containing information to be passed to the user." |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
251 "" |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
252 "While this does not have an answer, [`Conversation`] implementations" |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
253 "should still call [`set_answer`][`QAndA::set_answer`] to verify that" |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
254 "the message has been displayed (or actively discarded)." |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
255 ); |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
256 impl<'a> InfoMsg<'a> { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
257 /// Creates an informational message to send to the user. |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
258 fn new(message: &'a str) -> Self { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
259 Self { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
260 q: message, |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
261 a: Cell::new(Err(ErrorCode::ConversationError)), |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
262 } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
263 } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
264 } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
265 |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
266 q_and_a!( |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
267 ErrorMsg<'a, Q = &'a str, A = ()>, |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
268 "An error message to be passed to the user." |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
269 "" |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
270 "While this does not have an answer, [`Conversation`] implementations" |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
271 "should still call [`set_answer`][`QAndA::set_answer`] to verify that" |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
272 "the message has been displayed (or actively discarded)." |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
273 |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
274 ); |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
275 impl<'a> ErrorMsg<'a> { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
276 /// Creates an error message to send to the user. |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
277 fn new(message: &'a str) -> Self { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
278 Self { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
279 q: message, |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
280 a: Cell::new(Err(ErrorCode::ConversationError)), |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
281 } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
282 } |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
283 } |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
284 |
70
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
285 /// A channel for PAM modules to request information from the user. |
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
286 /// |
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
287 /// This trait is used by both applications and PAM modules: |
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
288 /// |
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
289 /// - Applications implement Conversation and provide a user interface |
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
290 /// to allow the user to respond to PAM questions. |
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
291 /// - Modules call a Conversation implementation to request information |
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
292 /// or send information to the user. |
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
293 pub trait Conversation { |
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
294 /// Sends messages to the user. |
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
295 /// |
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
296 /// The returned Vec of messages always contains exactly as many entries |
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
297 /// as there were messages in the request; one corresponding to each. |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
298 /// |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
299 /// TODO: write detailed documentation about how to use this. |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
300 fn communicate(&mut self, messages: &[Message]); |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
301 } |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
302 |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
303 /// Turns a simple function into a [`Conversation`]. |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
304 /// |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
305 /// This can be used to wrap a free-floating function for use as a |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
306 /// Conversation: |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
307 /// |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
308 /// ``` |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
309 /// use nonstick::conv::{Conversation, Message, conversation_func}; |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
310 /// mod some_library { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
311 /// # use nonstick::Conversation; |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
312 /// pub fn get_auth_data(conv: &mut impl Conversation) { /* ... */ } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
313 /// } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
314 /// |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
315 /// fn my_terminal_prompt(messages: &[Message]) { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
316 /// // ... |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
317 /// # todo!() |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
318 /// } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
319 /// |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
320 /// fn main() { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
321 /// some_library::get_auth_data(&mut conversation_func(my_terminal_prompt)); |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
322 /// } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
323 /// ``` |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
324 pub fn conversation_func(func: impl FnMut(&[Message])) -> impl Conversation { |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
325 Convo(func) |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
326 } |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
327 |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
328 struct Convo<C: FnMut(&[Message])>(C); |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
329 |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
330 impl<C: FnMut(&[Message])> Conversation for Convo<C> { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
331 fn communicate(&mut self, messages: &[Message]) { |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
332 self.0(messages) |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
333 } |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
334 } |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
335 |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
336 /// A conversation trait for asking or answering one question at a time. |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
337 /// |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
338 /// An implementation of this is provided for any [`Conversation`], |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
339 /// or a PAM application can implement this trait and handle messages |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
340 /// one at a time. |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
341 /// |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
342 /// For example, to use a `Conversation` as a `SimpleConversation`: |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
343 /// |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
344 /// ``` |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
345 /// # use nonstick::{Conversation, Result}; |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
346 /// # use secure_string::SecureString; |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
347 /// // Bring this trait into scope to get `masked_prompt`, among others. |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
348 /// use nonstick::SimpleConversation; |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
349 /// |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
350 /// fn ask_for_token(convo: &mut impl Conversation) -> Result<SecureString> { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
351 /// convo.masked_prompt("enter your one-time token") |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
352 /// } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
353 /// ``` |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
354 /// |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
355 /// or to use a `SimpleConversation` as a `Conversation`: |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
356 /// |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
357 /// ``` |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
358 /// use secure_string::SecureString; |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
359 /// use nonstick::{Conversation, SimpleConversation}; |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
360 /// # use nonstick::{BinaryData, Result}; |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
361 /// mod some_library { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
362 /// # use nonstick::Conversation; |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
363 /// pub fn get_auth_data(conv: &mut impl Conversation) { /* ... */ } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
364 /// } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
365 /// |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
366 /// struct MySimpleConvo { /* ... */ } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
367 /// # impl MySimpleConvo { fn new() -> Self { Self{} } } |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
368 /// |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
369 /// impl SimpleConversation for MySimpleConvo { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
370 /// // ... |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
371 /// # fn prompt(&mut self, request: &str) -> Result<String> { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
372 /// # todo!() |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
373 /// # } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
374 /// # |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
375 /// # fn masked_prompt(&mut self, request: &str) -> Result<SecureString> { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
376 /// # todo!() |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
377 /// # } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
378 /// # |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
379 /// # fn radio_prompt(&mut self, request: &str) -> Result<String> { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
380 /// # todo!() |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
381 /// # } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
382 /// # |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
383 /// # fn error_msg(&mut self, message: &str) { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
384 /// # todo!() |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
385 /// # } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
386 /// # |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
387 /// # fn info_msg(&mut self, message: &str) { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
388 /// # todo!() |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
389 /// # } |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
390 /// # |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
391 /// # fn binary_prompt(&mut self, data: &[u8], data_type: u8) -> Result<BinaryData> { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
392 /// # todo!() |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
393 /// # } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
394 /// } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
395 /// |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
396 /// fn main() { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
397 /// let mut simple = MySimpleConvo::new(); |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
398 /// some_library::get_auth_data(&mut simple.as_conversation()) |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
399 /// } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
400 /// ``` |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
401 pub trait SimpleConversation { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
402 /// Lets you use this simple conversation as a full [Conversation]. |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
403 /// |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
404 /// The wrapper takes each message received in [`Conversation::communicate`] |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
405 /// and passes them one-by-one to the appropriate method, |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
406 /// then collects responses to return. |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
407 fn as_conversation(&mut self) -> Demux<Self> |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
408 where |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
409 Self: Sized, |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
410 { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
411 Demux(self) |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
412 } |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
413 /// Prompts the user for something. |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
414 fn prompt(&mut self, request: &str) -> Result<String>; |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
415 /// Prompts the user for something, but hides what the user types. |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
416 fn masked_prompt(&mut self, request: &str) -> Result<SecureString>; |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
417 /// Prompts the user for a yes/no/maybe conditional (a Linux-PAM extension). |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
418 /// |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
419 /// PAM documentation doesn't define the format of the response. |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
420 fn radio_prompt(&mut self, request: &str) -> Result<String>; |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
421 /// Alerts the user to an error. |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
422 fn error_msg(&mut self, message: &str); |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
423 /// Sends an informational message to the user. |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
424 fn info_msg(&mut self, message: &str); |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
425 /// Requests binary data from the user (a Linux-PAM extension). |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
426 fn binary_prompt(&mut self, data: &[u8], data_type: u8) -> Result<BinaryData>; |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
427 } |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
428 |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
429 macro_rules! conv_fn { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
430 ($fn_name:ident($($param:ident: $pt:ty),+) -> $resp_type:ty { $ask:path }) => { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
431 fn $fn_name(&mut self, $($param: $pt),*) -> Result<$resp_type> { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
432 let prompt = $ask($($param),*); |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
433 self.communicate(&[prompt.message()]); |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
434 prompt.answer() |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
435 } |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
436 }; |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
437 ($fn_name:ident($($param:ident: $pt:ty),+) { $ask:path }) => { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
438 fn $fn_name(&mut self, $($param: $pt),*) { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
439 self.communicate(&[$ask($($param),*).message()]); |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
440 } |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
441 }; |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
442 } |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
443 |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
444 impl<C: Conversation> SimpleConversation for C { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
445 conv_fn!(prompt(message: &str) -> String { Prompt::ask }); |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
446 conv_fn!(masked_prompt(message: &str) -> SecureString { MaskedPrompt::ask }); |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
447 conv_fn!(radio_prompt(message: &str) -> String { RadioPrompt::ask }); |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
448 conv_fn!(error_msg(message: &str) { ErrorMsg::new }); |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
449 conv_fn!(info_msg(message: &str) { InfoMsg::new }); |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
450 conv_fn!(binary_prompt(data: &[u8], data_type: u8) -> BinaryData { BinaryPrompt::ask }); |
70
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
451 } |
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
452 |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
453 /// A [`Conversation`] which asks the questions one at a time. |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
454 /// |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
455 /// This is automatically created by [`SimpleConversation::as_conversation`]. |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
456 pub struct Demux<'a, SC: SimpleConversation>(&'a mut SC); |
70
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
457 |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
458 impl<SC: SimpleConversation> Conversation for Demux<'_, SC> { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
459 fn communicate(&mut self, messages: &[Message]) { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
460 for msg in messages { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
461 match msg { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
462 Message::Prompt(prompt) => prompt.set_answer(self.0.prompt(prompt.question())), |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
463 Message::MaskedPrompt(prompt) => { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
464 prompt.set_answer(self.0.masked_prompt(prompt.question())) |
70
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
465 } |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
466 Message::RadioPrompt(prompt) => { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
467 prompt.set_answer(self.0.radio_prompt(prompt.question())) |
70
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
468 } |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
469 Message::InfoMsg(prompt) => { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
470 self.0.info_msg(prompt.question()); |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
471 prompt.set_answer(Ok(())) |
70
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
472 } |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
473 Message::ErrorMsg(prompt) => { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
474 self.0.error_msg(prompt.question()); |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
475 prompt.set_answer(Ok(())) |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
476 } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
477 Message::BinaryPrompt(prompt) => { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
478 let q = prompt.question(); |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
479 prompt.set_answer(self.0.binary_prompt(q.data, q.data_type)) |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
480 } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
481 } |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
482 } |
70
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
483 } |
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
484 } |
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
485 |
69
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
486 #[cfg(test)] |
71
58f9d2a4df38
Reorganize everything again???
Paul Fisher <paul@pfish.zone>
parents:
70
diff
changeset
|
487 mod tests { |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
488 use super::{ |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
489 BinaryPrompt, Conversation, ErrorMsg, InfoMsg, MaskedPrompt, Message, Prompt, QAndA, |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
490 RadioPrompt, Result, SecureString, SimpleConversation, |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
491 }; |
70
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
492 use crate::constants::ErrorCode; |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
493 use crate::BinaryData; |
70
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
494 |
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
495 #[test] |
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
496 fn test_demux() { |
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
497 #[derive(Default)] |
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
498 struct DemuxTester { |
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
499 error_ran: bool, |
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
500 info_ran: bool, |
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
501 } |
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
502 |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
503 impl SimpleConversation for DemuxTester { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
504 fn prompt(&mut self, request: &str) -> Result<String> { |
70
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
505 match request { |
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
506 "what" => Ok("whatwhat".to_owned()), |
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
507 "give_err" => Err(ErrorCode::PermissionDenied), |
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
508 _ => panic!("unexpected prompt!"), |
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
509 } |
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
510 } |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
511 fn masked_prompt(&mut self, request: &str) -> Result<SecureString> { |
70
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
512 assert_eq!("reveal", request); |
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
513 Ok(SecureString::from("my secrets")) |
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
514 } |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
515 fn radio_prompt(&mut self, request: &str) -> Result<String> { |
70
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
516 assert_eq!("channel?", request); |
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
517 Ok("zero".to_owned()) |
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
518 } |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
519 fn error_msg(&mut self, message: &str) { |
70
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
520 self.error_ran = true; |
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
521 assert_eq!("whoopsie", message); |
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
522 } |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
523 fn info_msg(&mut self, message: &str) { |
70
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
524 self.info_ran = true; |
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
525 assert_eq!("did you know", message); |
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
526 } |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
527 fn binary_prompt(&mut self, data: &[u8], data_type: u8) -> Result<BinaryData> { |
70
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
528 assert_eq!(&[10, 9, 8], data); |
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
529 assert_eq!(66, data_type); |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
530 Ok(BinaryData::new(vec![5, 5, 5], 5)) |
70
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
531 } |
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
532 } |
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
533 |
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
534 let mut tester = DemuxTester::default(); |
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
535 |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
536 let what = Prompt::ask("what"); |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
537 let pass = MaskedPrompt::ask("reveal"); |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
538 let err = ErrorMsg::new("whoopsie"); |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
539 let info = InfoMsg::new("did you know"); |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
540 let has_err = Prompt::ask("give_err"); |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
541 |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
542 let mut conv = tester.as_conversation(); |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
543 |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
544 // Basic tests. |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
545 |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
546 conv.communicate(&[ |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
547 what.message(), |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
548 pass.message(), |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
549 err.message(), |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
550 info.message(), |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
551 has_err.message(), |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
552 ]); |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
553 |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
554 assert_eq!("whatwhat", what.answer().unwrap()); |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
555 assert_eq!(SecureString::from("my secrets"), pass.answer().unwrap()); |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
556 assert_eq!(Ok(()), err.answer()); |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
557 assert_eq!(Ok(()), info.answer()); |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
558 assert_eq!(ErrorCode::PermissionDenied, has_err.answer().unwrap_err()); |
70
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
559 assert!(tester.error_ran); |
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
560 assert!(tester.info_ran); |
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
561 |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
562 // Test the Linux extensions separately. |
70
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
563 |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
564 let mut conv = tester.as_conversation(); |
70
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
565 |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
566 let radio = RadioPrompt::ask("channel?"); |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
567 let bin = BinaryPrompt::ask(&[10, 9, 8], 66); |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
568 conv.communicate(&[radio.message(), bin.message()]); |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
569 |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
570 assert_eq!("zero", radio.answer().unwrap()); |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
571 assert_eq!(BinaryData::new(vec![5, 5, 5], 5), bin.answer().unwrap()); |
70
9f8381a1c09c
Implement low-level conversation primitives.
Paul Fisher <paul@pfish.zone>
parents:
69
diff
changeset
|
572 } |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
573 |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
574 fn test_mux() { |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
575 struct MuxTester; |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
576 |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
577 impl Conversation for MuxTester { |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
578 fn communicate(&mut self, messages: &[Message]) { |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
579 if let [msg] = messages { |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
580 match *msg { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
581 Message::InfoMsg(info) => { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
582 assert_eq!("let me tell you", info.question()); |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
583 info.set_answer(Ok(())) |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
584 } |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
585 Message::ErrorMsg(error) => { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
586 assert_eq!("oh no", error.question()); |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
587 error.set_answer(Ok(())) |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
588 } |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
589 Message::Prompt(prompt) => prompt.set_answer(match prompt.question() { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
590 "should_err" => Err(ErrorCode::PermissionDenied), |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
591 "question" => Ok("answer".to_owned()), |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
592 other => panic!("unexpected question {other:?}"), |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
593 }), |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
594 Message::MaskedPrompt(ask) => { |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
595 assert_eq!("password!", ask.question()); |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
596 ask.set_answer(Ok("open sesame".into())) |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
597 } |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
598 Message::BinaryPrompt(prompt) => { |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
599 assert_eq!(&[1, 2, 3], prompt.question().data); |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
600 assert_eq!(69, prompt.question().data_type); |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
601 prompt.set_answer(Ok(BinaryData::new(vec![3, 2, 1], 42))) |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
602 } |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
603 Message::RadioPrompt(ask) => { |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
604 assert_eq!("radio?", ask.question()); |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
605 ask.set_answer(Ok("yes".to_owned())) |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
606 } |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
607 } |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
608 } else { |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
609 panic!( |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
610 "there should only be one message, not {len}", |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
611 len = messages.len() |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
612 ) |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
613 } |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
614 } |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
615 } |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
616 |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
617 let mut tester = MuxTester; |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
618 |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
619 assert_eq!("answer", tester.prompt("question").unwrap()); |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
620 assert_eq!( |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
621 SecureString::from("open sesame"), |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
622 tester.masked_prompt("password!").unwrap() |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
623 ); |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
624 tester.error_msg("oh no"); |
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
625 tester.info_msg("let me tell you"); |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
626 { |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
627 assert_eq!("yes", tester.radio_prompt("radio?").unwrap()); |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
628 assert_eq!( |
74
c7c596e6388f
Make conversations type-safe (last big reorg) (REAL) (NOT CLICKBAIT)
Paul Fisher <paul@pfish.zone>
parents:
73
diff
changeset
|
629 BinaryData::new(vec![3, 2, 1], 42), |
73
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
630 tester.binary_prompt(&[1, 2, 3], 69).unwrap(), |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
631 ) |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
632 } |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
633 assert_eq!( |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
634 ErrorCode::BufferError, |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
635 tester.prompt("should_error").unwrap_err(), |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
636 ); |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
637 assert_eq!( |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
638 ErrorCode::ConversationError, |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
639 tester.masked_prompt("return_wrong_type").unwrap_err() |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
640 ) |
ac6881304c78
Do conversations, along with way too much stuff.
Paul Fisher <paul@pfish.zone>
parents:
72
diff
changeset
|
641 } |
69
8f3ae0c7ab92
Rework conversation data types and make safe wrappers.
Paul Fisher <paul@pfish.zone>
parents:
64
diff
changeset
|
642 } |