test and lint
This commit is contained in:
		
							parent
							
								
									f28c9fee83
								
							
						
					
					
						commit
						b6d54d086e
					
				
							
								
								
									
										11
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								src/main.rs
									
									
									
									
									
								
							| @ -20,20 +20,18 @@ fn main() { | |||||||
|         let code_generator = Arc::clone(&code_generator); |         let code_generator = Arc::clone(&code_generator); | ||||||
|         let rooms = Arc::clone(&rooms); |         let rooms = Arc::clone(&rooms); | ||||||
|         thread::spawn (move || { |         thread::spawn (move || { | ||||||
|             let mut ws = tungstenite::accept(stream.unwrap()).unwrap(); |             let ws = tungstenite::accept(stream.unwrap()).unwrap(); | ||||||
|             println!("New client!"); |  | ||||||
|             let mut ws = WebsocketWrapper::new(ws); |             let mut ws = WebsocketWrapper::new(ws); | ||||||
|  |             
 | ||||||
|             ws.send(msg!("HOSTJOIN")); |             ws.send(msg!("HOSTJOIN")); | ||||||
| 
 |  | ||||||
|             let msg = ws.recv(); |             let msg = ws.recv(); | ||||||
|             match msg {                
 |             match msg {                
 | ||||||
|                 None => (), |                 None => (), | ||||||
| 
 |  | ||||||
|                 Some(msg) => match msg.command.as_str() { |                 Some(msg) => match msg.command.as_str() { | ||||||
|                     "HOST" => { |                     "HOST" => { | ||||||
|                         let code = code_generator.lock().unwrap().generate(); |                         let code = code_generator.lock().unwrap().generate(); | ||||||
|                         let mut room = Room::default(); |                         let mut room = Room::default(); | ||||||
|                         let player = Player { name: "Guest".to_string() }; |                         let player = Player {}; | ||||||
|                         room.players.push(player); |                         room.players.push(player); | ||||||
|                         rooms.lock().unwrap().insert(code.clone(), room); |                         rooms.lock().unwrap().insert(code.clone(), room); | ||||||
|                         ws.send(msg!("CODE", code)); |                         ws.send(msg!("CODE", code)); | ||||||
| @ -41,14 +39,15 @@ fn main() { | |||||||
|                     _ => unimplemented!(), |                     _ => unimplemented!(), | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|  | 
 | ||||||
|         }); |         }); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | 
 | ||||||
| #[derive(Default)] | #[derive(Default)] | ||||||
| struct Room { | struct Room { | ||||||
|     players: Vec<Player>, |     players: Vec<Player>, | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| struct Player { | struct Player { | ||||||
|     name: String, |  | ||||||
| } | } | ||||||
|  | |||||||
| @ -10,13 +10,9 @@ pub type Result<T> = std::result::Result<T, Error>; | |||||||
| #[derive(PartialEq, Debug)] | #[derive(PartialEq, Debug)] | ||||||
| pub enum Error { | pub enum Error { | ||||||
|     BadParse, |     BadParse, | ||||||
|     UnknownCommand, |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| impl Message { | impl Message { | ||||||
|     pub fn new(command: String, args: Vec<String>) -> Self { |  | ||||||
| 	Message { command, args } |  | ||||||
|     } |  | ||||||
|     pub fn parse(text: String) -> Result<Message> { |     pub fn parse(text: String) -> Result<Message> { | ||||||
| 	let re = regex::Regex::new(r"^([A-Z_]+):\s*(.*)").unwrap(); | 	let re = regex::Regex::new(r"^([A-Z_]+):\s*(.*)").unwrap(); | ||||||
| 	match re.captures(text.as_str()) { | 	match re.captures(text.as_str()) { | ||||||
| @ -49,10 +45,7 @@ macro_rules! msg { | |||||||
|     ( $command:expr, $( $arg:expr ),*) => { |     ( $command:expr, $( $arg:expr ),*) => { | ||||||
| 	{ | 	{ | ||||||
| 	    let command = $command.to_string(); | 	    let command = $command.to_string(); | ||||||
| 	    let mut args = Vec::new(); | 	    let args = vec![$($arg.to_string()),*]; | ||||||
| 	    $( |  | ||||||
| 		args.push($arg.to_string()); |  | ||||||
| 	    )* |  | ||||||
| 	    Message { command, args } | 	    Message { command, args } | ||||||
| 	} | 	} | ||||||
|     }; |     }; | ||||||
| @ -61,7 +54,11 @@ macro_rules! msg { | |||||||
| 
 | 
 | ||||||
| impl std::fmt::Display for Message { | impl std::fmt::Display for Message { | ||||||
|     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> { |     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> { | ||||||
| 	write!(f, "{}: {}", self.command, self.args.as_slice().join(", ")) |         if self.args.is_empty() { | ||||||
|  |             write!(f, "{}:", self.command) | ||||||
|  |         } else { | ||||||
|  | 	    write!(f, "{}: {}", self.command, self.args.as_slice().join(", ")) | ||||||
|  |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @ -72,19 +69,13 @@ mod test { | |||||||
|     #[test] |     #[test] | ||||||
|     fn test_parse() -> Result<()> { |     fn test_parse() -> Result<()> { | ||||||
| 	let text = "COMMAND: arg1, arg2"; | 	let text = "COMMAND: arg1, arg2"; | ||||||
| 	let msg = Message::parse(text)?; | 	let msg = Message::parse(text.to_string())?; | ||||||
| 	assert_eq!(Message { | 	assert_eq!(msg!("COMMAND", "arg1", "arg2"), msg); | ||||||
| 	    command: "COMMAND", |  | ||||||
| 	    args: vec!["arg1", "arg2"], |  | ||||||
| 	}, msg); |  | ||||||
| 	Ok(()) | 	Ok(()) | ||||||
|     } |     } | ||||||
|     #[test] |     #[test] | ||||||
|     fn test_to_string() { |     fn test_to_string() { | ||||||
| 	let msg = Message { |         let msg = msg!("COMMAND", "arg1", "arg2"); | ||||||
| 	    command: "COMMAND", |  | ||||||
| 	    args: vec!["arg1", "arg2"], |  | ||||||
| 	}; |  | ||||||
| 	assert_eq!(msg.to_string(), "COMMAND: arg1, arg2".to_string()); | 	assert_eq!(msg.to_string(), "COMMAND: arg1, arg2".to_string()); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -21,12 +21,12 @@ impl WebsocketWrapper { | |||||||
|             Ok(WsMessage::Text(text)) => match Message::parse(text) { |             Ok(WsMessage::Text(text)) => match Message::parse(text) { | ||||||
|                 Ok(msg) => Some(msg), |                 Ok(msg) => Some(msg), | ||||||
|                 Err(_) => { |                 Err(_) => { | ||||||
|                     self.websocket.write_message(WsMessage::Text("ERROR: bad_format".to_string())); |                     self.websocket.write_message(WsMessage::Text("ERROR: bad_format".to_string())).unwrap(); | ||||||
|                     self.recv() |                     self.recv() | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|             _ => { |             _ => { | ||||||
|                 self.websocket.write_message(WsMessage::Text("ERROR: bad_command".to_string())); |                 self.websocket.write_message(WsMessage::Text("ERROR: bad_command".to_string())).unwrap(); | ||||||
|                 self.recv() |                 self.recv() | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|  | |||||||
| @ -8,7 +8,7 @@ async def test(): | |||||||
|         assert(response == "HOSTJOIN:") |         assert(response == "HOSTJOIN:") | ||||||
|         await ws.send("HOST:") |         await ws.send("HOST:") | ||||||
|         response = await ws.recv() |         response = await ws.recv() | ||||||
|         assert(re.match(r"(\d|[a-f]){6}", response)) |         assert(re.match(r"CODE: [a-f\d]{6}", response)) | ||||||
|          |          | ||||||
| asyncio.get_event_loop().run_until_complete(test()) | asyncio.get_event_loop().run_until_complete(test()) | ||||||
| print("All tests passed") | print("All tests passed") | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user