diff --git a/README.md b/README.md index d690619..3b91d1d 100644 --- a/README.md +++ b/README.md @@ -98,7 +98,11 @@ EXAMPLE HERE WITH LOOPS IN THREADS AND BLOCKING AND WHATEVER Notice how in our STDIN thread we are calling `Engine::parse` and the handling the output using a match statement depending on what kind of token it was. You are responsible for obeying the commands from this thread, the UCI library -just makes it convenient to work with. +just makes it convenient to work with. Here is an example of handling a parsed command: + +``` +EXAMPLE OF HANDLING A PARSED COMMAND AND SENDING BACK SOMETHING +``` At this point the engine will send a series of `setoption` commands to you in your STDIN thread. We can use the `Engine::parse` function to get these: diff --git a/src/lib.rs b/src/lib.rs index eedebb4..e3e46c4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,6 +9,7 @@ pub mod commands; pub mod options; +pub mod parser; use std::io::{BufRead, Write}; use std::vec; diff --git a/src/parser.rs b/src/parser.rs index 9be3f3e..d14a406 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -4,6 +4,7 @@ use std::collections::HashMap; use commands; /// Token represents a parsable token in the string sent via STDIN from the GUI +#[derive(Debug)] pub enum Token { UCI, DEBUG, @@ -18,6 +19,7 @@ pub enum Token { QUIT, } +#[derive(Clone, Debug, PartialEq, PartialOrd)] pub enum CommandValue { Int(i32), Float(f64), @@ -39,6 +41,7 @@ pub struct Parser {} /// /// 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)] pub struct TokenResult { token: Token, args: Option>, @@ -61,6 +64,10 @@ impl Parser { // TODO: Parse things based on the token, probably need a special function for each token based on a switch // of tokens[0]. + + + // remove this when it's working + Err("lol") } } diff --git a/tests/parser.rs b/tests/parser.rs index 42354e8..63f407b 100644 --- a/tests/parser.rs +++ b/tests/parser.rs @@ -4,3 +4,35 @@ // a cakewalk // // // Also remember to fix the constant thing noted in the lib.rs for uci.... lol +// + +extern crate uci; + +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( + [ + ("name", parser::CommandValue::Text(String::from("Hash"))), + ("value", parser::CommandValue::Int(32)), + ].iter().cloned().collect()), + }; + + let p = parser::Parser::new(); + + match p.parse(s) { + Ok(r) => { + assert_eq!(r, expected); + } + Err(_) => panic!("failed to parse setoption as expected") + } +} + +#[test] +fn bad_parse() { + assert_eq!(true, false); +}