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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use super::Parser;
use crate::parsers::{CommandArg, CommandArgParseError, ParseInput};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Swizzle {
    pub x: bool,
    pub y: bool,
    pub z: bool,
}

impl CommandArg for Swizzle {
    fn parse_arg(input: &mut ParseInput) -> Result<Self, CommandArgParseError> {
        input.skip_whitespace();
        let mut swizzle = Swizzle::default();
        while let Some(c) = input.peek() {
            match c {
                'x' => swizzle.x = true,
                'y' => swizzle.y = true,
                'z' => swizzle.z = true,
                _ => break,
            }
            input.pop();
        }

        Ok(swizzle)
    }

    fn display() -> Parser {
        Parser::Swizzle
    }
}

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

    #[test]
    fn test_swizzle() {
        let mut input = ParseInput::new("xyzzzz");
        let swizzle = Swizzle::parse_arg(&mut input).unwrap();
        assert_eq!(
            swizzle,
            Swizzle {
                x: true,
                y: true,
                z: true
            }
        );
        assert!(input.is_done());

        let mut input = ParseInput::new("xzy");
        let swizzle = Swizzle::parse_arg(&mut input).unwrap();
        assert_eq!(
            swizzle,
            Swizzle {
                x: true,
                y: true,
                z: true
            }
        );
        assert!(input.is_done());

        let mut input = ParseInput::new("x");
        let swizzle = Swizzle::parse_arg(&mut input).unwrap();
        assert_eq!(
            swizzle,
            Swizzle {
                x: true,
                y: false,
                z: false
            }
        );
        assert!(input.is_done());

        let mut input = ParseInput::new("x y z zy xyz");
        let swizzle_a = Swizzle::parse_arg(&mut input).unwrap();
        let swizzle_b = Swizzle::parse_arg(&mut input).unwrap();
        let swizzle_c = Swizzle::parse_arg(&mut input).unwrap();
        let swizzle_d = Swizzle::parse_arg(&mut input).unwrap();
        let swizzle_e = Swizzle::parse_arg(&mut input).unwrap();
        assert_eq!(
            swizzle_a,
            Swizzle {
                x: true,
                y: false,
                z: false
            }
        );
        assert_eq!(
            swizzle_b,
            Swizzle {
                x: false,
                y: true,
                z: false
            }
        );
        assert_eq!(
            swizzle_c,
            Swizzle {
                x: false,
                y: false,
                z: true
            }
        );
        assert_eq!(
            swizzle_d,
            Swizzle {
                x: false,
                y: true,
                z: true
            }
        );
        assert_eq!(
            swizzle_e,
            Swizzle {
                x: true,
                y: true,
                z: true
            }
        );
    }
}