refactor(error): change where some error types are in the error tree

This commit is contained in:
Christoph J. Scherr 2024-07-25 17:06:57 +02:00
parent 9ea3577a3b
commit 06a21bcef7
3 changed files with 11 additions and 12 deletions

View File

@ -33,14 +33,6 @@ pub enum Error {
#[from]
source: anyhow::Error,
},
// for `FromStr` of `BuiltinSolver`
#[error("Unknown builtin solver")]
UnknownBuiltinSolver,
#[error("pattern matching error")]
Regex {
#[from]
source: regex::Error,
},
#[error("Error sharing the benchmark data over multiple threads")]
Mutex {
#[from]
@ -72,6 +64,8 @@ pub enum BenchError {
pub enum SolverError {
#[error("Wordlist has no matches for the gamestate")]
NoMatches,
#[error("Unknown builtin solver")]
UnknownBuiltinSolver,
}
#[derive(Debug, Error)]
@ -86,4 +80,9 @@ pub enum WordlistError {
#[from]
source: std::io::Error,
},
#[error("pattern matching error")]
Regex {
#[from]
source: regex::Error,
},
}

View File

@ -1,7 +1,7 @@
use std::{fmt::Display, str::FromStr};
use crate::{
error::{Error, WResult},
error::{Error, SolverError, WResult},
game::{response::*, Game},
wlist::{
word::{Word, WordData},
@ -127,7 +127,7 @@ impl BuiltinSolverNames {
}
impl FromStr for BuiltinSolverNames {
type Err = Error;
type Err = SolverError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"naive" => Ok(Self::Naive),

View File

@ -11,7 +11,7 @@ pub mod builtin;
pub mod word;
use word::*;
use crate::error::WResult;
use crate::error::{Error, WResult, WordlistError};
pub type AnyWordlist = Box<dyn WordList>;
@ -89,7 +89,7 @@ pub trait WordList: Clone + std::fmt::Debug + Default + Sync + Display {
buf
}
fn get_words_matching(&self, pattern: String) -> WResult<Vec<WordData>> {
let pattern = Regex::new(&pattern)?;
let pattern = Regex::new(&pattern).map_err(WordlistError::from)?;
let hay = self.raw_wordlist();
let keys = pattern.captures_iter(&hay);
let mut buf = Vec::new();