mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 04:06:45 +01:00
43 lines
1.6 KiB
Rust
43 lines
1.6 KiB
Rust
use crate::*;
|
|
|
|
/// Conditional rendering, in unary and binary forms.
|
|
pub struct Cond;
|
|
|
|
impl Cond {
|
|
/// Render `item` when `cond` is true.
|
|
pub fn when <E: Engine, A: Render<E>> (cond: bool, item: A) -> When<E, A> {
|
|
When(cond, item, Default::default())
|
|
}
|
|
/// Render `item` if `cond` is true, otherwise render `other`.
|
|
pub fn either <E: Engine, A: Render<E>, B: Render<E>> (cond: bool, item: A, other: B) -> Either<E, A, B> {
|
|
Either(cond, item, other, Default::default())
|
|
}
|
|
}
|
|
|
|
/// Renders `self.1` when `self.0` is true.
|
|
pub struct When<E: Engine, A: Render<E>>(bool, A, PhantomData<E>);
|
|
|
|
/// Renders `self.1` when `self.0` is true, otherwise renders `self.2`
|
|
pub struct Either<E: Engine, A: Render<E>, B: Render<E>>(bool, A, B, PhantomData<E>);
|
|
|
|
impl<E: Engine, A: Render<E>> Render<E> for When<E, A> {
|
|
fn min_size (&self, to: E::Size) -> Perhaps<E::Size> {
|
|
let Self(cond, item, ..) = self;
|
|
if *cond { item.min_size(to) } else { Ok(Some([0.into(), 0.into()].into())) }
|
|
}
|
|
fn render (&self, to: &mut E::Output) -> Usually<()> {
|
|
let Self(cond, item, ..) = self;
|
|
if *cond { item.render(to) } else { Ok(()) }
|
|
}
|
|
}
|
|
|
|
impl<E: Engine, A: Render<E>, B: Render<E>> Render<E> for Either<E, A, B> {
|
|
fn min_size (&self, to: E::Size) -> Perhaps<E::Size> {
|
|
let Self(cond, item, other, ..) = self;
|
|
if *cond { item.min_size(to) } else { other.min_size(to) }
|
|
}
|
|
fn render (&self, to: &mut E::Output) -> Usually<()> {
|
|
let Self(cond, item, other, ..) = self;
|
|
if *cond { item.render(to) } else { other.render(to) }
|
|
}
|
|
}
|