aboutsummaryrefslogtreecommitdiff
path: root/src/consulter.rs
blob: a8e666f8ce15df688f44b3659d23ec7bfc09afb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
use std::fs::File;
use std::io::{Error, ErrorKind, Result, Write};
use std::process::Command;

use crate::game::*;
use crate::parser::*;

const FILE_NAME: &str = "/tmp/takwrap_ptn_consult.ptn";

pub fn consult_next_action(game: &Game, proc: &str) -> Result<Action> {
    let file = File::create(FILE_NAME)?;

    write!(&file, "{}", game)?;

    let output = Command::new(proc).args(&[FILE_NAME]).output()?;

    match String::from_utf8(output.stdout) {
        Err(e) => Err(Error::new(ErrorKind::InvalidData, e)),
        Ok(string) => match parse_action(game.query_stone_owner(), &string.trim()) {
            Err(string) => Err(Error::new(ErrorKind::InvalidData, string)),
            Ok(action) => Ok(action),
        },
    }
}