tengri/output/src/ops/either.rs
2025-05-24 23:57:12 +03:00

32 lines
1.1 KiB
Rust

use crate::*;
/// Show one item if a condition is true and another if the condition is false
pub struct Either<A, B>(pub bool, pub A, pub B);
impl<A, B> Either<A, B> {
/// Create a ternary condition.
pub const fn new (c: bool, a: A, b: B) -> Self {
Self(c, a, b)
}
}
#[cfg(feature = "dsl")] take!(Either<A, B>, A, B|state, words|Ok(
if let Some(Token { value: Value::Key("either"), .. }) = words.peek() {
let base = words.clone();
let _ = words.next().unwrap();
return Ok(Some(Self(
state.give_or_fail(words, ||"either: no condition")?,
state.give_or_fail(words, ||"either: no content 1")?,
state.give_or_fail(words, ||"either: no content 2")?,
)))
} else {
None
}));
impl<E: Output, A: Render<E>, B: Render<E>> Content<E> for Either<A, B> {
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) }
}
}