Replace match single binding with let else

This commit is contained in:
Joakim Soderlund 2022-12-20 15:16:33 +01:00
parent f99bfaf86a
commit 266da3357b
6 changed files with 58 additions and 50 deletions

View file

@ -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<Filter> {
.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<Filter> {
}
fn intfn(f: IntFn, op: Operator, value: &str) -> Result<Filter> {
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<Filter> {
}
fn dtufn(f: DtuFn, op: Operator, value: &str) -> Result<Filter> {
let parsed = parse_date_string(value, Utc::now(), Dialect::Uk);
let value: DateTime<Utc> = 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) {

View file

@ -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))
}

View file

@ -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<T: Read + Seek> {
archive: Mutex<ZipArchive<T>>,
@ -75,9 +78,9 @@ impl<T: Read + Seek> Fetcher<T> {
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<T: Read + Seek> Fetcher<T> {
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)
}

View file

@ -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 {

View file

@ -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<Vec<Story>> {
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<Vec<Story>> {
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<String>) -> Receiver<Result<Vec<Story>>> {
@ -92,9 +95,9 @@ fn deserialize(line: String) -> Result<Story> {
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::<i64>() else {
return Err(Error::custom("Invalid line key"));
};
if key != story.id {
return Err(Error::custom("Line key mismatch"));

View file

@ -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<i64, D::Error>
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::<i64>() {
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 }),