dsl: give! and take! macros
Some checks are pending
/ build (push) Waiting to run

This commit is contained in:
🪞👃🪞 2025-05-24 00:29:50 +03:00
parent cbd28a5934
commit 5a2177cc77
4 changed files with 131 additions and 94 deletions

View file

@ -27,45 +27,36 @@
//! test(area, &Align::sw(two_by_four()), [10, 28, 4, 2]);
//! test(area, &Align::w(two_by_four()), [10, 19, 4, 2]);
//! ```
use crate::*;
#[derive(Debug, Copy, Clone, Default)]
pub enum Alignment { #[default] Center, X, Y, NW, N, NE, E, SE, S, SW, W }
pub struct Align<A>(Alignment, A);
#[cfg(feature = "dsl")]
impl<'n, State: Give<A>, A: 'n> Take<'n, State> for Align<A> {
fn take <'source> (state: &State, words: &mut TokenIter<'source>) -> Perhaps<Self> {
if let Some(Token { value: Value::Key(key), .. }) = words.peek() {
#[cfg(feature = "dsl")]take!(Align<A>, A|state: Give<A>, words|Ok(Some(match words.peek() {
Some(Token { value: Value::Key(key), .. }) => match key {
"align/c"|"align/x"|"align/y"|
"align/n"|"align/s"|"align/e"|"al;qign/w"|
"align/nw"|"align/sw"|"align/ne"|"align/se" => {
let _ = words.next().unwrap();
let content = state.give_or_fail(&mut words.clone(), ||"expected content")?;
match key {
"align/c"|"align/x"|"align/y"|
"align/n"|"align/s"|"align/e"|"align/w"|
"align/nw"|"align/sw"|"align/ne"|"align/se" => {
let _ = words.next().unwrap();
let content = state.give_or_fail(&mut words.clone(), ||"expected content")?;
return Ok(Some(match key {
"align/c" => Self::c(content),
"align/x" => Self::x(content),
"align/y" => Self::y(content),
"align/n" => Self::n(content),
"align/s" => Self::s(content),
"align/e" => Self::e(content),
"align/w" => Self::w(content),
"align/nw" => Self::nw(content),
"align/ne" => Self::ne(content),
"align/sw" => Self::sw(content),
"align/se" => Self::se(content),
_ => unreachable!()
}))
},
_ => {}
"align/c" => Self::c(content),
"align/x" => Self::x(content),
"align/y" => Self::y(content),
"align/n" => Self::n(content),
"align/s" => Self::s(content),
"align/e" => Self::e(content),
"align/w" => Self::w(content),
"align/nw" => Self::nw(content),
"align/ne" => Self::ne(content),
"align/sw" => Self::sw(content),
"align/se" => Self::se(content),
_ => unreachable!()
}
}
Ok(None)
}
}
},
_ => return Ok(None)
},
_ => return Ok(None)
})));
impl<A> Align<A> {
#[inline] pub const fn c (a: A) -> Self { Self(Alignment::Center, a) }