pub mod constants;
pub mod hexchess;
pub mod macros;
pub use hexchess::hexchess::Hexchess;
pub use hexchess::san::San;
use constants::Color;
use wasm_bindgen::prelude::*;
fn set_panic_hook() {
#[cfg(feature = "console_error_panic_hook")]
console_error_panic_hook::set_once();
}
#[wasm_bindgen(js_name = apply)]
pub fn apply(hexchess: Hexchess, sequence: String) -> Hexchess {
set_panic_hook();
let mut clone = hexchess.clone();
match clone.apply(sequence.as_str()) {
Ok(_) => clone,
Err(err) => panic!("hexchess error: {:?}", err),
}
}
#[wasm_bindgen(js_name = applyMove)]
pub fn apply_move(hexchess: Hexchess, san: San) -> Hexchess {
set_panic_hook();
let mut clone = hexchess.clone();
match clone.apply_move(&san) {
Ok(_) => clone,
Err(err) => panic!("hexchess error: {:?}", err),
}
}
#[wasm_bindgen(js_name = applyMoveUnsafe)]
pub fn apply_move_unsafe(hexchess: Hexchess, san: San) -> Hexchess {
set_panic_hook();
*hexchess.clone().apply_move_unsafe(&san)
}
#[wasm_bindgen(js_name = createHexchess)]
pub fn create_hexchess() -> Hexchess {
set_panic_hook();
Hexchess::new()
}
#[wasm_bindgen(js_name = currentMoves)]
pub fn current_moves(hexchess: Hexchess) -> Vec<San> {
set_panic_hook();
hexchess.current_moves()
}
#[wasm_bindgen(js_name = initHexchess)]
pub fn init_hexchess() -> Hexchess {
set_panic_hook();
Hexchess::init()
}
#[wasm_bindgen(js_name = findKing, skip_typescript)]
pub fn find_king(hexchess: Hexchess, color: &str) -> JsValue {
set_panic_hook();
match color {
"w" => JsValue::from(hexchess.find_king(Color::White)),
"b" => JsValue::from(hexchess.find_king(Color::Black)),
_ => panic!("invalid color: {}", color),
}
}
#[wasm_bindgen(js_name = get)]
pub fn get(hexchess: Hexchess, position: String) -> JsValue {
set_panic_hook();
match hexchess.get(position.as_str()) {
Some(piece) => JsValue::from_str(&piece.to_string()),
None => JsValue::NULL,
}
}
#[wasm_bindgen(js_name = isCheck)]
pub fn is_check(hexchess: Hexchess) -> bool {
set_panic_hook();
hexchess.is_check()
}
#[wasm_bindgen(js_name = isCheckmate)]
pub fn is_checkmate(hexchess: Hexchess) -> bool {
set_panic_hook();
hexchess.is_checkmate()
}
#[wasm_bindgen(js_name = isStalemate)]
pub fn is_stalemate(hexchess: Hexchess) -> bool {
set_panic_hook();
hexchess.is_stalemate()
}
#[wasm_bindgen(js_name = movesFrom)]
pub fn moves_from(hexchess: Hexchess, position: u8) -> Vec<San> {
set_panic_hook();
hexchess.moves_from(position)
}
#[wasm_bindgen(js_name = movesFromUnsafe)]
pub fn moves_from_unsafe(hexchess: Hexchess, position: u8) -> Vec<San> {
set_panic_hook();
hexchess.moves_from_unsafe(position)
}
#[wasm_bindgen(js_name = parseHexchess)]
pub fn parse_hexchess(source: String) -> Hexchess {
set_panic_hook();
match Hexchess::parse(source.as_str()) {
Ok(hexchess) => hexchess,
Err(err) => panic!("hexchess error: {:?}", err),
}
}
#[wasm_bindgen(js_name = parseSan)]
pub fn parse_san(source: String) -> San {
set_panic_hook();
match San::from(source.as_str()) {
Ok(san) => san,
Err(err) => panic!("hexchess error: {:?}", err),
}
}
#[wasm_bindgen(js_name = stringifyHexchess)]
pub fn stringify_hexchess(hexchess: Hexchess) -> String {
set_panic_hook();
hexchess.to_string()
}
#[wasm_bindgen(js_name = stringifySan)]
pub fn stringify_san(san: San) -> String {
set_panic_hook();
san.to_string()
}