From 8a6eb19e279afefc126008c27b38cc66645e1c96 Mon Sep 17 00:00:00 2001 From: facile pop culture reference Date: Sat, 8 Aug 2026 23:37:06 +0300 Subject: [PATCH] reenable full/xy mod --- src/layout.rs | 86 +++++++++++++++++++++++++++++++++++++--------- src/layout/axis.rs | 41 ++++++++++------------ src/lib.rs | 31 ----------------- 3 files changed, 88 insertions(+), 70 deletions(-) diff --git a/src/layout.rs b/src/layout.rs index e2586fe..108f013 100644 --- a/src/layout.rs +++ b/src/layout.rs @@ -3,16 +3,6 @@ use crate::*; impl> Layout for T {} pub trait Layout: Draw + Sized { - fn full_w (self) -> impl Draw { - Full::W(self) - } - fn full_h (self) -> impl Draw { - Full::H(self) - } - fn full_wh (self) -> impl Draw { - Full::WH(self) - } - fn origin (self, azimuth: impl Into>) -> impl Draw { Origin(azimuth.into(), self) } @@ -98,7 +88,7 @@ impl Sizer { /// Some layout operations exist in multiple variants that take a single argument. /// Their handling in [eval_view] is uniform and goes like this: -macro_rules! eval_enum (( +#[macro_export] macro_rules! eval_enum (( $name:literal, $output:ident, $state:ident, $value:expr, $Enum:ident { $($v:literal => $V:ident),* $(,)? } @@ -111,7 +101,7 @@ macro_rules! eval_enum (( /// Some layout operations exist in XY, X, and Y variants that take 3 or 2 arguments. /// Their handling in [eval_view] is uniform and goes like this: -macro_rules! eval_xy ( +#[macro_export] macro_rules! eval_xy ( // Valueless variant: // (fill/x ...) // (fill/y ...) @@ -166,7 +156,36 @@ macro_rules! eval_xy ( }}; ); -macro_rules! def_layout_modifier { +#[macro_export] macro_rules! fn_kw_layout { + ($name:ident |$state:ident, $output: ident, $expr:ident| $body:block) => { + #[cfg(all(feature = "draw", feature = "eval"))] + pub fn $name ( + $state: &S, $output: &mut O, $expr: &str + ) -> Perhaps> where + S: Interpret>> + + for<'b> Namespace<'b, bool> + + for<'b> Namespace<'b, O::Unit> + + for<'b> Namespace<'b, Option> + $body + } +} + +#[macro_export] macro_rules! fn_kw_layout_tui { + ($name:ident |$state:ident, $output: ident, $expr:ident| $body:block) => { + #[cfg(all(feature = "draw", feature = "eval", feature = "term"))] + pub fn $name ( + $state: &S, $output: &mut Tui, $expr: &str + ) -> Perhaps> where + S: Interpret>> + + for<'b> Namespace<'b, bool> + + for<'b> Namespace<'b, u16> + + for<'b> Namespace<'b, Option> + + for<'b> Namespace<'b, Color> + $body + } +} + +#[macro_export] macro_rules! def_layout_modifier { ( $Trait:ident ($fn_x:ident $fn_y:ident $fn_xy:ident), $Struct:ident { $X:ident $Y:ident $XY:ident } $kw_name:ident $name:literal @@ -188,9 +207,7 @@ macro_rules! def_layout_modifier { } impl , X: Into> + Copy> Draw for $Struct { - fn draw (&$self, $to: &mut S) -> Perhaps> { - $body - } + fn draw (&$self, $to: &mut S) -> Perhaps> { $body } } fn_kw_layout!($kw_name |state, output, expr| { @@ -213,6 +230,43 @@ macro_rules! def_layout_modifier { } } +#[macro_export] macro_rules! def_layout_modifier_flat { + ( + $Trait:ident ($fn_x:ident $fn_y:ident $fn_xy:ident), + $Struct:ident { $X:ident $Y:ident $XY:ident } $kw_name:ident $name:literal + |$self:ident, $to:ident| $body:block + ) => { + impl> $Trait for T {} + + pub trait $Trait: Draw + Sized { + fn $fn_x (self) -> $Struct { $Struct::$X(self) } + fn $fn_y (self) -> $Struct { $Struct::$Y(self) } + fn $fn_xy (self) -> $Struct { $Struct::$XY(self) } + } + + pub enum $Struct> { + __(PhantomData), $X(I), $Y(I), $XY(I), + } + + impl > Draw for $Struct { + fn draw (&$self, $to: &mut S) -> Perhaps> { $body } + } + + fn_kw_layout!($kw_name |state, output, expr| { + let head = expr.head()?; + let mut frags = head.src()?.unwrap_or_default().split("/"); + Ok(matches!(frags.next(), Some($name)).then(||{ + let args = expr.tail(); + let arg0 = args.head(); + eval_xy!( + $name => expr, head, output, state, frags.next(), + $fn_xy, $fn_x, $fn_y, arg0, + ) + }).transpose()?.flatten()) + }); + } +} + mod area; pub use self::area::*; mod axis; pub use self::axis::*; mod azimuth; pub use self::azimuth::*; diff --git a/src/layout/axis.rs b/src/layout/axis.rs index bf9169f..28002d5 100644 --- a/src/layout/axis.rs +++ b/src/layout/axis.rs @@ -1,28 +1,23 @@ use crate::*; -/// Use whole drawing area along one or both axes. -pub enum Full> { - __(PhantomData), - W(I), - H(I), - WH(I), -} - -impl_draw!(,>|self: Full, to: T|{ - let XYWH(x0, y0, w0, h0) = to.area(); - let item = match self { - Self::W(i) => i, Self::H(i) => i, Self::WH(i) => i, _ => unreachable!() - }; - let (x1, y1, w1, h1) = match self { - Self::W(..) => (Some(x0), None, Some(w0), None), - Self::H(..) => (None, Some(y0), None, Some(h0)), - Self::WH(..) => (Some(x0), Some(y0), Some(w0), Some(h0)), - _ => unreachable!() - }; - Ok(to.draw(to.area(), item)?.map(|XYWH(x2, y2, w2, h2)|XYWH( - x1.unwrap_or(x2), y1.unwrap_or(y2), w1.unwrap_or(w2), h1.unwrap_or(h2), - ))) -}); +def_layout_modifier_flat!( + LayoutFull (full_w full_h full_wh), + Full { W H WH } kw_full "full" |self, to| { + let XYWH(x0, y0, w0, h0) = to.area(); + let item = match self { + Self::W(i) => i, Self::H(i) => i, Self::WH(i) => i, _ => unreachable!() + }; + let (x1, y1, w1, h1) = match self { + Self::W(..) => (Some(x0), None, Some(w0), None), + Self::H(..) => (None, Some(y0), None, Some(h0)), + Self::WH(..) => (Some(x0), Some(y0), Some(w0), Some(h0)), + _ => unreachable!() + }; + Ok(to.draw(to.area(), item)?.map(|XYWH(x2, y2, w2, h2)|XYWH( + x1.unwrap_or(x2), y1.unwrap_or(y2), w1.unwrap_or(w2), h1.unwrap_or(h2), + ))) + } +); #[cfg(test)] #[test] fn test_layout_full () -> Usually<()> { assert_eq!(check_layout("1")?, Some(XYWH(1u16, 1, 1, 1))); diff --git a/src/lib.rs b/src/lib.rs index 814adac..494a73b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -29,37 +29,6 @@ pub(crate) use ::{ std::marker::PhantomData }; -#[cfg(all(feature = "draw", feature = "eval"))] -macro_rules! fn_kw_layout { - ($name:ident |$state:ident, $output: ident, $expr:ident| $body:block) => { - #[cfg(all(feature = "draw", feature = "eval"))] - pub fn $name ( - $state: &S, $output: &mut O, $expr: &str - ) -> Perhaps> where - S: Interpret>> - + for<'b> Namespace<'b, bool> - + for<'b> Namespace<'b, O::Unit> - + for<'b> Namespace<'b, Option> - $body - } -} - -#[cfg(all(feature = "draw", feature = "eval", feature = "term"))] -macro_rules! fn_kw_layout_tui { - ($name:ident |$state:ident, $output: ident, $expr:ident| $body:block) => { - #[cfg(all(feature = "draw", feature = "eval", feature = "term"))] - pub fn $name ( - $state: &S, $output: &mut Tui, $expr: &str - ) -> Perhaps> where - S: Interpret>> - + for<'b> Namespace<'b, bool> - + for<'b> Namespace<'b, u16> - + for<'b> Namespace<'b, Option> - + for<'b> Namespace<'b, Color> - $body - } -} - #[cfg(feature = "lang")] pub use ::dizzle::{Usually, Perhaps}; #[cfg(feature = "lang")] use ::dizzle::*;