From ecf0ff4d054f11c79ec0ddbbdf45f3dfcf9fc8d7 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Wed, 23 Oct 2019 00:14:43 +0200 Subject: =?UTF-8?q?Introduce=20a=20set=20of=20macros=20for=20writing=20fun?= =?UTF-8?q?ctions=20more=20concisely=20=F0=9F=8E=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/library/spacing.rs | 76 -------------------------------------------------- 1 file changed, 76 deletions(-) delete mode 100644 src/library/spacing.rs (limited to 'src/library/spacing.rs') diff --git a/src/library/spacing.rs b/src/library/spacing.rs deleted file mode 100644 index 91288edc..00000000 --- a/src/library/spacing.rs +++ /dev/null @@ -1,76 +0,0 @@ -use std::marker::PhantomData; -use super::prelude::*; -use crate::size::Size; - -/// Adds vertical space. -pub type VerticalSpaceFunc = SpaceFunc; - -/// Adds horizontal space. -pub type HorizontalSpaceFunc = SpaceFunc; - -/// Adds generic space. -#[derive(Debug, PartialEq)] -pub struct SpaceFunc { - spacing: Spacing, - _phantom: PhantomData, -} - -/// Absolute or font-relative spacing. -#[derive(Debug, PartialEq)] -enum Spacing { - Absolute(Size), - Relative(f32), -} - -impl Function for SpaceFunc { - fn parse(header: &FuncHeader, body: Option<&str>, _: ParseContext) -> ParseResult - where Self: Sized { - if header.args.len() != 1 || !header.kwargs.is_empty() { - return err("align: expected exactly one positional argument"); - } - - let spacing = match header.args[0] { - Expression::Size(s) => Spacing::Absolute(s), - Expression::Number(f) => Spacing::Relative(f as f32), - _ => return err("space: expected size or number"), - }; - - if body.is_some() { - return err("space: expected no body"); - } - - Ok(SpaceFunc { - spacing, - _phantom: PhantomData, - }) - } - - fn layout(&self, ctx: LayoutContext) -> LayoutResult { - let space = match self.spacing { - Spacing::Absolute(s) => s, - Spacing::Relative(f) => Size::pt(f * ctx.style.font_size), - }; - - Ok(commands![F::cmd(space)]) - } -} - -pub trait SpaceFlow: std::fmt::Debug + PartialEq + 'static { - fn cmd(space: Size) -> Command<'static>; -} - -#[derive(Debug, PartialEq)] -pub struct SpaceVertical; -impl SpaceFlow for SpaceVertical { - fn cmd(space: Size) -> Command<'static> { - Command::Add(Layout::empty(Size::zero(), space)) - } -} - -#[derive(Debug, PartialEq)] -pub struct SpaceHorizontal; -impl SpaceFlow for SpaceHorizontal { - fn cmd(space: Size) -> Command<'static> { - Command::AddFlex(Layout::empty(space, Size::zero())) - } -} -- cgit v1.2.3