diff options
| author | Laurenz <laurmaedje@gmail.com> | 2019-10-17 10:12:34 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2019-10-17 10:12:34 +0200 |
| commit | f22f9513aea21408ebf6febd01912e630e9ad5e6 (patch) | |
| tree | 06885bca8bc31d26189f33c059649ed7909af282 /src/library/styles.rs | |
| parent | 9a1d57a11a510b8e6af024b4338ee58d791f3088 (diff) | |
Add pagebreak function ⏭
Diffstat (limited to 'src/library/styles.rs')
| -rw-r--r-- | src/library/styles.rs | 36 |
1 files changed, 19 insertions, 17 deletions
diff --git a/src/library/styles.rs b/src/library/styles.rs index f0e5bbdb..bc84ac3b 100644 --- a/src/library/styles.rs +++ b/src/library/styles.rs @@ -10,35 +10,37 @@ macro_rules! style_func { ) => { $(#[$outer])* #[derive(Debug, PartialEq)] - pub struct $struct { body: SyntaxTree } + pub struct $struct { + body: Option<SyntaxTree> + } impl Function for $struct { fn parse(header: &FuncHeader, body: Option<&str>, ctx: ParseContext) -> ParseResult<Self> where Self: Sized { // Accept only invocations without arguments and with body. - if header.args.is_empty() && header.kwargs.is_empty() { - if let Some(body) = body { - Ok($struct { body: parse(body, ctx)? }) - } else { - Err(ParseError::new(format!("expected body for function `{}`", $name))) - } - } else { - Err(ParseError::new(format!("unexpected arguments to function `{}`", $name))) + if has_arguments(header) { + return err(format!("{}: expected no arguments", $name)); } + + let body = parse_maybe_body(body, ctx)?; + + Ok($struct { body }) } fn layout(&self, ctx: LayoutContext) -> LayoutResult<CommandList> { - let mut commands = CommandList::new(); - - let saved_style = ctx.style.clone(); let mut new_style = ctx.style.clone(); new_style.toggle_class(FontClass::$class); - commands.add(Command::SetStyle(new_style)); - commands.add(Command::Layout(&self.body)); - commands.add(Command::SetStyle(saved_style)); - - Ok(commands) + if let Some(body) = &self.body { + let saved_style = ctx.style.clone(); + Ok(commands![ + Command::SetStyle(new_style), + Command::Layout(body), + Command::SetStyle(saved_style), + ]) + } else { + Ok(commands![Command::SetStyle(new_style)]) + } } } }; |
