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
55
56
57
58
59
60
//! Path types and tools.

use crate::P2;
use lyon_path::PathEvent;

/// An adapter for iterators over points that implements `Path`.
#[derive(Debug, Copy, Clone)]
pub struct FlatIterPath<I> {
    src: I,
    last: Option<P2>,
    first: Option<P2>,
    closed: bool,
}

impl<I> FlatIterPath<I>
where
    I: Iterator<Item = P2>,
{
    pub fn new(src: I, closed: bool) -> Self {
        Self {
            src,
            last: None,
            first: None,
            closed,
        }
    }
}

impl<I> Iterator for FlatIterPath<I>
where
    I: Iterator<Item = P2>,
{
    type Item = PathEvent;
    fn next(&mut self) -> Option<Self::Item> {
        let p = match self.src.next() {
            Some(p) => p,
            None => match (self.first.take(), self.last.take()) {
                (Some(first), Some(last)) => {
                    return Some(PathEvent::End {
                        last,
                        first,
                        close: self.closed,
                    })
                }
                _ => return None,
            },
        };

        let result = if let Some(last) = self.last {
            Some(PathEvent::Line { from: last, to: p })
        } else {
            assert!(self.first.is_none());
            self.first = Some(p);
            Some(PathEvent::Begin { at: p })
        };

        self.last = Some(p);
        result
    }
}