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
use super::Parser;
use crate::parsers::{AbsoluteOrRelative, CommandArg, CommandArgParseError, ParseInput};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct BlockPos {
    pub x: AbsoluteOrRelative<i32>,
    pub y: AbsoluteOrRelative<i32>,
    pub z: AbsoluteOrRelative<i32>,
}

impl CommandArg for BlockPos {
    fn parse_arg(input: &mut ParseInput) -> Result<Self, CommandArgParseError> {
        input.skip_whitespace();
        let x = AbsoluteOrRelative::<i32>::parse_arg(input)?;
        input.skip_whitespace();
        let y = AbsoluteOrRelative::<i32>::parse_arg(input)?;
        input.skip_whitespace();
        let z = AbsoluteOrRelative::<i32>::parse_arg(input)?;

        Ok(BlockPos { x, y, z })
    }

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

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

    #[test]
    fn test_block_pos() {
        let mut input = ParseInput::new("~-1 2 3");
        assert_eq!(
            BlockPos::parse_arg(&mut input).unwrap(),
            BlockPos {
                x: AbsoluteOrRelative::Relative(-1),
                y: AbsoluteOrRelative::Absolute(2),
                z: AbsoluteOrRelative::Absolute(3)
            }
        );
        assert!(input.is_done());

        let mut input = ParseInput::new("-1 ~2 3 ");
        assert_eq!(
            BlockPos::parse_arg(&mut input).unwrap(),
            BlockPos {
                x: AbsoluteOrRelative::Absolute(-1),
                y: AbsoluteOrRelative::Relative(2),
                z: AbsoluteOrRelative::Absolute(3)
            }
        );
        assert!(!input.is_done());

        let mut input = ParseInput::new("-1 2 ~3 4");
        assert_eq!(
            BlockPos::parse_arg(&mut input).unwrap(),
            BlockPos {
                x: AbsoluteOrRelative::Absolute(-1),
                y: AbsoluteOrRelative::Absolute(2),
                z: AbsoluteOrRelative::Relative(3)
            }
        );
        assert!(!input.is_done());
    }
}