mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 11:46:42 +01:00
32 lines
1.1 KiB
Rust
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) }
|
|
}
|
|
}
|