-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathworkers.rs
116 lines (102 loc) · 2.83 KB
/
workers.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use serde::{Deserialize, Serialize};
use std::io::{BufRead, BufReader, Write};
use std::process::{Command, Stdio};
use crate::model::{BrowserPath, PathData};
#[derive(Serialize, Deserialize, Debug)]
#[serde(tag = "type", content = "data")]
pub enum NixValue {
#[serde(rename = "0")]
Thunk,
#[serde(rename = "1")]
Int(i64),
#[serde(rename = "2")]
Float(f64),
#[serde(rename = "3")]
Bool(bool),
#[serde(rename = "4")]
String(String),
#[serde(rename = "5")]
Path(String),
#[serde(rename = "6")]
Null,
#[serde(rename = "7")]
Attrs(Vec<String>),
#[serde(rename = "8")]
List(usize),
#[serde(rename = "9")]
Function,
#[serde(rename = "10")]
External,
#[serde(rename = "11")]
Error(String),
}
pub const WORKER_BINARY_PATH: &str = env!("WORKER_BINARY_PATH");
pub struct WorkerHost {
pub tx: kanal::Sender<BrowserPath>,
pub rx: kanal::Receiver<(BrowserPath, PathData)>,
}
impl WorkerHost {
pub fn new(expr: String) -> WorkerHost {
let (tx, rx) = kanal::unbounded::<BrowserPath>();
let (result_tx, result_rx) = kanal::unbounded();
let rx = rx.clone();
let result_tx = result_tx.clone();
std::thread::spawn(move || {
let mut child = Command::new(WORKER_BINARY_PATH)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
// .stderr(Stdio::piped())
.spawn()
.expect("Failed to spawn worker");
let mut stdin = child.stdin.take().expect("Failed to open stdin");
let stdout = child.stdout.take().expect("Failed to open stdout");
let mut reader = BufReader::new(stdout);
let _ = writeln!(stdin, "{}", expr);
loop {
let received = rx.recv();
tracing::info!("{:?}", received);
match received {
Ok(path) => {
result_tx
.send((path.clone(), PathData::Loading))
.expect("Failed to send loading state");
if let Err(e) = writeln!(stdin, "{}", path.to_expr()) {
tracing::error!("Failed to send path, {e}");
break;
}
let mut response = String::new();
if let Err(e) = reader.read_line(&mut response) {
tracing::error!("Failed to read response: {e}");
let _ = result_tx.send((
path,
PathData::Error(format!("Failed to read response: {e}")),
));
continue;
}
let value: NixValue = match serde_json::from_str(&response) {
Ok(v) => v,
Err(e) => {
tracing::error!("{response}");
tracing::error!("Failed to deserialize response: {e}");
let _ = result_tx.send((
path,
PathData::Error(format!("Failed to deserialize response: {e}")),
));
continue;
}
};
result_tx
.send((path, value.into()))
.expect("Failed to send result");
}
Err(_) => {
// Channel closed, exit the loop
break;
}
}
}
child.kill().expect("killing child failed");
});
WorkerHost { tx, rx: result_rx }
}
}