diff --git a/query/src/optimizer.rs b/query/src/optimizer.rs index 9cd1ce1..9e9fa0a 100644 --- a/query/src/optimizer.rs +++ b/query/src/optimizer.rs @@ -2,15 +2,18 @@ use chrono::prelude::*; -use chrono_english::{parse_date_string, Dialect}; +use chrono_english::parse_date_string; +use chrono_english::Dialect; use regex::escape; use regex::RegexBuilder; use fimfareader::archive::Story; -use fimfareader::error::{Error, Result}; +use fimfareader::error::Error; +use fimfareader::error::Result; -use super::parser::{Operator, Source}; +use super::parser::Operator; +use super::parser::Source; use Operator::*; use Source::*; @@ -43,9 +46,9 @@ fn strfn(f: StrFn, op: Operator, value: &str) -> Result { .size_limit(1_048_576) .build(); - let regex = result.map_err(|e| match e { - _ => Error::query("Invalid value for fuzzy match"), - })?; + let Ok(regex) = result else { + return Err(Error::query("Invalid value for fuzzy match")); + }; match op { Exact => ok!(move |s| f(s) == exact), @@ -55,9 +58,9 @@ fn strfn(f: StrFn, op: Operator, value: &str) -> Result { } fn intfn(f: IntFn, op: Operator, value: &str) -> Result { - let value: i64 = value.parse().map_err(|e| match e { - _ => Error::query("Invalid value for number type"), - })?; + let Ok(value) = value.parse() else { + return Err(Error::query("Invalid value for number type")); + }; match op { Exact => ok!(move |s| f(s) == value), @@ -68,11 +71,9 @@ fn intfn(f: IntFn, op: Operator, value: &str) -> Result { } fn dtufn(f: DtuFn, op: Operator, value: &str) -> Result { - let parsed = parse_date_string(value, Utc::now(), Dialect::Uk); - - let value: DateTime = parsed.map_err(|e| match e { - _ => Error::query("Invalid value for date type"), - })?; + let Ok(value) = parse_date_string(value, Utc::now(), Dialect::Uk) else { + return Err(Error::query("Invalid value for date type")); + }; match op { Exact => ok!(move |s| match f(s) { diff --git a/query/src/parser.rs b/query/src/parser.rs index 8b358c8..27ef887 100644 --- a/query/src/parser.rs +++ b/query/src/parser.rs @@ -110,9 +110,9 @@ fn item(input: &str) -> IResult<&str, Filter> { let result = tuple((source, operator, target))(input)?; let (left, (src, op, value)) = result; - let filter = optimize(src, op, &value).map_err(|e| match e { - _ => Err::Failure((input, NomErrorKind::Permutation)), - })?; + let Ok(filter) = optimize(src, op, &value) else { + return Err(Err::Failure((input, NomErrorKind::Permutation))); + }; Ok((left, filter)) } diff --git a/src/archive/fetcher.rs b/src/archive/fetcher.rs index fc014c5..508143e 100644 --- a/src/archive/fetcher.rs +++ b/src/archive/fetcher.rs @@ -1,8 +1,10 @@ //! Archive fetcher. use std::fs::File; +use std::io::BufReader; use std::io::ErrorKind as IoErrorKind; -use std::io::{BufReader, Read, Seek}; +use std::io::Read; +use std::io::Seek; use std::path::Path; use std::sync::Mutex; @@ -13,7 +15,8 @@ use zip::result::ZipError; use super::parser::parse; use super::story::Story; -use crate::error::{Error, Result}; +use crate::error::Error; +use crate::error::Result; pub struct Fetcher { archive: Mutex>, @@ -75,9 +78,9 @@ impl Fetcher { let path = &story.archive.path; - let mut archive = self.archive.lock().map_err(|e| match e { - _ => Error::archive("Could not acquire fetcher lock"), - })?; + let Ok(mut archive) = self.archive.lock() else { + return Err(Error::archive("Could not acquire fetcher lock")); + }; let mut file = archive.by_name(path).map_err(|e| match e { FileNotFound => Error::archive("Missing story data"), @@ -87,9 +90,9 @@ impl Fetcher { let size = file.size() as usize; let mut buf = Vec::with_capacity(size); - file.read_to_end(&mut buf).map_err(|e| match e { - _ => Error::archive("Could not read story data"), - })?; + let Ok(_) = file.read_to_end(&mut buf) else { + return Err(Error::archive("Could not read story data")); + }; Ok(buf) } diff --git a/src/archive/interner.rs b/src/archive/interner.rs index 42020bc..3bc0424 100644 --- a/src/archive/interner.rs +++ b/src/archive/interner.rs @@ -16,7 +16,7 @@ where fn get(&self, value: &T) -> Option<&'static T> { let store = self.0.read().unwrap(); - store.get(value).map(|value| *value) + store.get(value).copied() } fn set(&self, value: T) -> &'static T { diff --git a/src/archive/parser.rs b/src/archive/parser.rs index cad15ad..72602dc 100644 --- a/src/archive/parser.rs +++ b/src/archive/parser.rs @@ -1,7 +1,8 @@ //! Index parser. use std::io::BufRead; -use std::sync::mpsc::{channel, Receiver}; +use std::sync::mpsc::channel; +use std::sync::mpsc::Receiver; use std::thread::spawn; use rayon::prelude::*; @@ -20,9 +21,9 @@ pub fn parse(reader: impl BufRead) -> Result> { let rx = spawn_parser(rx); for line in reader.lines() { - let line = line.map_err(|e| match e { - _ => Error::custom("Could not read line"), - })?; + let Ok(line) = line else { + return Err(Error::custom("Could not read line")); + }; if line.len() == 1 { wrappers.push_str(&line); @@ -46,9 +47,11 @@ pub fn parse(reader: impl BufRead) -> Result> { return Err(Error::custom("Invalid file structure")); } - rx.recv().map_err(|e| match e { - _ => Error::custom("Missing parser result"), - })? + let Ok(result) = rx.recv() else { + return Err(Error::custom("Missing parser result")); + }; + + result } fn spawn_parser(stream: Receiver) -> Receiver>> { @@ -92,9 +95,9 @@ fn deserialize(line: String) -> Result { let story: Story = from_str(json)?; - let key: i64 = skey.parse().map_err(|e| match e { - _ => Error::custom("Invalid line key"), - })?; + let Ok(key) = skey.parse::() else { + return Err(Error::custom("Invalid line key")); + }; if key != story.id { return Err(Error::custom("Line key mismatch")); diff --git a/src/archive/story.rs b/src/archive/story.rs index 66737cd..c7811ee 100644 --- a/src/archive/story.rs +++ b/src/archive/story.rs @@ -3,7 +3,8 @@ use chrono::prelude::*; use lazy_static::lazy_static; use serde::de::Error; -use serde::{Deserialize, Deserializer}; +use serde::Deserialize; +use serde::Deserializer; use serde_json::Value; use super::interner::Interner; @@ -184,16 +185,16 @@ fn string_to_id<'de, D>(d: D) -> Result where D: Deserializer<'de>, { - let value = Value::deserialize(d)?; - - if value.is_i64() { - Ok(value.as_i64().unwrap()) - } else if value.is_string() { - value.as_str().unwrap().parse().map_err(|e| match e { - _ => Error::custom("Could not parse ID string"), - }) - } else { - Err(Error::custom("Invalid type for ID value")) + match Value::deserialize(d)? { + Value::Number(value) => match value.as_i64() { + Some(value) => Ok(value), + _ => Err(Error::custom("Could not parse ID number")), + }, + Value::String(value) => match value.parse::() { + Ok(value) => Ok(value), + _ => Err(Error::custom("Could not parse ID string")), + }, + _ => Err(Error::custom("Invalid type for ID value")), } } @@ -218,9 +219,9 @@ impl<'de> Deserialize<'de> for Color { .and_then(|value| value.as_str()) .ok_or_else(|| Error::custom("Color is missing hex value"))?; - let array = hex::decode(text).map_err(|e| match e { - _ => Error::custom("Color hex has invalid value"), - })?; + let Ok(array) = hex::decode(text) else { + return Err(Error::custom("Color hex has invalid value")); + }; match array[..] { [red, green, blue] => Ok(Color { red, green, blue }),