Skip to content

Commit

Permalink
improve js api
Browse files Browse the repository at this point in the history
  • Loading branch information
scottbedard committed Feb 4, 2024
1 parent 08034fb commit 2047f2f
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 56 deletions.
55 changes: 29 additions & 26 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,55 +24,58 @@ extern "C" {
fn alert(s: &str);
}

/// Execute hexchess notation
#[wasm_bindgen]
pub fn apply(hexchess: Hexchess, notation: Notation) -> Hexchess {
/// Execute notation on hexchess object
#[wasm_bindgen(js_name = applyNotation)]
pub fn apply_notation(hexchess: Hexchess, notation: Notation) -> Hexchess {
set_panic_hook();

let mut output = hexchess.clone();

let _ = output.apply(notation);

output
}

/// Parse algebraic hexchess notation
#[wasm_bindgen]
pub fn notation(str: String) -> Notation {
/// Create empty hexchess object
#[wasm_bindgen(js_name = createHexchess)]
pub fn create_hexchess() -> Hexchess {
set_panic_hook();

Notation::from(&str).unwrap()
Hexchess::new()
}

/// Parse hexchess FEN string
#[wasm_bindgen]
pub fn parse(fen: String) -> Hexchess {
/// Create hexchess object with initial position
#[wasm_bindgen(js_name = createHexchessInitial)]
pub fn create_hexchess_initial() -> Hexchess {
set_panic_hook();
Hexchess::initial()
}

let hexchess = Hexchess::from(&fen).unwrap();
/// Create hexchess object from string
#[wasm_bindgen(js_name = parseHexchess)]
pub fn parse_hexchess(fen: String) -> Hexchess {
set_panic_hook();
Hexchess::from(&fen).unwrap()
}

hexchess
/// Create hexchess notation object from string
#[wasm_bindgen(js_name = parseNotation)]
pub fn parse_notation(str: String) -> Notation {
set_panic_hook();
Notation::from(&str).unwrap()
}

/// Stringify hexchess object
#[wasm_bindgen]
pub fn stringify(hexchess: Hexchess) -> String {
#[wasm_bindgen(js_name = stringifyHexchess)]
pub fn stringify_hexchess(hexchess: Hexchess) -> String {
set_panic_hook();

hexchess.to_string()
}

/// Find target moves from a position, regardless of turn color
#[wasm_bindgen(skip_typescript)]
pub fn targets(hexchess: Hexchess, position: Position) -> JsValue {
#[wasm_bindgen(js_name = getTargets, skip_typescript)]
pub fn get_targets(hexchess: Hexchess, position: Position) -> JsValue {
set_panic_hook();

let targets = hexchess.targets(position);

JsValue::from_serde(&targets).unwrap()
JsValue::from_serde(&hexchess.targets(position)).unwrap()
}

#[wasm_bindgen(typescript_custom_section)]
const TS_APPEND_CONTENT: &'static str = r#"
export function targets(hexchess: Hexchess, position: Position): Notation[];
export function getTargets(hexchess: Hexchess, position: Position): Notation[];
"#;
13 changes: 0 additions & 13 deletions tests/apply.spec.ts

This file was deleted.

17 changes: 17 additions & 0 deletions tests/notation.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {
applyNotation,
parseHexchess,
parseNotation,
} from '@bedard/hexchess'

import { describe, expect, it } from 'vitest'

describe('apply', () => {
it('executes turn notation', () => {
let hexchess = parseHexchess('b/qbk/n1b1n/r5r/ppppppppp/11/5P5/4P1P4/3P1B1P3/2P2B2P2/1PRNQBKNRP1')

hexchess = applyNotation(hexchess, parseNotation('g4g5'))

expect(hexchess.board.g5).toBe('P')
})
})
21 changes: 10 additions & 11 deletions tests/serialization.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { assertType, describe, expect, it } from 'vitest'
import { Hexchess, Notation, notation, parse, stringify } from '@bedard/hexchess'
import { describe, expect, it } from 'vitest'

import {
parseHexchess,
parseNotation,
stringifyHexchess,
} from '@bedard/hexchess'

describe('serialization', () => {
it('parse / stringify hexchess', () => {
const str = 'b/qbk/n1b1n/r5r/ppppppppp/11/5P5/4P1P4/3P1B1P3/2P2B2P2/1PRNQBKNRP1 w - 0 1'

const hexchess = parse(str)

assertType<Hexchess>(hexchess)
const hexchess = parseHexchess(str)

expect(hexchess).toEqual({
board: {
Expand Down Expand Up @@ -109,15 +112,11 @@ describe('serialization', () => {
turn: 'w'
})

expect(stringify(hexchess)).toBe(str)
expect(stringifyHexchess(hexchess)).toBe(str)
})

it('parse notation', () => {
const n = notation('g4g5')

assertType<Notation>(n)

expect(notation('g4g5')).toEqual({
expect(parseNotation('g4g5')).toEqual({
from: 'g4',
promotion: null,
to: 'g5',
Expand Down
6 changes: 3 additions & 3 deletions tests/targets.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { describe, expect, it } from 'vitest'
import { parse, targets } from '@bedard/hexchess'
import { getTargets, parseHexchess } from '@bedard/hexchess'

describe('targets', () => {
it('targets', () => {
const hexchess = parse('b/qbk/n1b1n/r5r/ppppppppp/11/5P5/4P1P4/3P1B1P3/2P2B2P2/1PRNQBKNRP1 w - 0 1')
const hexchess = parseHexchess('b/qbk/n1b1n/r5r/ppppppppp/11/5P5/4P1P4/3P1B1P3/2P2B2P2/1PRNQBKNRP1 w - 0 1')

const result = targets(hexchess, 'g4')
const result = getTargets(hexchess, 'g4')

expect(result).toEqual([
{ from: 'g4', promotion: null, to: 'g5' },
Expand Down
6 changes: 3 additions & 3 deletions tests/types.spec-d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { assertType, describe, it } from 'vitest'
import { Hexchess, Notation, notation, parse } from '@bedard/hexchess'
import { Hexchess, Notation, parseNotation, parseHexchess } from '@bedard/hexchess'

describe('types', () => {
it('Hexchess', () => {
assertType<Hexchess>(parse('b/qbk/n1b1n/r5r/ppppppppp/11/5P5/4P1P4/3P1B1P3/2P2B2P2/1PRNQBKNRP1'))
assertType<Hexchess>(parseHexchess('b/qbk/n1b1n/r5r/ppppppppp/11/5P5/4P1P4/3P1B1P3/2P2B2P2/1PRNQBKNRP1'))
})

it('Notation', () => {
assertType<Notation>(notation('e4e5'))
assertType<Notation>(parseNotation('e4e5'))
})
})

0 comments on commit 2047f2f

Please sign in to comment.