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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
use crate::ViewerError;
use html_view_shared::{
PROTOCOL_VERSION, ViewerCommand, ViewerCommandResponse, ViewerContent, ViewerExitReason,
ViewerExitStatus,
};
use std::path::PathBuf;
use std::process::Child;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Duration;
use uuid::Uuid;
/// The result of opening a viewer.
#[derive(Debug)]
pub enum ViewerResult {
/// The viewer was opened in blocking mode and has completed.
Blocking(ViewerExitStatus),
/// The viewer was opened in non-blocking mode.
NonBlocking(ViewerHandle),
}
/// A handle to a running viewer process in non-blocking mode.
#[derive(Debug)]
pub struct ViewerHandle {
/// Unique identifier for this viewer instance.
pub id: Uuid,
/// The spawned child process.
child: Child,
/// Path to the result JSON file.
result_path: PathBuf,
/// Path to the temporary directory (will be cleaned up when handle is dropped).
temp_dir: PathBuf,
/// Optional path to the command file for sending runtime updates.
command_path: Option<PathBuf>,
/// Sequence counter for commands.
command_seq: Arc<AtomicU64>,
/// Optional path to the command response file.
response_path: Option<PathBuf>,
}
impl ViewerHandle {
/// Create a new viewer handle.
pub(crate) fn new(
id: Uuid,
child: Child,
result_path: PathBuf,
temp_dir: PathBuf,
command_path: Option<PathBuf>,
response_path: Option<PathBuf>,
) -> Self {
Self {
id,
child,
result_path,
temp_dir,
command_path,
command_seq: Arc::new(AtomicU64::new(0)),
response_path,
}
}
/// Try to check whether the viewer has finished and return its exit status.
///
/// This is non-blocking. Returns `Ok(None)` if the process is still running.
///
/// # Example
///
/// ```no_run
/// use html_view::{ViewerOptions, ViewerWaitMode, ViewerResult};
///
/// let mut options = ViewerOptions::inline_html("<h1>Test</h1>");
/// options.wait = ViewerWaitMode::NonBlocking;
///
/// if let ViewerResult::NonBlocking(mut handle) = html_view::open(options).unwrap() {
/// loop {
/// if let Some(status) = handle.try_wait().unwrap() {
/// println!("Viewer exited: {:?}", status);
/// break;
/// }
/// // Do other work...
/// std::thread::sleep(std::time::Duration::from_millis(100));
/// }
/// }
/// ```
pub fn try_wait(&mut self) -> Result<Option<ViewerExitStatus>, ViewerError> {
match self.child.try_wait()? {
Some(_exit_status) => {
// Process has exited, read the result file
let result = self.read_result_file()?;
Ok(Some(result))
}
None => Ok(None),
}
}
/// Block until the viewer finishes and return its exit status.
///
/// # Example
///
/// ```no_run
/// use html_view::{ViewerOptions, ViewerWaitMode, ViewerResult};
///
/// let mut options = ViewerOptions::inline_html("<h1>Test</h1>");
/// options.wait = ViewerWaitMode::NonBlocking;
///
/// if let ViewerResult::NonBlocking(handle) = html_view::open(options).unwrap() {
/// let status = handle.wait().unwrap();
/// println!("Viewer exited: {:?}", status);
/// }
/// ```
pub fn wait(mut self) -> Result<ViewerExitStatus, ViewerError> {
self.child.wait()?;
self.read_result_file()
}
/// Attempt to terminate the viewer process early.
///
/// # Example
///
/// ```no_run
/// use html_view::{ViewerOptions, ViewerWaitMode, ViewerResult};
///
/// let mut options = ViewerOptions::inline_html("<h1>Test</h1>");
/// options.wait = ViewerWaitMode::NonBlocking;
///
/// if let ViewerResult::NonBlocking(mut handle) = html_view::open(options).unwrap() {
/// std::thread::sleep(std::time::Duration::from_secs(2));
/// handle.terminate().unwrap();
/// }
/// ```
pub fn terminate(&mut self) -> Result<(), ViewerError> {
self.child.kill()?;
Ok(())
}
/// Refresh the viewer with new content.
///
/// This updates the displayed content without closing the window.
/// The window and behavior options remain unchanged.
///
/// # Example
///
/// ```no_run
/// use html_view::{ViewerOptions, ViewerWaitMode, ViewerResult, ViewerContent};
///
/// let mut options = ViewerOptions::inline_html("<h1>Initial</h1>");
/// options.wait = ViewerWaitMode::NonBlocking;
///
/// if let ViewerResult::NonBlocking(mut handle) = html_view::open(options).unwrap() {
/// std::thread::sleep(std::time::Duration::from_secs(2));
///
/// // Update content
/// handle.refresh(ViewerContent::InlineHtml {
/// html: "<h1>Updated!</h1>".to_string(),
/// base_dir: None,
/// }).unwrap();
/// }
/// ```
pub fn refresh(&mut self, content: ViewerContent) -> Result<(), ViewerError> {
// Check process is still alive first
if let Some(_) = self.try_wait()? {
return Err(ViewerError::CommandFailed("Process has exited".to_string()));
}
// Verify viewer supports refresh
let command_path = self.command_path.clone().ok_or_else(|| {
ViewerError::RefreshNotSupported(
"Viewer was launched without refresh support".to_string(),
)
})?;
// Create command with sequence number
let seq = self.command_seq.fetch_add(1, Ordering::SeqCst);
let command = ViewerCommand::Refresh { seq, content };
// Write command atomically (temp file + rename)
let temp_path = self.temp_dir.join(format!("command_{}.tmp", seq));
let command_json = serde_json::to_string(&command)
.map_err(|e| ViewerError::SerdeError(format!("Failed to serialize command: {}", e)))?;
std::fs::write(&temp_path, &command_json)?;
std::fs::rename(&temp_path, &command_path)?;
// Wait for response
self.wait_for_response(seq, Duration::from_secs(5))
}
/// Refresh the viewer with inline HTML (convenience method).
///
/// # Example
///
/// ```no_run
/// use html_view::{ViewerOptions, ViewerWaitMode, ViewerResult};
///
/// let mut options = ViewerOptions::inline_html("<h1>Initial</h1>");
/// options.wait = ViewerWaitMode::NonBlocking;
///
/// if let ViewerResult::NonBlocking(mut handle) = html_view::open(options).unwrap() {
/// std::thread::sleep(std::time::Duration::from_secs(2));
/// handle.refresh_html("<h1>Updated!</h1>").unwrap();
/// }
/// ```
pub fn refresh_html<S: Into<String>>(&mut self, html: S) -> Result<(), ViewerError> {
self.refresh(ViewerContent::InlineHtml {
html: html.into(),
base_dir: None,
})
}
fn wait_for_response(&mut self, seq: u64, timeout: Duration) -> Result<(), ViewerError> {
let response_path = self.response_path.as_ref().ok_or_else(|| {
ViewerError::RefreshNotSupported("No response path configured".to_string())
})?;
// Exponential backoff parameters
const MAX_ATTEMPTS: u32 = 50;
const INITIAL_DELAY_MS: u64 = 10;
const MAX_DELAY_MS: u64 = 100;
let start_time = std::time::Instant::now();
let mut delay_ms = INITIAL_DELAY_MS;
for _attempt in 0..MAX_ATTEMPTS {
// Check if we've exceeded the timeout
if start_time.elapsed() >= timeout {
return Err(ViewerError::CommandTimeout {
seq,
timeout_secs: timeout.as_secs(),
});
}
// Bail out early if the viewer process has already exited.
if let Some(_status) = self.child.try_wait()? {
// Try to surface the exit reason if available.
let message = match self.read_result_file() {
Ok(status) => format!(
"viewer exited ({:?}) while waiting for command response",
status.reason
),
Err(_) => {
"viewer process exited while waiting for command response".to_string()
}
};
return Err(ViewerError::CommandFailed(message));
}
// Try to read response file
if let Ok(data) = std::fs::read_to_string(response_path) {
match serde_json::from_str::<ViewerCommandResponse>(&data) {
Ok(response) if response.seq == seq => {
if response.success {
return Ok(());
} else {
return Err(ViewerError::CommandFailed(
response
.error
.unwrap_or_else(|| "Unknown error".to_string()),
));
}
}
_ => {
// Wrong sequence number or parse error, keep waiting
}
}
}
// Wait before retrying
std::thread::sleep(Duration::from_millis(delay_ms));
// Exponential backoff with cap
delay_ms = (delay_ms * 2).min(MAX_DELAY_MS);
}
Err(ViewerError::CommandTimeout {
seq,
timeout_secs: timeout.as_secs(),
})
}
/// Read and parse the result file with exponential backoff.
fn read_result_file(&self) -> Result<ViewerExitStatus, ViewerError> {
// Exponential backoff parameters
const MAX_ATTEMPTS: u32 = 10;
const INITIAL_DELAY_MS: u64 = 10;
const MAX_DELAY_MS: u64 = 1000;
let mut delay_ms = INITIAL_DELAY_MS;
let mut last_error = None;
let mut saw_not_found = false;
for attempt in 0..MAX_ATTEMPTS {
match std::fs::read_to_string(&self.result_path) {
Ok(data) => {
// Successfully read file, try to parse it
let status: ViewerExitStatus = serde_json::from_str(&data)
.map_err(|e| ViewerError::InvalidResponse(e.to_string()))?;
// Check version compatibility
check_version_compatibility(&status.viewer_version)?;
return Ok(status);
}
Err(e) => {
if e.kind() == std::io::ErrorKind::NotFound {
saw_not_found = true;
}
last_error = Some(e);
// If this isn't the last attempt, wait before retrying
if attempt < MAX_ATTEMPTS - 1 {
std::thread::sleep(std::time::Duration::from_millis(delay_ms));
// Exponential backoff with cap
delay_ms = (delay_ms * 2).min(MAX_DELAY_MS);
}
}
}
}
if saw_not_found {
// Assume the viewer closed cleanly if no result file was produced.
return Ok(ViewerExitStatus {
id: self.id,
reason: ViewerExitReason::ClosedByUser,
viewer_version: PROTOCOL_VERSION.to_string(),
});
}
// All attempts failed
Err(ViewerError::ResultReadFailed(format!(
"Failed to read result file at {} after {} attempts: {}\n\
Suggestion: The viewer process may have crashed. Check system logs or run with devtools enabled.",
self.result_path.display(),
MAX_ATTEMPTS,
last_error
.map(|e| e.to_string())
.unwrap_or_else(|| "unknown error".to_string())
)))
}
}
/// Check if viewer version is compatible with library version.
fn check_version_compatibility(viewer_version: &str) -> Result<(), ViewerError> {
let library_version = PROTOCOL_VERSION;
// Parse versions (simple major.minor.patch parsing)
let parse_version = |v: &str| -> Result<(u32, u32, u32), ViewerError> {
let parts: Vec<&str> = v.split('.').collect();
if parts.len() != 3 {
return Err(ViewerError::InvalidResponse(format!(
"Invalid version format: {}",
v
)));
}
let major = parts[0].parse::<u32>().map_err(|_| {
ViewerError::InvalidResponse(format!("Invalid major version: {}", parts[0]))
})?;
let minor = parts[1].parse::<u32>().map_err(|_| {
ViewerError::InvalidResponse(format!("Invalid minor version: {}", parts[1]))
})?;
let patch = parts[2].parse::<u32>().map_err(|_| {
ViewerError::InvalidResponse(format!("Invalid patch version: {}", parts[2]))
})?;
Ok((major, minor, patch))
};
let (lib_major, lib_minor, _lib_patch) = parse_version(library_version)?;
let (viewer_major, viewer_minor, _viewer_patch) = parse_version(viewer_version)?;
// Check for version 0.0.0 (old viewer that doesn't report version)
if viewer_major == 0 && viewer_minor == 0 {
return Err(ViewerError::VersionMismatch {
library: library_version.to_string(),
viewer: viewer_version.to_string(),
suggestion: "Your html_view_app binary is outdated and doesn't report its version.\n\
Please update it with: cargo install html_view_app --force"
.to_string(),
});
}
// Major version must match (breaking changes)
if lib_major != viewer_major {
let suggestion = if lib_major > viewer_major {
format!(
"Your html_view_app binary is too old.\n\
Please update it with: cargo install html_view_app --version {}.{}.0 --force",
lib_major, lib_minor
)
} else {
format!(
"Your html_view_app binary is too new.\n\
Either downgrade the viewer or update the html_view library to version {}.{}.x",
viewer_major, viewer_minor
)
};
return Err(ViewerError::VersionMismatch {
library: library_version.to_string(),
viewer: viewer_version.to_string(),
suggestion,
});
}
// For major version 0, minor version must also match (unstable API)
if lib_major == 0 && lib_minor != viewer_minor {
let suggestion = if lib_minor > viewer_minor {
format!(
"Your html_view_app binary is too old for this pre-1.0 library.\n\
Please update it with: cargo install html_view_app --version 0.{}.0 --force",
lib_minor
)
} else {
format!(
"Your html_view_app binary is too new for this pre-1.0 library.\n\
Either downgrade the viewer or update the html_view library to version 0.{}.x",
viewer_minor
)
};
return Err(ViewerError::VersionMismatch {
library: library_version.to_string(),
viewer: viewer_version.to_string(),
suggestion,
});
}
Ok(())
}
impl Drop for ViewerHandle {
fn drop(&mut self) {
// Best effort cleanup - ignore errors
let _ = std::fs::remove_dir_all(&self.temp_dir);
}
}