From 06a21bcef7860abe89abb39f4d8e8aabab96cd5d Mon Sep 17 00:00:00 2001 From: "Christoph J. Scherr" Date: Thu, 25 Jul 2024 17:06:57 +0200 Subject: [PATCH] refactor(error): change where some error types are in the error tree --- src/error.rs | 15 +++++++-------- src/solve/mod.rs | 4 ++-- src/wlist/mod.rs | 4 ++-- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/error.rs b/src/error.rs index e2fac23..a0ddbc8 100644 --- a/src/error.rs +++ b/src/error.rs @@ -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, + }, } diff --git a/src/solve/mod.rs b/src/solve/mod.rs index 9e34dbc..e87a48c 100644 --- a/src/solve/mod.rs +++ b/src/solve/mod.rs @@ -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 { match s.to_lowercase().as_str() { "naive" => Ok(Self::Naive), diff --git a/src/wlist/mod.rs b/src/wlist/mod.rs index 214c082..fb36b70 100644 --- a/src/wlist/mod.rs +++ b/src/wlist/mod.rs @@ -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; @@ -89,7 +89,7 @@ pub trait WordList: Clone + std::fmt::Debug + Default + Sync + Display { buf } fn get_words_matching(&self, pattern: String) -> WResult> { - 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();