Skip to main content

dada_debug/
lib.rs

1use std::sync::mpsc::Sender;
2
3use dada_ir_ast::DebugEvent;
4use structopt::StructOpt;
5
6mod assets;
7mod error;
8mod events;
9mod hbs;
10mod root;
11mod server;
12mod source;
13mod view;
14
15/// Command line options for the debug server
16#[derive(Debug, StructOpt)]
17pub struct DebugOptions {
18    #[structopt(long, default_value = "2222")]
19    pub port: u32,
20}
21
22impl DebugOptions {
23    /// Create a debug server from the options
24    pub fn to_server(&self) -> DebugServer {
25        DebugServer {
26            port: self.port,
27            thread: None,
28        }
29    }
30}
31
32/// Debug server that monitors
33pub struct DebugServer {
34    port: u32,
35    thread: Option<std::thread::JoinHandle<anyhow::Result<()>>>,
36}
37
38impl DebugServer {
39    /// Start the debug server, panicking if already launched.
40    ///
41    /// Returns a port where you should send debug events.
42    pub fn launch(&mut self) -> Sender<DebugEvent> {
43        assert!(self.thread.is_none());
44        let (debug_tx, debug_rx) = std::sync::mpsc::channel();
45        let port = self.port;
46        self.thread = Some(std::thread::spawn(move || server::main(port, debug_rx)));
47        debug_tx
48    }
49
50    /// Block on the debug server thread (if it has been launched)
51    pub fn block_on(self) -> anyhow::Result<()> {
52        if let Some(thread) = self.thread {
53            thread.join().unwrap()?;
54        }
55        Ok(())
56    }
57}