Parses a variable-length sequence of X/Y coordinate pairs.
(scope: &Scope<'_>)
| 75 | |
| 76 | /// Parses a variable-length sequence of X/Y coordinate pairs. |
| 77 | fn parse_polygon_coordinates(scope: &Scope<'_>) -> CallResult<Vec<PixelsXY>> { |
| 78 | match scope.nargs() { |
| 79 | 0 => return Ok(vec![]), |
| 80 | 1 => return parse_polygon_coordinates_from_array(scope), |
| 81 | _ => (), |
| 82 | } |
| 83 | |
| 84 | if !scope.nargs().is_multiple_of(2) { |
| 85 | let narg = u8::try_from(scope.nargs() - 1).expect("Argument index must fit in u8"); |
| 86 | return Err(CallError::Syntax( |
| 87 | scope.get_pos(narg), |
| 88 | "Expected an even number of coordinates".to_owned(), |
| 89 | )); |
| 90 | } |
| 91 | |
| 92 | let mut points = Vec::with_capacity(scope.nargs() / 2); |
| 93 | for i in (0..scope.nargs()).step_by(2) { |
| 94 | let xarg = u8::try_from(i).expect("Argument index must fit in u8"); |
| 95 | let yarg = u8::try_from(i + 1).expect("Argument index must fit in u8"); |
| 96 | points.push(parse_coordinates(scope, xarg, yarg)?); |
| 97 | } |
| 98 | Ok(points) |
| 99 | } |
| 100 | |
| 101 | /// Parses an array reference that contains a variable-length sequence of X/Y coordinate pairs. |
| 102 | fn parse_polygon_coordinates_from_array(scope: &Scope<'_>) -> CallResult<Vec<PixelsXY>> { |
no test coverage detected