From 4c12c857ff237fee1833c1a75d27e9616b2c5999 Mon Sep 17 00:00:00 2001 From: Taylor Bockman Date: Wed, 20 Dec 2017 22:21:44 -0800 Subject: [PATCH] lol --- Cargo.toml | 1 + src/lib.rs | 2 ++ src/parser.rs | 27 ++++++++++++++++++++++----- tests/options.rs | 1 - tests/parser.rs | 43 ++++++++++++++++++++++++++++++++++++++----- 5 files changed, 63 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index cf77afc..9b99552 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,3 +10,4 @@ categories = ["games", "game-engines", "api-bindings", "config", "development-to license = "GPL-3.0" [dependencies] +"either" = "1.4.0" diff --git a/src/lib.rs b/src/lib.rs index e3e46c4..04a47fb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,6 +7,8 @@ // IMPLEMENT OPTION SENDING. // ALSO CHANGE COMMANDS TO HAVE A CONSTANTS SUBMODULE LIKE OPTIONS +extern crate either; + pub mod commands; pub mod options; pub mod parser; diff --git a/src/parser.rs b/src/parser.rs index d14a406..4919547 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1,10 +1,15 @@ //! Parser contains the command parser for handling receiving commands from the GUI + use std::collections::HashMap; use commands; +use either::Either; +pub use either::Left; +pub use either::Right; + /// Token represents a parsable token in the string sent via STDIN from the GUI -#[derive(Debug)] +#[derive(Debug, Eq, PartialEq)] pub enum Token { UCI, DEBUG, @@ -38,13 +43,25 @@ pub struct Parser {} /// --------- ---- ---- ----- -- /// Token arg val arg val /// +/// debug on +/// ----- -- +/// Token val +/// /// /// The user is still responsible for knowing what to look for in the hashmap but this structure makes it far /// easier to work with commands. Some commands don't have arguments, in that case the args will be None. -#[derive(Debug)] +#[derive(Debug, PartialEq)] pub struct TokenResult { token: Token, - args: Option>, + args: Either>, &'static str>, +} + +impl TokenResult { + + /// Intantiates a new TokenResult + pub fn new(token: Token, args: Either>, &'static str>>) -> TokenResult { + TokenResult { token, args } + } } impl Parser { @@ -56,7 +73,7 @@ impl Parser { /// Parses a single string from the GUI and turns it into a neatly packaged TokenResult for processing /// by the engine. - pub fn parse(s: &str) -> Result { + pub fn parse(&self, s: &str) -> Result { // TODO: Remove the /n from the end @@ -67,7 +84,7 @@ impl Parser { // remove this when it's working - Err("lol") + Ok(TokenResult::new(Token::SETOPTION, Left(Some(HashMap::new())))) } } diff --git a/tests/options.rs b/tests/options.rs index f35b73c..9c7075c 100644 --- a/tests/options.rs +++ b/tests/options.rs @@ -1,6 +1,5 @@ extern crate uci; -use uci::Engine; use uci::options::constants; use uci::options::{ EngineOption, EngineOptionType, EngineOptionDataType, EngineOptionData }; diff --git a/tests/parser.rs b/tests/parser.rs index 63f407b..9993f10 100644 --- a/tests/parser.rs +++ b/tests/parser.rs @@ -13,14 +13,14 @@ use uci::parser; #[test] fn parse_setoption() { let s = "setoption name Hash value 32\n"; - let expected = parser::TokenResult { - token: parser::Token::SETOPTION, - args: Some( + let expected = parser::TokenResult::new( + parser::Token::SETOPTION, + parser::Left(Some( [ ("name", parser::CommandValue::Text(String::from("Hash"))), ("value", parser::CommandValue::Int(32)), - ].iter().cloned().collect()), - }; + ].iter().cloned().collect()) + )); let p = parser::Parser::new(); @@ -33,6 +33,39 @@ fn parse_setoption() { } #[test] +fn parse_uci() { + let s = "uci\n"; + let expected = parser::TokenResult::new( + parser::Token::UCI, parser::Left(None); + + let p = parser::Parser::new(); + + match p.parse(s) { + Ok(r) => { + assert_eq!(r, expected); + } + Err(_) => panic!("failed to parse uci as expected") + } +} + +#[test] +fn parse_debug() { + let s = "debug on\n"; + let expected = parser::TokenResult::new( + parser::Token::DEBUG, parser::Right("on") + ); + + let p = parser::Parser::new(); + + match p.parse(s) { + Ok(r) => { + assert_eq!(r, expected); + } + Err(_) => panic!("failed to parse uci as expected") + } +} + +#[test] fn bad_parse() { assert_eq!(true, false); }