wordle_solver/
lib.rs

1//! # wordle_solver
2//!
3//! Wordle solver is a small library as well as an example utility (`wordle-solver`)
4//! that helps you win the daily wordle ( <https://www.powerlanguage.co.uk/wordle/> )
5//!
6//! See the solver module and the Solver struct
7//!
8
9pub use solver::*;
10
11pub mod solver;
12
13use assets_manager::{
14    loader::{LoadFrom, StringLoader},
15    Asset, AssetCache,
16};
17
18use regex::Regex;
19use std::collections::HashSet;
20use std::error::Error;
21
22// The wrapper around the whole string dictionary, with a From trait
23// to be able to load it from an asset
24struct AssetString(String);
25impl From<String> for AssetString {
26    fn from(s: String) -> AssetString {
27        AssetString(s)
28    }
29}
30impl Asset for AssetString {
31    const EXTENSION: &'static str = "txt";
32    type Loader = LoadFrom<String, StringLoader>;
33}
34
35pub fn load_words(cache: &AssetCache, fname: &str, size: u8) -> Result<HashSet<String>, Box<dyn Error>> {
36    let bd = cache.load::<AssetString>(fname)?.read();
37    let s = &bd.0;
38
39    let regex_string = format!("^[[:alpha:]]{{{}}}$", size);
40    let nchars: Regex = Regex::new(&regex_string).unwrap();
41
42    // The only interesting words are 5 characters, and all letters.
43    let words: HashSet<String> = s
44        .lines()
45        .map(|rs| rs.to_uppercase()) // turn all refs into a string.
46        .filter(|rs| nchars.is_match(rs))
47        .collect();
48    Ok(words)
49}