vampirc / vampirc-uci

A Universal Chess Interface (UCI) protocol parser and message generator.

Home Page:https://vampirc.kejzar.si

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Reading UCI commands from stdin hangs the program

marmistrz opened this issue · comments

Hi,

I tried using your crate to parse UCI from stdin, synchronously.

use std::io::{self, BufRead};
use vampirc_uci::{parse, MessageList};

fn main() -> io::Result<()> {
    for line in io::stdin().lock().lines() {
        println!("got line: {:?}", line);
        let msgs: MessageList = parse(&line?);
        for msg in msgs {
            println!("parsed: {}", msg);
        }
    }

    Ok(())
}

It appears that the commands are not properly parsed if they don't contain a trailing newline and such is stripped by read_line. Moreover, parse just hangs and doesn't return any error.

On the other hand, I'm wondering if it wouldn't be better for parse to accept any type that implements BufRead. It makes no sense to iterate over the messages if we read stdin line-by-line.

Okay, it definitely should not just be hanging if newlines are omitted. Gonna look into this ASAP.

I retested it and I must've mistaken the freezes for just stdin waiting for more input. Sorry for that.

I also have a patch which allows reading single message. Let me know if you're interested in me upstreaming it.

It doesn't actually hang, just returns an empty MessageList. It is technically an error in the input and as explained in the docs, parse() ignores errors. If you try parse_strict() you will actually get an error back and if you try parse_with_unknown() you will actually get a UciMessage::Unknown back.

Although I can see how it could be confusing, I guess you would except to get the parsed message back even if it lacks a newline.

Ah, I see you already figured it out by yourself. Sure, send over the patch or do a pull request and I'll incorporate it into 0.10.0.