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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//! A module for working with Pixelflut commands.
//!
//! # Examples
//!
//! Parsing commands:
//! ```
//! use pixelflut::{Command, Pixel, Coordinate};
//! use pixelflut::error::Result;
//!
//! let command: Result<Command> = "PX 10 20 aabbcc".parse();
//!
//! match command {
//!     Ok(Command::Px( Pixel{ position: Coordinate{ x, y }, .. } ))
//!         => println!("draw pixel on {x} {y}", x = x, y = y),
//!     Ok(Command::Size) => println!("resturn size of field"),
//!     Err(err) => println!("client send shit: {}", err),
//! }
//! ```

use std::{fmt};
use std::str::FromStr;

use pixel::{Pixel, Coordinate, Color};
use error::{Error, ErrorKind, Result};

/// A pixelflut command
/// 
/// Send to the Server
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum Command {
    Px(Pixel),
    Size,
}

impl fmt::Display for Command {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Command::Px ( ref pixel ) => write!(f, "PX {}", pixel),
            Command::Size => write!(f, "SIZE"),
        }
    }
}

impl FromStr for Command {
    type Err = Error;

    fn from_str(s: &str) -> Result<Command> {
        let mut iter = s.split_whitespace();

        let command = iter.next().ok_or(ErrorKind::InvalidCommand)?;

        let command = match command {
            "PX" => { Command::Px( Pixel::new(
                Coordinate::new(
                    iter.next()
                        .ok_or(ErrorKind::WrongNumberOfArguments)?.parse()?,
                    iter.next()
                        .ok_or(ErrorKind::WrongNumberOfArguments)?.parse()?
                ),
                iter.next().ok_or(ErrorKind::WrongNumberOfArguments)?.parse::<Color>()?
            ) ) },
            "SIZE" => {
                if iter.next().is_some() {
                    return Err(ErrorKind::WrongNumberOfArguments.into())
                } else {
                    Command::Size
                }
            },
            _ => return Err(ErrorKind::InvalidCommand.into()),
        };

        if iter.next() == None {
            Ok(command)
        } else {
            Err(ErrorKind::WrongNumberOfArguments.into())
        }

    }
}

impl From<Pixel> for Command {
    fn from(pixel: Pixel) -> Command {
        Command::Px(pixel)
    }
}

/// A pixelflut command
/// 
/// Send to the Client
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum Response {
    Size { w: u32, h: u32 },
}

impl fmt::Display for Response {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Response::Size { w, h } =>
                write!(f, "SIZE {} {}", w, h),
        }
    }
}

impl FromStr for Response {
    type Err = Error;

    fn from_str(s: &str) -> Result<Response> {
        let mut iter = s.split_whitespace();

        let command = iter.next().ok_or(ErrorKind::InvalidCommand)?;

        let command = match command {
            "SIZE" => {
                if let Some(w) = iter.next() {
                    Response::Size {
                        w: w.parse()?,
                        h: iter.next()
                            .ok_or(ErrorKind::WrongNumberOfArguments)?.parse()?,
                    }
                } else {
                    return Err(ErrorKind::WrongNumberOfArguments.into())
                }
            },
            _ => return Err(ErrorKind::InvalidCommand.into()),
        };

        if iter.next() == None {
            Ok(command)
        } else {
            Err(ErrorKind::WrongNumberOfArguments.into())
        }

    }
}

#[cfg(test)]
mod test {
    #[test]
    fn display() {
        use command::{Command, Response};
        use pixel::Pixel;

        let pxcommand = Command::Px(Pixel::new((45, 67), (0x11, 0x22, 0x55)));

        assert_eq!( format!("{}", pxcommand), "PX 45 67 112255" );
        assert_eq!( pxcommand, "PX 45 67 112255".parse().unwrap() );
        assert_eq!( format!("{}", Command::Size), "SIZE" );
        assert_eq!( Command::Size, "SIZE".parse().unwrap() );
        assert_eq!(
            format!("{}", Response::Size { w: 12, h: 34 } ),
            "SIZE 12 34"
        );
        assert_eq!(
            Response::Size { w: 12, h: 34 },
            "SIZE 12 34".parse().unwrap()
        );
        assert!( "SIZE Blah".parse::<Response>().is_err() );
        assert!( "FOO".parse::<Response>().is_err() );
        assert!( "FOO".parse::<Response>().is_err() );
    }

}