diff options
author | stuebinm | 2021-10-26 18:13:50 +0200 |
---|---|---|
committer | stuebinm | 2021-10-26 18:13:50 +0200 |
commit | e7427d5c67c0a90c369adecb4b0c65c043cb2e34 (patch) | |
tree | 3e58c244cda8d34f9ab3f087bacc1ed6aeedfdf8 /isabelle-proto/src/pipe.rs | |
parent | 4c6d323ad1536e9ef6082eb34d872ccae1f389ef (diff) |
Diffstat (limited to 'isabelle-proto/src/pipe.rs')
-rw-r--r-- | isabelle-proto/src/pipe.rs | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/isabelle-proto/src/pipe.rs b/isabelle-proto/src/pipe.rs new file mode 100644 index 0000000..ef80ff3 --- /dev/null +++ b/isabelle-proto/src/pipe.rs @@ -0,0 +1,63 @@ + +use regex::Regex; +use serde::Deserialize; +use std::{io::{BufRead, BufReader}, process::ChildStdout}; + +use crate::messages::*; + +pub(crate) fn decode_sync<'a, T> (msg: &'a str) -> Option<SyncAnswer<T, String>> +where T: Deserialize<'a> { + let regex = Regex::new(r"^[a-zA-Z]+\b").unwrap(); + let mat = regex.find(msg)?; + let ty = &msg[..mat.end()]; + let rest = &msg[mat.end()..]; + + match ty { + "OK" => Some(SyncAnswer::Ok(serde_json::from_str(&rest).ok()?)), + "ERROR" => Some(SyncAnswer::Error(rest.to_owned())), + _ => None + } +} + +pub(crate) fn decode_async<'a,T,E,N> (msg: &'a str) -> Option<AsyncAnswer<T,E,N>> +where T: Deserialize<'a>, N: Deserialize<'a>, E: Deserialize<'a> { + let regex = Regex::new(r"^[a-zA-Z]+\b").unwrap(); + let mat = regex.find(msg)?; + let ty = &msg[..mat.end()]; + let rest = &msg[mat.end()..]; + + match ty { + "NOTE" => Some(AsyncAnswer::Note(serde_json::from_str(&rest).ok()?)), + "FINISHED" => Some(AsyncAnswer::Finished(serde_json::from_str(&rest).ok()?)), + "FAILED" => Some(AsyncAnswer::Failed(serde_json::from_str(&rest).ok()?)), + _ => None + } +} + + + + +pub(crate) fn wait_for_client(pipe: &mut BufReader<ChildStdout>) -> Option<ClientHello> { + for res in pipe.lines() { + match res { + Err(_) => (), + Ok(line) => { + let hello : Option<SyncAnswer<ClientHello, _>> = decode_sync(&line); + if let Some(SyncAnswer::Ok(data)) = hello { + return Some(data); + } + } + } + } + None +} + + +pub(crate) fn get_async_task_id (reader: &mut BufReader<ChildStdout>) -> TaskID { + let mut res = String::new(); + reader.read_line(&mut res).unwrap(); + match decode_sync(&res).unwrap() { + SyncAnswer::Ok(AsyncStartOk { task }) => task, + SyncAnswer::Error(_) => panic!("failed to start async task!") + } +} |