use super::model::{Line, Point}; use crate::parsing::{newline, parse_n, parse_number}; use yap::Tokens; pub fn parse_point(tokens: &mut impl Tokens) -> Option { parse_n(tokens, |t| parse_number(t), |t| t.token(',')).map(|point: [usize; 2]| Point { x: point[0], y: point[1], }) } pub fn parse_line(tokens: &mut impl Tokens) -> Option { parse_n(tokens, |t| parse_point(t), |t| t.tokens(" -> ".chars())).map(|points: [Point; 2]| { Line { start: points[0], end: points[1], } }) } pub fn parse_lines(tokens: &mut impl Tokens) -> Vec { tokens.sep_by(|t| parse_line(t), |t| newline(t)).collect() } #[cfg(test)] mod test { use super::super::model::{Line, Point}; use yap::IntoTokens; #[test] fn parse_point() { assert_eq!( Some(Point { x: 1, y: 3 }), super::parse_point(&mut "1,3".into_tokens()) ); } #[test] fn parse_line() { assert_eq!( Some(Line { start: Point { x: 1, y: 3 }, end: Point { x: 5, y: 3 } }), super::parse_line(&mut "1,3 -> 5,3".into_tokens()) ); } }