-
-
Notifications
You must be signed in to change notification settings - Fork 119
/
lib.rs
235 lines (203 loc) · 7.61 KB
/
lib.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
use std::{
collections::{HashMap, HashSet},
ffi::OsStr,
io,
path::PathBuf,
process::Command,
sync::mpsc,
thread,
};
use notify::Watcher as _;
use thiserror::Error;
pub struct Model {
src_path: PathBuf,
lib_path: PathBuf,
manifest_path: PathBuf,
}
impl Model {
pub fn from_path(
path: PathBuf,
target_dir: Option<PathBuf>,
) -> io::Result<Self> {
let name = {
// Can't panic. It only would, if the path ends with "..", and we
// are canonicalizing it here to prevent that.
let canonical = path.canonicalize()?;
let file_name = canonical.file_name().unwrap();
file_name.to_string_lossy().replace('-', "_")
};
let src_path = path.join("src");
let lib_path = {
let file = if cfg!(windows) {
format!("{}.dll", name)
} else if cfg!(target_os = "macos") {
format!("lib{}.dylib", name)
} else {
//Unix
format!("lib{}.so", name)
};
let target_dir = target_dir.unwrap_or_else(|| path.join("target"));
target_dir.join("debug").join(file)
};
let manifest_path = path.join("Cargo.toml");
Ok(Self {
src_path,
lib_path,
manifest_path,
})
}
pub fn load_once(
&self,
arguments: &HashMap<String, String>,
) -> Result<fj::Shape, Error> {
let manifest_path = self.manifest_path.display().to_string();
let status = Command::new("cargo")
.arg("build")
.args(["--manifest-path", &manifest_path])
.status()?;
if !status.success() {
return Err(Error::Compile);
}
// So, strictly speaking this is all unsound:
// - `Library::new` requires us to abide by the arbitrary requirements
// of any library initialization or termination routines.
// - `Library::get` requires us to specify the correct type for the
// model function.
// - The model function itself is `unsafe`, because it is a function
// from across an FFI interface.
//
// Typical models won't have initialization or termination routines (I
// think), should abide by the `ModelFn` signature, and might not do
// anything unsafe. But we have no way to know that the library the user
// told us to load actually does (I think).
//
// I don't know of a way to fix this. We should take this as motivation
// to switch to a better technique:
// https://github.com/hannobraun/Fornjot/issues/71
let shape = unsafe {
let lib = libloading::Library::new(&self.lib_path)?;
let model: libloading::Symbol<ModelFn> = lib.get(b"model")?;
model(arguments)
};
Ok(shape)
}
pub fn load_and_watch(
self,
parameters: HashMap<String, String>,
) -> Result<Watcher, Error> {
let (tx, rx) = mpsc::sync_channel(0);
let tx2 = tx.clone();
let watch_path = self.src_path.clone();
let mut watcher = notify::recommended_watcher(
move |event: notify::Result<notify::Event>| {
// Unfortunately the `notify` documentation doesn't say when
// this might happen, so no idea if it needs to be handled.
let event = event.expect("Error handling watch event");
// Various acceptable ModifyKind kinds. Varies across platforms
// (e.g. MacOs vs. Windows10)
if let notify::EventKind::Modify(
notify::event::ModifyKind::Any,
)
| notify::EventKind::Modify(
notify::event::ModifyKind::Data(
notify::event::DataChange::Any,
),
)
| notify::EventKind::Modify(
notify::event::ModifyKind::Data(
notify::event::DataChange::Content,
),
) = event.kind
{
let file_ext = event
.paths
.get(0)
.expect("File path missing in watch event")
.extension();
let black_list = HashSet::from([
OsStr::new("swp"),
OsStr::new("tmp"),
OsStr::new("swx"),
]);
if let Some(ext) = file_ext {
if black_list.contains(ext) {
return;
}
}
// This will panic, if the other end is disconnected, which
// is probably the result of a panic on that thread, or the
// application is being shut down.
//
// Either way, not much we can do about it here, except
// maybe to provide a better error message in the future.
tx.send(()).unwrap();
}
},
)?;
watcher.watch(&watch_path, notify::RecursiveMode::Recursive)?;
// To prevent a race condition between the initial load and the start of
// watching, we'll trigger the initial load here, after having started
// watching.
//
// Will panic, if the receiving end has panicked. Not much we can do
// about that, if it happened.
thread::spawn(move || tx2.send(()).unwrap());
Ok(Watcher {
_watcher: Box::new(watcher),
channel: rx,
model: self,
parameters,
})
}
}
pub struct Watcher {
_watcher: Box<dyn notify::Watcher>,
channel: mpsc::Receiver<()>,
model: Model,
parameters: HashMap<String, String>,
}
impl Watcher {
pub fn receive(&self) -> Option<fj::Shape> {
match self.channel.try_recv() {
Ok(()) => {
let shape = match self.model.load_once(&self.parameters) {
Ok(shape) => shape,
Err(Error::Compile) => {
// It would be better to display an error in the UI,
// where the user can actually see it. Issue:
// https://github.com/hannobraun/fornjot/issues/30
println!("Error compiling model");
return None;
}
Err(err) => {
panic!("Error reloading model: {:?}", err);
}
};
Some(shape)
}
Err(mpsc::TryRecvError::Empty) => {
// Nothing to receive from the channel.
None
}
Err(mpsc::TryRecvError::Disconnected) => {
// The other end has disconnected. This is probably the result
// of a panic on the other thread, or a program shutdown in
// progress. In any case, not much we can do here.
panic!();
}
}
}
}
#[derive(Debug, Error)]
pub enum Error {
#[error("Error compiling model")]
Compile,
#[error("I/O error while loading model")]
Io(#[from] io::Error),
#[error("Error loading model from dynamic library")]
LibLoading(#[from] libloading::Error),
#[error("Error watching model for changes")]
Notify(#[from] notify::Error),
}
type ModelFn =
unsafe extern "C" fn(args: &HashMap<String, String>) -> fj::Shape;