summaryrefslogtreecommitdiff
path: root/src/library/spacing.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2020-08-04 13:48:07 +0200
committerLaurenz <laurmaedje@gmail.com>2020-08-04 13:48:07 +0200
commit2467cd6272c13b618ad53c5dadff5b8c8e7885bf (patch)
tree6ad13ec06a04997564efc514b40daa3fb65233e2 /src/library/spacing.rs
parented4fdcb0ada909f1cc3d7436334e253f0ec14d55 (diff)
Refactor function parsing ♻
Diffstat (limited to 'src/library/spacing.rs')
-rw-r--r--src/library/spacing.rs54
1 files changed, 36 insertions, 18 deletions
diff --git a/src/library/spacing.rs b/src/library/spacing.rs
index 6503556f..14c6135a 100644
--- a/src/library/spacing.rs
+++ b/src/library/spacing.rs
@@ -2,31 +2,49 @@ use crate::layout::SpacingKind;
use crate::length::ScaleLength;
use super::*;
-function! {
- /// `h` and `v`: Add horizontal or vertical spacing.
- #[derive(Debug, Clone, PartialEq)]
- pub struct SpacingFunc {
- spacing: Option<(SpecAxis, ScaleLength)>,
- }
+/// `h`: Add horizontal spacing.
+///
+/// # Positional arguments
+/// - The spacing (length or relative to font size).
+pub fn h(call: FuncCall, _: &ParseState) -> Pass<SyntaxNode> {
+ spacing(call, Horizontal)
+}
- type Meta = SpecAxis;
+/// `v`: Add vertical spacing.
+///
+/// # Positional arguments
+/// - The spacing (length or relative to font size).
+pub fn v(call: FuncCall, _: &ParseState) -> Pass<SyntaxNode> {
+ spacing(call, Vertical)
+}
- parse(header, body, state, f, meta) {
- expect_no_body(body, f);
- Self {
- spacing: header.args.pos.expect::<ScaleLength>(f)
- .map(|s| (meta, s))
- .or_missing(header.name.span, "spacing", f),
- }
- }
+fn spacing(call: FuncCall, axis: SpecAxis) -> Pass<SyntaxNode> {
+ let mut f = Feedback::new();
+ let mut args = call.header.args;
+ expect_no_body(call.body, &mut f);
+ let node = SpacingNode {
+ spacing: args.pos.expect::<ScaleLength>(&mut f)
+ .map(|s| (axis, s))
+ .or_missing(call.header.name.span, "spacing", &mut f),
+ };
+ drain_args(args, &mut f);
+ Pass::node(node, f)
+}
+
+#[derive(Debug, Clone, PartialEq)]
+struct SpacingNode {
+ spacing: Option<(SpecAxis, ScaleLength)>,
+}
- layout(self, ctx, f) {
- if let Some((axis, spacing)) = self.spacing {
+#[async_trait(?Send)]
+impl Layout for SpacingNode {
+ async fn layout<'a>(&'a self, ctx: LayoutContext<'_>) -> Pass<Commands<'a>> {
+ Pass::okay(if let Some((axis, spacing)) = self.spacing {
let axis = axis.to_generic(ctx.axes);
let spacing = spacing.raw_scaled(ctx.style.text.font_size());
vec![AddSpacing(spacing, SpacingKind::Hard, axis)]
} else {
vec![]
- }
+ })
}
}