1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
pub enum Direction {
    Left,
    Right,
    Up,
    Down,
}

impl Direction {
    pub fn iter<'a>(
        &'a self,
        start_point: (usize, usize),
        max: (usize, usize),
    ) -> DirectionIterator<'a> {
        DirectionIterator {
            direction: self,
            cur_point: start_point,
            max,
        }
    }
}

pub struct DirectionIterator<'a> {
    direction: &'a Direction,
    cur_point: (usize, usize),
    max: (usize, usize),
}

impl<'a> Iterator for DirectionIterator<'a> {
    type Item = (usize, usize);

    fn next(&mut self) -> Option<Self::Item> {
        let next_point = match self.direction {
            Direction::Up => (self.cur_point.0.checked_sub(1)?, self.cur_point.1),
            Direction::Right => (
                self.cur_point.0,
                max_or_option(self.cur_point.1 + 1, self.max.1)?,
            ),
            Direction::Down => (
                max_or_option(self.cur_point.0 + 1, self.max.0)?,
                self.cur_point.1,
            ),
            Direction::Left => (self.cur_point.0, self.cur_point.1.checked_sub(1)?),
        };
        self.cur_point = next_point;
        Some(next_point)
    }
}

fn max_or_option(v: usize, max: usize) -> Option<usize> {
    if v < max {
        return Some(v);
    }
    None
}