diff options
| author | Laurenz <laurmaedje@gmail.com> | 2019-10-10 23:36:17 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2019-10-10 23:38:03 +0200 |
| commit | 8f788f9a4f5e970bbe6147987b711470d57aca8d (patch) | |
| tree | cc89008dfdfc62ecf4eb2517d92ec7c7095fa573 /src/library/align.rs | |
| parent | 61470fba68e9f19b481034427add5f3d8cfbc0a8 (diff) | |
Add standard `align` function and support right-alignment ➡️
Diffstat (limited to 'src/library/align.rs')
| -rw-r--r-- | src/library/align.rs | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/library/align.rs b/src/library/align.rs new file mode 100644 index 00000000..4b2ee8c3 --- /dev/null +++ b/src/library/align.rs @@ -0,0 +1,51 @@ +//! Alignment function. + +use super::prelude::*; +use crate::layout::Alignment; + + +/// Allows to align content in different ways. +#[derive(Debug, PartialEq)] +pub struct AlignFunc { + alignment: Alignment, + body: Option<SyntaxTree>, +} + +impl Function for AlignFunc { + fn parse(header: &FuncHeader, body: Option<&str>, ctx: ParseContext) + -> ParseResult<Self> where Self: Sized { + + if header.args.len() != 1 || !header.kwargs.is_empty() { + return err("expected exactly one positional argument specifying the alignment"); + } + + let alignment = if let Expression::Ident(ident) = &header.args[0] { + match ident.as_str() { + "left" => Alignment::Left, + "right" => Alignment::Right, + s => return err(format!("invalid alignment specifier: '{}'", s)), + } + } else { + return err(format!("expected alignment specifier, found: '{}'", header.args[0])); + }; + + let body = if let Some(body) = body { + Some(parse(body, ctx)?) + } else { + None + }; + + Ok(AlignFunc { alignment, body }) + } + + fn layout(&self, mut ctx: LayoutContext) -> LayoutResult<Option<Layout>> { + if let Some(body) = &self.body { + // Override the previous alignment and do the layouting. + ctx.space.alignment = self.alignment; + layout(body, ctx) + .map(|l| Some(Layout::Boxed(l))) + } else { + unimplemented!("context-modifying align func") + } + } +} |
