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
//! Functions for managing engine communications.

use wasm_bindgen::prelude::*;

#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(test))]
pub mod conn;
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(test))]
pub use conn::EngineConnection;

#[cfg(target_arch = "wasm32")]
#[cfg(not(test))]
pub mod conn_wasm;
#[cfg(target_arch = "wasm32")]
#[cfg(not(test))]
pub use conn_wasm::EngineConnection;

#[cfg(test)]
pub mod conn_mock;
#[cfg(test)]
pub use conn_mock::EngineConnection;

use crate::executor::SourceRange;

#[derive(Debug)]
#[wasm_bindgen]
pub struct EngineManager {
    connection: EngineConnection,
}

#[wasm_bindgen]
impl EngineManager {
    #[cfg(target_arch = "wasm32")]
    #[cfg(not(test))]
    #[wasm_bindgen(constructor)]
    pub async fn new(manager: conn_wasm::EngineCommandManager) -> EngineManager {
        EngineManager {
            // This unwrap is safe because the connection is always created.
            connection: EngineConnection::new(manager).await.unwrap(),
        }
    }

    pub fn send_modeling_cmd(&mut self, id_str: &str, cmd_str: &str) -> Result<(), String> {
        let id = uuid::Uuid::parse_str(id_str).map_err(|e| e.to_string())?;
        let cmd = serde_json::from_str(cmd_str).map_err(|e| e.to_string())?;
        self.connection
            .send_modeling_cmd(id, SourceRange::default(), cmd)
            .map_err(String::from)?;

        Ok(())
    }
}