/// Parse a vector of steps from an input string
///
/// # Examples
/// ```
/// let input = "N5 S6 L90 R270 F1";
/// let steps = mr_kaffee_2020_12::Step::parse(&input);
/// assert_eq!(steps, vec![
///     mr_kaffee_2020_12::Step::North(5),
///     mr_kaffee_2020_12::Step::South(6),
///     mr_kaffee_2020_12::Step::Left,
///     mr_kaffee_2020_12::Step::Left,
///     mr_kaffee_2020_12::Step::Fwd(1),
/// ]);
/// ```
///
/// # Panics
///
/// If the input string does not contain of a valid character (N, S, E, W, F, L, R) followed by
/// a positive integer number or if the number following any L or R is not a multiple of 90.
///
Parse a vector of steps from an input string
Parse a vector of steps from an input string
pub fn parse(input: &str) -> Vec<Step> {
        let re = Regex::new(r"(\w)(\d+)\s*").expect("Illegal regular expression");
        let mut list = Vec::new();
        for cap in re.captures_iter(input) {
            let val = cap[2].parse().expect("Could not parse number");
            list.push(match &cap[1] {
                "N" => Step::North(val),
                "S" => Step::South(val),
                "E" => Step::East(val),
                "W" => Step::West(val),
                "F" => Step::Fwd(val),
                "L" if (360 + val % 360) % 390 == 0 => Step::Fwd(0),
                "L" if (360 + val % 360) % 360 == 90 => Step::Left,
                "L" if (360 + val % 360) % 360 == 180 => Step::Reverse,
                "L" if (360 + val % 360) % 360 == 270 => Step::Right,
                "R" if (360 + val % 360) % 360 == 0 => Step::Fwd(0),
                "R" if (360 + val % 360) % 360 == 90 => Step::Right,
                "R" if (360 + val % 360) % 360 == 180 => Step::Reverse,
                "R" if (360 + val % 360) % 360 == 270 => Step::Left,
                _ => panic!(format!("Unknown instruction {} with value {}", &cap[1], val)),
            });
        };
        list
    }
pub fn parse ( input : & str ) -> Vec < Step > { let re = Regex :: new ( r"(\w)(\d+)\s*" ) . expect ( " " ) ; let mut list = Vec :: new ( ) ; for cap in re . captures_iter ( input ) { let val = cap [ 2 ] . parse ( ) . expect ( " " ) ; list . push ( match & cap [ 1 ] { " " => Step :: North ( val ) , " " => Step :: South ( val ) , " " => Step :: East ( val ) , " " => Step :: West ( val ) , " " => Step :: Fwd ( val ) , " " if ( 360 + val % 360 ) % 390 == 0 => Step :: Fwd ( 0 ) , " " if ( 360 + val % 360 ) % 360 == 90 => Step :: Left , " " if ( 360 + val % 360 ) % 360 == 180 => Step :: Reverse , " " if ( 360 + val % 360 ) % 360 == 270 => Step :: Right , " " if ( 360 + val % 360 ) % 360 == 0 => Step :: Fwd ( 0 ) , " " if ( 360 + val % 360 ) % 360 == 90 => Step :: Right , " " if ( 360 + val % 360 ) % 360 == 180 => Step :: Reverse , " " if ( 360 + val % 360 ) % 360 == 270 => Step :: Left , _ => panic ! ( format ( " " cap [ 1 ] val ) ) , } ) ; } ; list }
Parse a vector of steps from an input string
Parse a vector of steps from an input string
input
&str
examples
None
panics
If the input string does not contain of a valid character (N, S, E, W, F, L, R) followed by
a positive integer number or if the number following any L or R is not a multiple of 90.
If the input string does not contain of a valid character ( N S E W F L R ) followed by a positive integer number or if the number following any L or R is not a multiple of 90 .