#![allow(missing_docs)]
use super::error::{PromptError, Result};
use std::collections::BTreeMap;
type Answers = BTreeMap<String, String>;
type StepFn = Box<dyn FnOnce(&Answers) -> Result<String>>;
pub struct GroupBuilder {
steps: Vec<(String, StepFn)>,
}
pub fn group() -> GroupBuilder {
GroupBuilder { steps: Vec::new() }
}
impl GroupBuilder {
pub fn step<F>(mut self, name: impl Into<String>, f: F) -> Self
where
F: FnOnce(&Answers) -> Result<String> + 'static,
{
self.steps.push((name.into(), Box::new(f)));
self
}
pub fn run(self) -> Result<Answers> {
let mut answers = Answers::new();
for (name, step) in self.steps {
match step(&answers) {
Ok(v) => {
answers.insert(name, v);
}
Err(PromptError::Interrupted) => {
super::cancel("Cancelled");
return Err(PromptError::Interrupted);
}
Err(e) => return Err(e),
}
}
Ok(answers)
}
}