ibuilder 0.1.8

Interactive builder for Rust types
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
//! Module with the implementors of `BuildableValue` for the various standard types.

use std::any::Any;
use std::marker::PhantomData;
use std::path::PathBuf;
use std::str::FromStr;

use crate::nodes::{Field, FieldKind, Node};
use crate::{
    BuildableValue, BuildableValueConfig, Choice, ChooseError, Input, NewBuildableValue, Options,
};

macro_rules! type_builder_boilerplate {
    (normal) => {
        fn get_subfields(&self, _: &[String]) -> Vec<String> {
            vec![]
        }

        fn to_node(&self) -> Node {
            if let Some(value) = &self.value {
                Node::Leaf(Field::String(value.to_string()))
            } else {
                Node::Leaf(Field::Missing)
            }
        }
    };
    (path) => {
        fn get_subfields(&self, _: &[String]) -> Vec<String> {
            vec![]
        }

        fn to_node(&self) -> Node {
            if let Some(value) = &self.value {
                Node::Leaf(Field::String(
                    value.as_os_str().to_string_lossy().to_string(),
                ))
            } else {
                Node::Leaf(Field::Missing)
            }
        }
    };
}

macro_rules! type_builder_struct {
    ($base:ty, $name:ident, $query:expr) => {
        type_builder_struct!(
            $base,
            $name,
            $query,
            concat!("Builder for the type `", stringify!($base), "`")
        );
    };
    ($base:ty, $name:ident, $query:expr, $docstring:expr) => {
        #[doc = $docstring]
        #[derive(Debug)]
        pub struct $name {
            /// The current value.
            pub value: Option<$base>,
            /// The message to show to the user.
            pub prompt: String,
        }

        impl $name {
            /// Make a new instance of the builder.
            pub fn new(config: BuildableValueConfig<$base>) -> Self {
                Self {
                    value: config.default,
                    prompt: config.prompt.unwrap_or_else(|| $query.to_string()),
                }
            }
        }
    };
}

macro_rules! type_builder {
    ($base:ty, $name:ident, $query:expr) => {
        type_builder!(
            $base,
            $name,
            $query,
            normal
        );
    };
    ($base:ty, $name:ident, $query:expr, $variant:tt) => {
        type_builder!(
            @,
            $base,
            $name,
            $query,
            concat!("Builder for the type `", stringify!($base), "`"),
            $variant
        );
    };
    (@, $base:ty, $name:ident, $query:expr, $docstring:expr, $variant:tt) => {
        type_builder_struct!($base, $name, $query, $docstring);

        impl BuildableValue for $name {
            type_builder_boilerplate!($variant);

            fn apply(&mut self, data: Input, current_fields: &[String]) -> Result<(), ChooseError> {
                if !current_fields.is_empty() {
                    panic!(
                        "{}.apply() called with non empty fields: {:?}",
                        stringify!($name),
                        current_fields
                    );
                }
                match data {
                    Input::Text(data) => {
                        self.value = Some(<$base>::from_str(&data).map_err(|e| {
                            ChooseError::InvalidText {
                                error: e.to_string(),
                            }
                        })?);
                    }
                    _ => return Err(ChooseError::UnexpectedChoice),
                }
                Ok(())
            }

            fn get_options(&self, current_fields: &[String]) -> Options {
                if !current_fields.is_empty() {
                    panic!(
                        "{}.get_options() called with non empty fields: {:?}",
                        stringify!($name),
                        current_fields
                    );
                }
                Options {
                    query: self.prompt.clone(),
                    text_input: true,
                    choices: vec![],
                }
            }

            fn get_value_any(&self) -> Option<Box<dyn Any>> {
                self.value.clone().map(|x| Box::new(x) as Box<dyn Any>)
            }
        }

        impl NewBuildableValue for $base {
            fn new_buildable_value(config: BuildableValueConfig<()>) -> Box<dyn BuildableValue> {
                Box::new($name::new(BuildableValueConfig {
                    default: None,
                    prompt: config.prompt,
                }))
            }
        }
    };
}

type_builder!(i8, I8Builder, "Type an integer");
type_builder!(i16, I16Builder, "Type an integer");
type_builder!(i32, I32Builder, "Type an integer");
type_builder!(i64, I64Builder, "Type an integer");
type_builder!(u8, U8Builder, "Type an integer");
type_builder!(u16, U16Builder, "Type an integer");
type_builder!(u32, U32Builder, "Type an integer");
type_builder!(u64, U64Builder, "Type an integer");
type_builder!(isize, IsizeBuilder, "Type an integer");
type_builder!(usize, UsizeBuilder, "Type an integer");
type_builder!(f32, F32Builder, "Type an integer");
type_builder!(f64, F64Builder, "Type an integer");
type_builder!(String, StringBuilder, "Type a string");
type_builder!(char, CharBuilder, "Type a char");
type_builder!(PathBuf, PathBufBuilder, "Type a path", path);

type_builder_struct!(bool, BoolBuilder, "True or false?");

impl BuildableValue for BoolBuilder {
    type_builder_boilerplate!(normal);

    fn apply(&mut self, data: Input, current_fields: &[String]) -> Result<(), ChooseError> {
        if !current_fields.is_empty() {
            panic!(
                "BoolBuilder.apply() called with non empty fields: {:?}",
                current_fields
            );
        }
        match data {
            Input::Choice(data) => match data.as_str() {
                "true" => self.value = Some(true),
                "false" => self.value = Some(false),
                _ => return Err(ChooseError::UnexpectedChoice),
            },
            Input::Text(_) => return Err(ChooseError::UnexpectedText),
        }
        Ok(())
    }

    fn get_options(&self, current_fields: &[String]) -> Options {
        if !current_fields.is_empty() {
            panic!(
                "BoolBuilder.get_options() called with non empty fields: {:?}",
                current_fields
            );
        }
        Options {
            query: self.prompt.clone(),
            text_input: false,
            choices: vec![
                Choice {
                    choice_id: "true".to_string(),
                    text: "true".to_string(),
                    needs_action: false,
                },
                Choice {
                    choice_id: "false".to_string(),
                    text: "false".to_string(),
                    needs_action: false,
                },
            ],
        }
    }

    fn get_value_any(&self) -> Option<Box<dyn Any>> {
        self.value.map(|x| Box::new(x) as Box<dyn Any>)
    }
}

/// Builder for the type `Vec<T>`.
///
/// The type parameters are:
/// - `B`: the type of the builder that produces `T`
/// - `T`: the type of the items of the final `Vec`
///
/// The state machine that this builder implements is a bit complex since it has to handle
/// insertions, deletions and updates and it looks like this:
///
/// ```text
///                 |
///                 v
///            +-------------+    __new    +-------------+  <B> specific
///  +-------> |  empty      | ----------> | not empty   | ------>>>
///  |         |  main       |             | edit        |
///  |         +-------------+             +-------------+
///  |                                         ^    |
///  |                                         |    |
///  |         +-------------+  index / __new  |    | __back
///  +-------> |  not empty  | ----------------+    |
///  |         |  main       | <--------------------+
///  | index   +-------------+
///  |            ^    |
///  |     __back |    | __remove
///  |            |    v
///  |         +-------------+
///  +-------- |  not empty  |
///            |  remove     |
///            +-------------+
/// ```
///
/// When `__new` is applied a new item is pushed at the back of the `Vec` and when `__new` is to
/// be considered as an index it refers to the last element of the `Vec`.
pub struct VecBuilder<T>
where
    T: NewBuildableValue + 'static,
{
    items: Vec<Box<dyn BuildableValue>>,
    inner_type: PhantomData<T>,
    prompt: String,
}

impl<T> std::fmt::Debug for VecBuilder<T>
where
    T: NewBuildableValue + 'static,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("VecBuilder")
            .field("items", &self.items)
            .finish()
    }
}

impl<T> NewBuildableValue for Vec<T>
where
    T: NewBuildableValue + 'static,
{
    fn new_buildable_value(config: BuildableValueConfig<()>) -> Box<dyn BuildableValue> {
        Box::new(VecBuilder::<T> {
            items: Vec::new(),
            inner_type: Default::default(),
            prompt: config
                .prompt
                .unwrap_or_else(|| "Select an action".to_string()),
        })
    }
}

impl<T> BuildableValue for VecBuilder<T>
where
    T: NewBuildableValue + 'static,
{
    fn apply(&mut self, data: Input, current_fields: &[String]) -> Result<(), ChooseError> {
        // vec main menu
        if current_fields.is_empty() {
            match data {
                Input::Choice(data) if data == "__new" => {
                    self.items.push(T::new_buildable_value(Default::default()));
                }
                Input::Choice(data) => {
                    if data != "__remove" {
                        // check that the inserted index is valid
                        let index =
                            usize::from_str(&data).map_err(|_| ChooseError::UnexpectedChoice)?;
                        if index >= self.items.len() {
                            return Err(ChooseError::UnexpectedChoice);
                        }
                    }
                }
                _ => return Err(ChooseError::UnexpectedText),
            }
        // remove item or apply to element
        } else {
            let field = &current_fields[0];
            let rest = &current_fields[1..];
            match field.as_str() {
                "__remove" => match data {
                    Input::Choice(choice) => {
                        let index =
                            usize::from_str(&choice).map_err(|_| ChooseError::UnexpectedChoice)?;
                        if index >= self.items.len() {
                            return Err(ChooseError::UnexpectedChoice);
                        }
                        self.items.remove(index);
                    }
                    Input::Text(_) => return Err(ChooseError::UnexpectedText),
                },
                "__new" => {
                    self.items
                        .last_mut()
                        .expect("Vec __new didn't push")
                        .apply(data, rest)?;
                }
                index => {
                    let index = usize::from_str(index)
                        .unwrap_or_else(|_| panic!("Invalid index for vec: {}", index));
                    self.items[index].apply(data, rest)?;
                }
            }
        }
        Ok(())
    }

    fn get_options(&self, current_fields: &[String]) -> Options {
        // vec main manu
        if current_fields.is_empty() {
            let mut choices = vec![Choice {
                choice_id: "__new".to_string(),
                text: "New element".to_string(),
                needs_action: false,
            }];
            if !self.items.is_empty() {
                choices.push(Choice {
                    choice_id: "__remove".to_string(),
                    text: "Remove element".to_string(),
                    needs_action: false,
                });
                for i in 0..self.items.len() {
                    choices.push(Choice {
                        choice_id: i.to_string(),
                        text: format!("Edit item {}", i),
                        needs_action: self.items[i].get_value_any().is_none(),
                    });
                }
            }
            Options {
                query: self.prompt.clone(),
                text_input: false,
                choices,
            }
        // item menu
        } else {
            let field = &current_fields[0];
            let rest = &current_fields[1..];
            match field.as_str() {
                // select the item to remove
                "__remove" => {
                    let mut choices = Vec::new();
                    for i in 0..self.items.len() {
                        choices.push(Choice {
                            choice_id: i.to_string(),
                            text: format!("Remove item {}", i),
                            needs_action: false,
                        });
                    }
                    Options {
                        query: "Select the item to remove".to_string(),
                        text_input: false,
                        choices,
                    }
                }
                // last action was __new, now inside the last item menu
                "__new" => self
                    .items
                    .last()
                    .expect("Vec __new didn't push")
                    .get_options(rest),
                // edit one of the items
                index => {
                    let index = usize::from_str(index)
                        .unwrap_or_else(|_| panic!("Invalid index for vec: {}", index));
                    self.items[index].get_options(rest)
                }
            }
        }
    }

    fn get_subfields(&self, current_fields: &[String]) -> Vec<String> {
        // main manu
        if current_fields.is_empty() {
            if self.items.is_empty() {
                vec!["__new".into()]
            } else {
                let mut res = vec!["__new".into(), "__remove".into()];
                for i in 0..self.items.len() {
                    res.push(i.to_string());
                }
                res
            }
        } else {
            let field = &current_fields[0];
            let rest = &current_fields[1..];
            match field.as_str() {
                // just select the item to remove
                "__remove" => vec![],
                "__new" => self
                    .items
                    .last()
                    .expect("Vec __new didn't push")
                    .get_subfields(rest),
                index => {
                    let index = usize::from_str(index)
                        .unwrap_or_else(|_| panic!("Invalid index for vec: {}", index));
                    self.items[index].get_subfields(rest)
                }
            }
        }
    }

    fn to_node(&self) -> Node {
        let items = self
            .items
            .iter()
            .map(|i| FieldKind::Unnamed(i.to_node()))
            .collect();
        // Vec has no name
        Node::Composite("".into(), items)
    }

    fn get_value_any(&self) -> Option<Box<dyn Any>> {
        let mut results: Vec<T> = Vec::with_capacity(self.items.len());
        for item in &self.items {
            results.push(*item.get_value_any()?.downcast::<T>().unwrap());
        }
        Some(Box::new(results))
    }
}

/// Builder for the type `Box<T>`.
pub struct BoxBuilder<T>
where
    T: NewBuildableValue + 'static,
{
    value: Box<dyn BuildableValue>,
    inner_type: PhantomData<T>,
}

impl<T> std::fmt::Debug for BoxBuilder<T>
where
    T: NewBuildableValue + 'static,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("BoxBuilder")
            .field("value", &self.value)
            .finish()
    }
}

impl<T> NewBuildableValue for Box<T>
where
    T: NewBuildableValue + 'static,
{
    fn new_buildable_value(config: BuildableValueConfig<()>) -> Box<dyn BuildableValue> {
        Box::new(BoxBuilder::<T> {
            value: T::new_buildable_value(config),
            inner_type: Default::default(),
        })
    }
}

impl<T> BuildableValue for BoxBuilder<T>
where
    T: NewBuildableValue + 'static,
{
    fn apply(&mut self, data: Input, current_fields: &[String]) -> Result<(), ChooseError> {
        self.value.apply(data, current_fields)
    }

    fn get_options(&self, current_fields: &[String]) -> Options {
        self.value.get_options(current_fields)
    }

    fn get_subfields(&self, current_fields: &[String]) -> Vec<String> {
        self.value.get_subfields(current_fields)
    }

    fn to_node(&self) -> Node {
        self.value.to_node()
    }

    fn get_value_any(&self) -> Option<Box<dyn Any>> {
        Some(Box::new(Box::new(
            *self.value.get_value_any()?.downcast::<T>().unwrap(),
        )))
    }
}

/// Builder for the type `Option<T>`.
pub struct OptionBuilder<T>
where
    T: NewBuildableValue + 'static,
{
    value: Option<Box<dyn BuildableValue>>,
    inner_type: PhantomData<T>,
    prompt: String,
}

impl<T> std::fmt::Debug for OptionBuilder<T>
where
    T: NewBuildableValue + 'static,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("OptionBuilder")
            .field("value", &self.value)
            .finish()
    }
}

impl<T> NewBuildableValue for Option<T>
where
    T: NewBuildableValue + 'static,
{
    fn new_buildable_value(config: BuildableValueConfig<()>) -> Box<dyn BuildableValue> {
        Box::new(OptionBuilder::<T> {
            value: None,
            inner_type: Default::default(),
            prompt: config
                .prompt
                .unwrap_or_else(|| "Choose an option".to_string()),
        })
    }
}

impl<T> BuildableValue for OptionBuilder<T>
where
    T: NewBuildableValue + 'static,
{
    fn apply(&mut self, data: Input, current_fields: &[String]) -> Result<(), ChooseError> {
        if current_fields.is_empty() {
            match data {
                Input::Choice(data) => match data.as_str() {
                    "__remove" => self.value = None,
                    "__edit" => {}
                    "__set" => self.value = Some(T::new_buildable_value(Default::default())),
                    _ => return Err(ChooseError::UnexpectedChoice),
                },
                Input::Text(_) => return Err(ChooseError::UnexpectedText),
            }
            Ok(())
        } else {
            let field = &current_fields[0];
            let rest = &current_fields[1..];
            if field == "__edit" || field == "__set" {
                self.value.as_mut().unwrap().apply(data, rest)
            } else {
                unreachable!("Unexpected field: {}", field);
            }
        }
    }

    fn get_options(&self, current_fields: &[String]) -> Options {
        if current_fields.is_empty() {
            let choices = match self.value {
                Some(_) => vec![
                    Choice {
                        choice_id: "__remove".to_string(),
                        text: "Remove value".to_string(),
                        needs_action: false,
                    },
                    Choice {
                        choice_id: "__edit".to_string(),
                        text: "Edit value".to_string(),
                        needs_action: false,
                    },
                ],
                None => vec![Choice {
                    choice_id: "__set".to_string(),
                    text: "Set value".to_string(),
                    needs_action: false,
                }],
            };
            Options {
                query: self.prompt.clone(),
                text_input: false,
                choices,
            }
        } else {
            let field = &current_fields[0];
            let rest = &current_fields[1..];
            if field == "__edit" || field == "__set" {
                self.value.as_ref().unwrap().get_options(rest)
            } else {
                unreachable!("Unexpected field: {}", field);
            }
        }
    }

    fn get_subfields(&self, current_fields: &[String]) -> Vec<String> {
        if current_fields.is_empty() {
            match self.value {
                Some(_) => vec!["__edit".to_string()],
                None => vec!["__set".to_string()],
            }
        } else {
            let field = &current_fields[0];
            let rest = &current_fields[1..];
            if field == "__edit" || field == "__set" {
                self.value.as_ref().unwrap().get_subfields(rest)
            } else {
                unreachable!("Unexpected field: {}", field);
            }
        }
    }

    fn to_node(&self) -> Node {
        match &self.value {
            Some(inner) => inner.to_node(),
            None => Node::Leaf(Field::String("None".into())),
        }
    }

    fn get_value_any(&self) -> Option<Box<dyn Any>> {
        match &self.value {
            Some(inner) => Some(Box::new(Some(
                *inner.get_value_any()?.downcast::<T>().unwrap(),
            ))),
            None => Some(Box::new(None::<T>)),
        }
    }
}