feat: refine wasm interfacing

- allow loading wordlist from internal data
- no unwraps in wasm branches
This commit is contained in:
2026-08-20 12:23:38 +02:00
parent c6e8849b28
commit ffc5b9d38e
5 changed files with 89 additions and 50 deletions
+48 -13
View File
@@ -1,25 +1,60 @@
use std::io;
use wasm_bindgen::prelude::*;
use crate::{solver, word_list::WordList};
use crate::{
pattern::{CharStatus, Pattern},
solver,
word_list::WordList,
};
#[wasm_bindgen]
extern "C" {
fn alert(s: &str);
}
#[wasm_bindgen]
pub fn greet() {
alert("Hello, wasm-on-web!");
}
static DEFAULT_VALID_WORDS: &str = include_str!("../../valid-words.txt");
static DEFAULT_ANSWERS: &str = include_str!("../../answers.txt");
#[wasm_bindgen]
struct Solver(solver::Solver);
#[wasm_bindgen]
pub enum Color {
GREY,
YELLOW,
GREEN,
}
#[wasm_bindgen]
extern "C" {
pub fn alert(msg: &str);
}
#[allow(dead_code)]
#[wasm_bindgen]
impl Solver {
pub fn new() -> Solver {
let wl = WordList::from(["test", "yooo"].iter());
Solver(solver::Solver::from_single_word_list(wl))
pub fn new() -> Result<Solver, String> {
let valid_words =
WordList::try_from(io::BufReader::new(io::Cursor::new(DEFAULT_VALID_WORDS)))?;
let answers = WordList::try_from(io::BufReader::new(io::Cursor::new(DEFAULT_ANSWERS)))?;
Ok(Solver(solver::Solver::new(valid_words, answers)))
}
pub fn guess(&mut self, word: &str, colors: Vec<Color>) -> Result<(), String> {
let pat = Pattern::try_new(
word,
colors
.iter()
.map(|c| match c {
Color::GREY => CharStatus::GREY,
Color::YELLOW => CharStatus::YELLOW,
Color::GREEN => CharStatus::GREEN,
})
.collect(),
)?;
alert(&format!("{:?}", pat));
self.0.apply_guess(pat)?;
Ok(())
}
pub fn stats(&self) -> String {