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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#[derive(Serialize, Deserialize)]
pub enum Command {
    Point(f32, f32),
    Line(f32, f32, f32, f32),
    Clear,
    Done,
}

impl Command {
    pub fn point<P: Into<(f32, f32)>>(p: P) -> Command {
        let t = p.into();
        Command::Point(t.0, t.1)
    }

    pub fn line_points<P: Into<(f32, f32)>>(
        p1: P,
        p2: P,
    ) -> Command {
        let t1 = p1.into();
        let t2 = p2.into();
        Command::Line(t1.0, t1.1, t2.0, t2.1)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json;

    #[test]
    fn line_coords() {
        let l = Command::Line(0.0, 0.0, 1.0, 1.0);
        let json = serde_json::to_string(&l).unwrap();
        let bytes = json.len();
        assert_eq!(json, "{\"Line\":[0.0,0.0,1.0,1.0]}");
        assert_eq!(bytes, 26)
    }

    #[test]
    fn line_points() {
        let l = Command::line_points((0.0, 0.0), (1.0, 1.0));
        let json = serde_json::to_string(&l).unwrap();
        let bytes = json.len();
        assert_eq!(json, "{\"Line\":[0.0,0.0,1.0,1.0]}");
        assert_eq!(bytes, 26)
    }

    #[test]
    fn clear() {
        let c = Command::Clear;
        let json = serde_json::to_string(&c).unwrap();
        let bytes = json.len();
        assert_eq!(json, "\"Clear\"");
        assert_eq!(bytes, 7)
    }

    #[test]
    fn vec() {
        let mut buffer = Vec::new();

        buffer.push(Command::Clear);
        let mut r = 0.0;
        while r < 10.0 {
            let l = Command::Line(0.0, r, 0.0, r + 1.0);
            buffer.push(l);
            r += 1.0;
        }
        buffer.push(Command::Done);

        let json = serde_json::to_string(&buffer).unwrap();
        let bytes = json.len();
        assert_eq!(
            json,
            "[\"Clear\",{\"Line\":[0.0,0.0,0.0,1.0]},{\"Line\":[0.0,1.0,0.0,2.0]},{\"Line\":[0.0,2.0,0.0,3.0]},{\"Line\":[0.0,3.0,0.0,4.0]},{\"Line\":[0.0,4.0,0.0,5.0]},{\"Line\":[0.0,5.0,0.0,6.0]},{\"Line\":[0.0,6.0,0.0,7.0]},{\"Line\":[0.0,7.0,0.0,8.0]},{\"Line\":[0.0,8.0,0.0,9.0]},{\"Line\":[0.0,9.0,0.0,10.0]},\"Done\"]"
        );
        assert_eq!(bytes, 287)
    }
}