use crate::*; /// Show one item if a condition is true and another if the condition is false pub struct Either(pub PhantomData, pub bool, pub A, pub B); impl Either { pub fn new (c: bool, a: A, b: B) -> Self { Self(Default::default(), c, a, b) } } impl<'a, E: Output + 'a, T: EdnViewData<'a, E>> TryFromEdn<'a, T> for Either, RenderBox<'a, E>> { fn try_from_edn (state: &'a T, head: &EdnItem<&str>, tail: &'a [EdnItem<&str>]) -> Option { use EdnItem::*; if let (Key("either"), [condition, content, alternative]) = (head, tail) { Some(Self::new( state.get(condition).expect("either: no condition"), state.get(content).expect("either: no content"), state.get(alternative).expect("either: no alternative") )) } else { None } } } impl, B: Render> Content for Either { fn layout (&self, to: E::Area) -> E::Area { let Self(_, cond, a, b) = self; if *cond { a.layout(to) } else { b.layout(to) } } fn render (&self, to: &mut E) { let Self(_, cond, a, b) = self; if *cond { a.render(to) } else { b.render(to) } } }