use super::{BoxedPlan, Declined, Plan, PlanTree};
pub struct InOrder<I, O> {
plans: Vec<BoxedPlan<I, O>>,
}
const NOTHING_LISTED: &str = "the list holds no plan";
impl<I, O> InOrder<I, O> {
pub fn new(plans: Vec<BoxedPlan<I, O>>) -> Self {
Self { plans }
}
}
impl<I, O> Plan<I> for InOrder<I, O> {
type Output = O;
fn launch(&self, inputs: &I) -> Result<O, Declined> {
let mut declined = None;
for plan in &self.plans {
match plan.launch(inputs) {
Ok(out) => return Ok(out),
Err(why) => declined = Some(why),
}
}
Err(declined.unwrap_or_else(|| Declined::by(self, NOTHING_LISTED)))
}
fn tree(&self) -> PlanTree {
PlanTree::InOrder(self.plans.iter().map(Plan::tree).collect())
}
}