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/style.rs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/library/style.rs (limited to 'src/library/style.rs') diff --git a/src/library/style.rs b/src/library/style.rs new file mode 100644 index 00000000..397375a4 --- /dev/null +++ b/src/library/style.rs @@ -0,0 +1,39 @@ +use crate::func::prelude::*; +use toddle::query::FontClass; + +macro_rules! stylefunc { + ($ident:ident) => { + /// Styles text. + #[derive(Debug, PartialEq)] + pub struct $ident { + body: Option + } + + function! { + data: $ident, + + parse(args, body, ctx) { + args.done()?; + Ok($ident { body: parse!(optional: body, ctx) }) + } + + layout(this, ctx) { + let mut new_style = ctx.style.clone(); + new_style.toggle_class(FontClass::$ident); + + Ok(match &this.body { + Some(body) => commands![ + Command::SetStyle(new_style), + Command::Layout(body), + Command::SetStyle(ctx.style.clone()), + ], + None => commands![Command::SetStyle(new_style)] + }) + } + } + }; +} + +stylefunc!(Italic); +stylefunc!(Bold); +stylefunc!(Monospace); -- cgit v1.2.3