Use separate result type for WASM REPL init and input
@@ -617,7 +617,7 @@ pub mod simple_frontend {
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
- pub enum WASMInputResultTag {
+ pub enum WASMResultTag {
Success = 0,
Blank = 1,
Partial = 2,
@@ -625,38 +625,57 @@ pub mod simple_frontend {
}
#[wasm_bindgen]
- pub struct WASMInputResult {
+ pub struct WASMInitResult {
msg: String,
- tag: WASMInputResultTag,
+ tag: WASMResultTag,
state: REPLState,
}
+ impl WASMInitResult {
+ fn mk_error(msg: String) -> WASMInitResult {
+ WASMInitResult {
+ msg,
+ tag: WASMResultTag::Error,
+ state: REPLState(REPLImpl::new()),
+ }
+ }
+ }
+
+ #[wasm_bindgen]
+ pub struct WASMInputResult {
+ msg: String,
+ tag: WASMResultTag,
+ }
+
+ impl WASMInputResult {
+ fn mk_error(msg: String) -> WASMInputResult {
+ WASMInputResult {
+ msg,
+ tag: WASMResultTag::Error,
+ }
+ }
+ }
+
pub enum InputResult {
Success(String),
Blank,
Partial,
}
- impl WASMInputResult {
- fn from_input_result(repl: REPLState, ir: InputResult) -> Self {
+ impl From<InputResult> for WASMInputResult {
+ fn from(ir: InputResult) -> Self {
match ir {
- InputResult::Success(msg) =>
- WASMInputResult {
+ InputResult::Success(msg) => WASMInputResult {
msg,
- tag: WASMInputResultTag::Success,
- state: repl,
+ tag: WASMResultTag::Success,
},
- InputResult::Blank =>
- WASMInputResult {
+ InputResult::Blank => WASMInputResult {
msg: String::new(),
- tag: WASMInputResultTag::Blank,
- state: repl,
+ tag: WASMResultTag::Blank,
},
- InputResult::Partial =>
- WASMInputResult {
+ InputResult::Partial => WASMInputResult {
msg: String::new(),
- tag: WASMInputResultTag::Partial,
- state: repl,
+ tag: WASMResultTag::Partial,
},
}
}
@@ -665,31 +684,22 @@ pub mod simple_frontend {
#[wasm_bindgen]
pub struct REPLState(REPLImpl);
- fn mk_err(msg: String) -> WASMInputResult {
- WASMInputResult {
- msg,
- tag: WASMInputResultTag::Error,
- state: REPLState(REPLImpl::new()),
- }
- }
-
#[wasm_bindgen]
- pub fn wasm_init() -> WASMInputResult {
+ pub fn wasm_init() -> WASMInitResult {
init()
- .map(|repl| WASMInputResult {
+ .map(|repl| WASMInitResult {
msg: String::new(),
- tag: WASMInputResultTag::Success,
+ tag: WASMResultTag::Success,
state: REPLState(repl),
})
- .unwrap_or_else(mk_err)
+ .unwrap_or_else(WASMInitResult::mk_error)
}
#[wasm_bindgen]
pub fn wasm_input(mut state: REPLState, line: &str) -> WASMInputResult {
- match input(&mut state.0, line) {
- Ok(result) => WASMInputResult::from_input_result(state, result),
- Err(err) => mk_err(err)
- }
+ input(&mut state.0, line)
+ .map(WASMInputResult::from)
+ .unwrap_or_else(WASMInputResult::mk_error)
}
pub fn init() -> Result<REPLImpl, String> {
