elicitor-types 0.6.1

Core types for the elicit crate - presentation-agnostic survey definitions.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
use crate::{DefaultValue, ResponsePath, ResponseValue};

/// A single question in a survey.
#[derive(Debug, Clone, PartialEq)]
pub struct Question {
    /// The path to this question's response in the Responses map.
    path: ResponsePath,

    /// The prompt text shown to the user.
    ask: String,

    /// The kind of question (determines input type and nested structure).
    kind: QuestionKind,

    /// Default value for this question (none, suggested, or assumed).
    default: DefaultValue,
}

impl Question {
    /// Create a new question.
    pub fn new(path: impl Into<ResponsePath>, ask: impl Into<String>, kind: QuestionKind) -> Self {
        Self {
            path: path.into(),
            ask: ask.into(),
            kind,
            default: DefaultValue::None,
        }
    }

    /// Get the response path for this question.
    pub fn path(&self) -> &ResponsePath {
        &self.path
    }

    /// Get the prompt text.
    pub fn ask(&self) -> &str {
        &self.ask
    }

    /// Get the question kind.
    pub fn kind(&self) -> &QuestionKind {
        &self.kind
    }

    /// Get a mutable reference to the question kind.
    pub fn kind_mut(&mut self) -> &mut QuestionKind {
        &mut self.kind
    }

    /// Get the default value.
    pub fn default(&self) -> &DefaultValue {
        &self.default
    }

    /// Set a suggested default value (user can modify).
    pub fn set_suggestion(&mut self, value: impl Into<ResponseValue>) {
        self.default = DefaultValue::Suggested(value.into());
    }

    /// Set an assumed value (question is skipped entirely).
    pub fn set_assumption(&mut self, value: impl Into<ResponseValue>) {
        self.default = DefaultValue::Assumed(value.into());
    }

    /// Clear any default value.
    pub fn clear_default(&mut self) {
        self.default = DefaultValue::None;
    }

    /// Check if this question should be skipped (has an assumed value).
    pub fn is_assumed(&self) -> bool {
        self.default.is_assumed()
    }
}

/// The kind of question, determining input type and structure.
#[derive(Debug, Clone, PartialEq)]
pub enum QuestionKind {
    /// No data to collect (unit enum variants, unit structs).
    Unit,

    /// Single-line text input.
    Input(InputQuestion),

    /// Multi-line text input (opens editor or textarea).
    Multiline(MultilineQuestion),

    /// Masked input for passwords.
    Masked(MaskedQuestion),

    /// Integer input with optional min/max bounds.
    Int(IntQuestion),

    /// Floating-point input with optional min/max bounds.
    Float(FloatQuestion),

    /// Yes/no confirmation.
    Confirm(ConfirmQuestion),

    /// List of values (Vec<T> where T is a primitive type).
    List(ListQuestion),

    /// Select any number of options from a list (Vec<Enum>).
    AnyOf(AnyOfQuestion),

    /// A group of questions — answer all (nested structs, struct variants).
    AllOf(AllOfQuestion),

    /// Choose one variant — pick one, then answer its questions (enums).
    OneOf(OneOfQuestion),
}

impl QuestionKind {
    /// Check if this is a Unit kind (no data to collect).
    pub fn is_unit(&self) -> bool {
        self == &Self::Unit
    }

    pub fn is_basic(&self) -> bool {
        matches!(
            self,
            Self::Input(_)
                | Self::Multiline(_)
                | Self::Masked(_)
                | Self::Int(_)
                | Self::Float(_)
                | Self::Confirm(_)
                | Self::List(_)
        )
    }

    /// Check if this is a structural kind (AllOf, OneOf, AnyOf).
    pub fn is_structural(&self) -> bool {
        matches!(self, Self::AllOf(_) | Self::OneOf(_) | Self::AnyOf(_))
    }
}

/// A variant in a OneOf question (enum variant).
#[derive(Debug, Clone, PartialEq)]
pub struct Variant {
    /// Variant name for display (e.g., "Male", "Female", "Other").
    pub name: String,

    /// What to collect for this variant.
    /// - Unit for unit variants (no data)
    /// - Input for newtype variants with String
    /// - AllOf for struct variants
    /// - OneOf for nested enums
    pub kind: QuestionKind,
}

impl Variant {
    /// Create a new variant with the given name and kind.
    pub fn new(name: impl Into<String>, kind: QuestionKind) -> Self {
        Self {
            name: name.into(),
            kind,
        }
    }

    /// Create a unit variant (no data to collect).
    pub fn unit(name: impl Into<String>) -> Self {
        Self::new(name, QuestionKind::Unit)
    }
}

/// Configuration for an AnyOf question (multi-select with potential follow-up questions).
#[derive(Debug, Clone, PartialEq)]
pub struct AnyOfQuestion {
    /// The available variants to choose from.
    pub variants: Vec<Variant>,

    /// Default selected indices (if any).
    pub defaults: Vec<usize>,
}

impl AnyOfQuestion {
    /// Create a new AnyOf question with the given variants.
    pub fn new(variants: Vec<Variant>) -> Self {
        Self {
            variants,
            defaults: Vec::new(),
        }
    }

    /// Create with default selections.
    pub fn with_defaults(variants: Vec<Variant>, defaults: Vec<usize>) -> Self {
        Self { variants, defaults }
    }
}

/// Configuration for an AllOf question (a group of questions that are all answered).
///
/// Used for nested structs and struct enum variants.
#[derive(Debug, Clone, PartialEq)]
pub struct AllOfQuestion {
    /// The questions in this group.
    pub questions: Vec<Question>,
}

impl AllOfQuestion {
    /// Create a new AllOf question with the given questions.
    pub fn new(questions: Vec<Question>) -> Self {
        Self { questions }
    }

    /// Create an empty AllOf question.
    pub fn empty() -> Self {
        Self {
            questions: Vec::new(),
        }
    }

    /// Get the questions.
    pub fn questions(&self) -> &[Question] {
        &self.questions
    }

    /// Get a mutable reference to the questions.
    pub fn questions_mut(&mut self) -> &mut Vec<Question> {
        &mut self.questions
    }
}

/// Configuration for a OneOf question (choose exactly one variant).
///
/// Used for enums where the user selects one variant, then answers
/// any follow-up questions for that variant.
#[derive(Debug, Clone, PartialEq)]
pub struct OneOfQuestion {
    /// The available variants to choose from.
    pub variants: Vec<Variant>,

    /// Default selected variant index (if any).
    pub default: Option<usize>,
}

impl OneOfQuestion {
    /// Create a new OneOf question with the given variants.
    pub fn new(variants: Vec<Variant>) -> Self {
        Self {
            variants,
            default: None,
        }
    }

    /// Create with a default selection.
    pub fn with_default(variants: Vec<Variant>, default: usize) -> Self {
        Self {
            variants,
            default: Some(default),
        }
    }

    /// Get the variants.
    pub fn variants(&self) -> &[Variant] {
        &self.variants
    }

    /// Get a mutable reference to the variants.
    pub fn variants_mut(&mut self) -> &mut Vec<Variant> {
        &mut self.variants
    }
}

/// Configuration for a text input question.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct InputQuestion {
    /// Optional default value.
    pub default: Option<String>,

    /// Validation function name (resolved at compile time).
    pub validate: Option<String>,
}

impl InputQuestion {
    /// Create a new input question.
    pub fn new() -> Self {
        Self::default()
    }

    /// Create with a default value.
    pub fn with_default(default: impl Into<String>) -> Self {
        Self {
            default: Some(default.into()),
            validate: None,
        }
    }

    /// Create with a validator.
    pub fn with_validator(validate: Option<String>) -> Self {
        Self {
            default: None,
            validate,
        }
    }
}

/// Configuration for a multi-line text editor question.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct MultilineQuestion {
    /// Optional default value.
    pub default: Option<String>,

    /// Validation function name.
    pub validate: Option<String>,
}

impl MultilineQuestion {
    /// Create a new multiline question.
    pub fn new() -> Self {
        Self::default()
    }

    /// Create with a validator.
    pub fn with_validator(validate: Option<String>) -> Self {
        Self {
            default: None,
            validate,
        }
    }
}

/// Configuration for a password/masked input question.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct MaskedQuestion {
    /// The masking character (default: '*').
    pub mask: Option<char>,

    /// Validation function name.
    pub validate: Option<String>,
}

impl MaskedQuestion {
    /// Create a new masked question.
    pub fn new() -> Self {
        Self::default()
    }

    /// Create with a custom mask character.
    pub fn with_mask(mask: char) -> Self {
        Self {
            mask: Some(mask),
            validate: None,
        }
    }

    /// Create with a validator.
    pub fn with_validator(validate: Option<String>) -> Self {
        Self {
            mask: None,
            validate,
        }
    }
}

/// Configuration for an integer input question.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct IntQuestion {
    /// Optional default value.
    pub default: Option<i64>,

    /// Optional minimum value.
    pub min: Option<i64>,

    /// Optional maximum value.
    pub max: Option<i64>,

    /// Validation function name.
    pub validate: Option<String>,
}

impl IntQuestion {
    /// Create a new integer question.
    pub fn new() -> Self {
        Self::default()
    }

    /// Create with bounds.
    pub fn with_bounds(min: Option<i64>, max: Option<i64>) -> Self {
        Self {
            default: None,
            min,
            max,
            validate: None,
        }
    }

    /// Create with bounds and a validator.
    pub fn with_bounds_and_validator(
        min: Option<i64>,
        max: Option<i64>,
        validate: Option<String>,
    ) -> Self {
        Self {
            default: None,
            min,
            max,
            validate,
        }
    }
}

/// Configuration for a floating-point input question.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct FloatQuestion {
    /// Optional default value.
    pub default: Option<f64>,

    /// Optional minimum value.
    pub min: Option<f64>,

    /// Optional maximum value.
    pub max: Option<f64>,

    /// Validation function name.
    pub validate: Option<String>,
}

impl FloatQuestion {
    /// Create a new float question.
    pub fn new() -> Self {
        Self::default()
    }

    /// Create with bounds.
    pub fn with_bounds(min: Option<f64>, max: Option<f64>) -> Self {
        Self {
            default: None,
            min,
            max,
            validate: None,
        }
    }

    /// Create with bounds and a validator.
    pub fn with_bounds_and_validator(
        min: Option<f64>,
        max: Option<f64>,
        validate: Option<String>,
    ) -> Self {
        Self {
            default: None,
            min,
            max,
            validate,
        }
    }
}

/// Configuration for a yes/no confirmation question.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct ConfirmQuestion {
    /// Default value (true for yes, false for no).
    pub default: bool,
}

impl ConfirmQuestion {
    /// Create a new confirm question with default false.
    pub fn new() -> Self {
        Self::default()
    }

    /// Create with a default value.
    pub fn with_default(default: bool) -> Self {
        Self { default }
    }
}

/// The type of elements in a list question.
#[derive(Debug, Clone, PartialEq, Default)]
pub enum ListElementKind {
    /// String elements.
    #[default]
    String,
    /// Integer elements with optional bounds.
    Int { min: Option<i64>, max: Option<i64> },
    /// Float elements with optional bounds.
    Float { min: Option<f64>, max: Option<f64> },
}

/// Configuration for a list input question (Vec<T>).
///
/// Allows collecting multiple values of the same type.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct ListQuestion {
    /// The type of elements in the list.
    pub element_kind: ListElementKind,

    /// Optional minimum number of elements.
    pub min_items: Option<usize>,

    /// Optional maximum number of elements.
    pub max_items: Option<usize>,

    /// Validation function name.
    pub validate: Option<String>,
}

impl ListQuestion {
    /// Create a new list question for strings.
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a list question for strings.
    pub fn strings() -> Self {
        Self {
            element_kind: ListElementKind::String,
            ..Default::default()
        }
    }

    /// Create a list question for integers.
    pub fn ints() -> Self {
        Self {
            element_kind: ListElementKind::Int {
                min: None,
                max: None,
            },
            ..Default::default()
        }
    }

    /// Create a list question for integers with bounds.
    pub fn ints_with_bounds(min: Option<i64>, max: Option<i64>) -> Self {
        Self {
            element_kind: ListElementKind::Int { min, max },
            ..Default::default()
        }
    }

    /// Create a list question for floats.
    pub fn floats() -> Self {
        Self {
            element_kind: ListElementKind::Float {
                min: None,
                max: None,
            },
            ..Default::default()
        }
    }

    /// Create a list question for floats with bounds.
    pub fn floats_with_bounds(min: Option<f64>, max: Option<f64>) -> Self {
        Self {
            element_kind: ListElementKind::Float { min, max },
            ..Default::default()
        }
    }

    /// Set item count constraints.
    pub fn with_item_bounds(mut self, min: Option<usize>, max: Option<usize>) -> Self {
        self.min_items = min;
        self.max_items = max;
        self
    }

    /// Set a validator function.
    pub fn with_validator(mut self, validate: impl Into<String>) -> Self {
        self.validate = Some(validate.into());
        self
    }
}

/// The key suffix used to store the selected enum variant index in responses.
/// For a field "method", the selection is stored at "method.selected_variant".
pub const SELECTED_VARIANT_KEY: &str = "selected_variant";

/// The key suffix used to store selected variant indices for AnyOf questions.
/// For a field "features", the selections are stored at "features.selected_variants".
pub const SELECTED_VARIANTS_KEY: &str = "selected_variants";