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] #[from]
source: anyhow::Error, 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")] #[error("Error sharing the benchmark data over multiple threads")]
Mutex { Mutex {
#[from] #[from]
@ -72,6 +64,8 @@ pub enum BenchError {
pub enum SolverError { pub enum SolverError {
#[error("Wordlist has no matches for the gamestate")] #[error("Wordlist has no matches for the gamestate")]
NoMatches, NoMatches,
#[error("Unknown builtin solver")]
UnknownBuiltinSolver,
} }
#[derive(Debug, Error)] #[derive(Debug, Error)]
@ -86,4 +80,9 @@ pub enum WordlistError {
#[from] #[from]
source: std::io::Error, 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 std::{fmt::Display, str::FromStr};
use crate::{ use crate::{
error::{Error, WResult}, error::{Error, SolverError, WResult},
game::{response::*, Game}, game::{response::*, Game},
wlist::{ wlist::{
word::{Word, WordData}, word::{Word, WordData},
@ -127,7 +127,7 @@ impl BuiltinSolverNames {
} }
impl FromStr for BuiltinSolverNames { impl FromStr for BuiltinSolverNames {
type Err = Error; type Err = SolverError;
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() { match s.to_lowercase().as_str() {
"naive" => Ok(Self::Naive), "naive" => Ok(Self::Naive),

View File

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