diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-05-18 21:32:40 +0200 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2021-05-18 21:32:40 +0200 |
| commit | 118fc1014bcfc5585fd3ce32348fbfe14bdc05a9 (patch) | |
| tree | 51ef7937188776d0343196fda207c77e732859d1 /src/library/shapes.rs | |
| parent | 7025590405623b99ec93aa6eddc9fc909e8f83a8 (diff) | |
Leaner argument parsing
Diffstat (limited to 'src/library/shapes.rs')
| -rw-r--r-- | src/library/shapes.rs | 36 |
1 files changed, 18 insertions, 18 deletions
diff --git a/src/library/shapes.rs b/src/library/shapes.rs index d6cdba8e..4d588140 100644 --- a/src/library/shapes.rs +++ b/src/library/shapes.rs @@ -17,10 +17,10 @@ use crate::layout::{BackgroundNode, BackgroundShape, Fill, FixedNode, PadNode}; /// # Return value /// A template that inserts a rectangle and sets the body into it. pub fn rect(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value { - let width = args.get(ctx, "width"); - let height = args.get(ctx, "height"); - let fill = args.get(ctx, "fill"); - let body = args.find::<TemplateValue>(ctx).unwrap_or_default(); + let width = args.eat_named(ctx, "width"); + let height = args.eat_named(ctx, "height"); + let fill = args.eat_named(ctx, "fill"); + let body = args.eat::<TemplateValue>(ctx).unwrap_or_default(); rect_impl("rect", width, height, None, fill, body) } @@ -42,11 +42,11 @@ pub fn rect(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value { /// # Return value /// A template that inserts a square and sets the body into it. pub fn square(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value { - let length = args.get::<Length>(ctx, "length").map(Linear::from); - let width = length.or_else(|| args.get(ctx, "width")); - let height = if width.is_none() { args.get(ctx, "height") } else { None }; - let fill = args.get(ctx, "fill"); - let body = args.find::<TemplateValue>(ctx).unwrap_or_default(); + let length = args.eat_named::<Length>(ctx, "length").map(Linear::from); + let width = length.or_else(|| args.eat_named(ctx, "width")); + let height = width.is_none().then(|| args.eat_named(ctx, "height")).flatten(); + let fill = args.eat_named(ctx, "fill"); + let body = args.eat::<TemplateValue>(ctx).unwrap_or_default(); rect_impl("square", width, height, Some(1.0), fill, body) } @@ -89,10 +89,10 @@ fn rect_impl( /// # Return value /// A template that inserts an ellipse and sets the body into it. pub fn ellipse(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value { - let width = args.get(ctx, "width"); - let height = args.get(ctx, "height"); - let fill = args.get(ctx, "fill"); - let body = args.find::<TemplateValue>(ctx).unwrap_or_default(); + let width = args.eat_named(ctx, "width"); + let height = args.eat_named(ctx, "height"); + let fill = args.eat_named(ctx, "fill"); + let body = args.eat::<TemplateValue>(ctx).unwrap_or_default(); ellipse_impl("ellipse", width, height, None, fill, body) } @@ -114,11 +114,11 @@ pub fn ellipse(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value { /// # Return value /// A template that inserts a circle and sets the body into it. pub fn circle(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value { - let radius = args.get::<Length>(ctx, "radius").map(|r| 2.0 * Linear::from(r)); - let width = radius.or_else(|| args.get(ctx, "width")); - let height = if width.is_none() { args.get(ctx, "height") } else { None }; - let fill = args.get(ctx, "fill"); - let body = args.find::<TemplateValue>(ctx).unwrap_or_default(); + let radius = args.eat_named::<Length>(ctx, "radius").map(|r| 2.0 * Linear::from(r)); + let width = radius.or_else(|| args.eat_named(ctx, "width")); + let height = width.is_none().then(|| args.eat_named(ctx, "height")).flatten(); + let fill = args.eat_named(ctx, "fill"); + let body = args.eat::<TemplateValue>(ctx).unwrap_or_default(); ellipse_impl("circle", width, height, Some(1.0), fill, body) } |
