derive_wizard_types/
interview.rs1use crate::{AssumedAnswer, SuggestedAnswer};
2
3#[derive(Debug, Clone)]
5pub struct Interview {
6 pub sections: Vec<Question>,
7 pub prelude: Option<String>,
8 pub epilogue: Option<String>,
9}
10
11#[derive(Debug, Clone)]
12pub struct Question {
13 id: Option<String>,
15
16 name: String,
18
19 prompt: String,
21
22 kind: QuestionKind,
23
24 assumed: Option<AssumedAnswer>,
26}
27
28impl Question {
29 pub const fn new(id: Option<String>, name: String, prompt: String, kind: QuestionKind) -> Self {
31 Self {
32 id,
33 name,
34 prompt,
35 kind,
36 assumed: None,
37 }
38 }
39
40 pub fn id(&self) -> Option<&str> {
41 self.id.as_deref()
42 }
43
44 pub fn name(&self) -> &str {
45 &self.name
46 }
47
48 pub fn prompt(&self) -> &str {
49 &self.prompt
50 }
51
52 pub const fn kind(&self) -> &QuestionKind {
53 &self.kind
54 }
55
56 pub const fn kind_mut(&mut self) -> &mut QuestionKind {
57 &mut self.kind
58 }
59
60 pub const fn assumed(&self) -> Option<&AssumedAnswer> {
61 self.assumed.as_ref()
62 }
63
64 pub fn set_suggestion(&mut self, value: impl Into<SuggestedAnswer>) {
66 match (&mut self.kind, value.into()) {
67 (QuestionKind::Input(q), SuggestedAnswer::String(v)) => {
68 q.default = Some(v);
69 }
70 (QuestionKind::Multiline(q), SuggestedAnswer::String(v)) => {
71 q.default = Some(v);
72 }
73 (QuestionKind::Int(q), SuggestedAnswer::Int(v)) => {
74 q.default = Some(v);
75 }
76 (QuestionKind::Float(q), SuggestedAnswer::Float(v)) => {
77 q.default = Some(v);
78 }
79 (QuestionKind::Confirm(q), SuggestedAnswer::Bool(v)) => {
80 q.default = v;
81 }
82 _ => {}
83 }
84 }
85
86 pub fn set_assumption(&mut self, value: impl Into<AssumedAnswer>) {
88 self.assumed = Some(value.into());
89 }
90}
91
92#[derive(Debug, Clone)]
94pub enum QuestionKind {
95 Input(InputQuestion),
97
98 Multiline(MultilineQuestion),
100
101 Masked(MaskedQuestion),
103
104 Int(IntQuestion),
106
107 Float(FloatQuestion),
109
110 Confirm(ConfirmQuestion),
112
113 MultiSelect(MultiSelectQuestion),
115
116 Sequence(Vec<Question>),
117
118 Alternative(usize, Vec<Question>),
119}
120
121impl QuestionKind {
122 pub fn is_enum_alternatives(&self) -> bool {
124 matches!(self, Self::Sequence(questions)
125 if !questions.is_empty()
126 && questions.iter().all(|q| matches!(q.kind(), Self::Alternative(_, _))))
127 }
128}
129
130#[derive(Debug, Clone)]
132pub struct InputQuestion {
133 pub default: Option<String>,
135
136 pub validate: Option<String>,
138}
139
140#[derive(Debug, Clone)]
142pub struct MultilineQuestion {
143 pub default: Option<String>,
145
146 pub validate: Option<String>,
148}
149
150#[derive(Debug, Clone)]
152pub struct MaskedQuestion {
153 pub mask: Option<char>,
155
156 pub validate: Option<String>,
158}
159
160#[derive(Debug, Clone)]
162pub struct IntQuestion {
163 pub default: Option<i64>,
165
166 pub min: Option<i64>,
168
169 pub max: Option<i64>,
171
172 pub validate: Option<String>,
174}
175
176#[derive(Debug, Clone)]
178pub struct FloatQuestion {
179 pub default: Option<f64>,
181
182 pub min: Option<f64>,
184
185 pub max: Option<f64>,
187
188 pub validate: Option<String>,
190}
191
192#[derive(Debug, Clone)]
194pub struct ConfirmQuestion {
195 pub default: bool,
197}
198
199#[derive(Debug, Clone)]
201pub struct MultiSelectQuestion {
202 pub options: Vec<String>,
204
205 pub defaults: Vec<usize>,
207}