mirror of
https://github.com/JockeTF/fimfareader.git
synced 2024-11-30 08:57:59 +01:00
Replace match single binding with let else
This commit is contained in:
parent
f99bfaf86a
commit
266da3357b
6 changed files with 58 additions and 50 deletions
|
@ -2,15 +2,18 @@
|
||||||
|
|
||||||
use chrono::prelude::*;
|
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::escape;
|
||||||
use regex::RegexBuilder;
|
use regex::RegexBuilder;
|
||||||
|
|
||||||
use fimfareader::archive::Story;
|
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 Operator::*;
|
||||||
use Source::*;
|
use Source::*;
|
||||||
|
@ -43,9 +46,9 @@ fn strfn(f: StrFn, op: Operator, value: &str) -> Result<Filter> {
|
||||||
.size_limit(1_048_576)
|
.size_limit(1_048_576)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let regex = result.map_err(|e| match e {
|
let Ok(regex) = result else {
|
||||||
_ => Error::query("Invalid value for fuzzy match"),
|
return Err(Error::query("Invalid value for fuzzy match"));
|
||||||
})?;
|
};
|
||||||
|
|
||||||
match op {
|
match op {
|
||||||
Exact => ok!(move |s| f(s) == exact),
|
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> {
|
fn intfn(f: IntFn, op: Operator, value: &str) -> Result<Filter> {
|
||||||
let value: i64 = value.parse().map_err(|e| match e {
|
let Ok(value) = value.parse() else {
|
||||||
_ => Error::query("Invalid value for number type"),
|
return Err(Error::query("Invalid value for number type"));
|
||||||
})?;
|
};
|
||||||
|
|
||||||
match op {
|
match op {
|
||||||
Exact => ok!(move |s| f(s) == value),
|
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> {
|
fn dtufn(f: DtuFn, op: Operator, value: &str) -> Result<Filter> {
|
||||||
let parsed = parse_date_string(value, Utc::now(), Dialect::Uk);
|
let Ok(value) = parse_date_string(value, Utc::now(), Dialect::Uk) else {
|
||||||
|
return Err(Error::query("Invalid value for date type"));
|
||||||
let value: DateTime<Utc> = parsed.map_err(|e| match e {
|
};
|
||||||
_ => Error::query("Invalid value for date type"),
|
|
||||||
})?;
|
|
||||||
|
|
||||||
match op {
|
match op {
|
||||||
Exact => ok!(move |s| match f(s) {
|
Exact => ok!(move |s| match f(s) {
|
||||||
|
|
|
@ -110,9 +110,9 @@ fn item(input: &str) -> IResult<&str, Filter> {
|
||||||
let result = tuple((source, operator, target))(input)?;
|
let result = tuple((source, operator, target))(input)?;
|
||||||
let (left, (src, op, value)) = result;
|
let (left, (src, op, value)) = result;
|
||||||
|
|
||||||
let filter = optimize(src, op, &value).map_err(|e| match e {
|
let Ok(filter) = optimize(src, op, &value) else {
|
||||||
_ => Err::Failure((input, NomErrorKind::Permutation)),
|
return Err(Err::Failure((input, NomErrorKind::Permutation)));
|
||||||
})?;
|
};
|
||||||
|
|
||||||
Ok((left, filter))
|
Ok((left, filter))
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
//! Archive fetcher.
|
//! Archive fetcher.
|
||||||
|
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
|
use std::io::BufReader;
|
||||||
use std::io::ErrorKind as IoErrorKind;
|
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::path::Path;
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
|
|
||||||
|
@ -13,7 +15,8 @@ use zip::result::ZipError;
|
||||||
|
|
||||||
use super::parser::parse;
|
use super::parser::parse;
|
||||||
use super::story::Story;
|
use super::story::Story;
|
||||||
use crate::error::{Error, Result};
|
use crate::error::Error;
|
||||||
|
use crate::error::Result;
|
||||||
|
|
||||||
pub struct Fetcher<T: Read + Seek> {
|
pub struct Fetcher<T: Read + Seek> {
|
||||||
archive: Mutex<ZipArchive<T>>,
|
archive: Mutex<ZipArchive<T>>,
|
||||||
|
@ -75,9 +78,9 @@ impl<T: Read + Seek> Fetcher<T> {
|
||||||
|
|
||||||
let path = &story.archive.path;
|
let path = &story.archive.path;
|
||||||
|
|
||||||
let mut archive = self.archive.lock().map_err(|e| match e {
|
let Ok(mut archive) = self.archive.lock() else {
|
||||||
_ => Error::archive("Could not acquire fetcher lock"),
|
return Err(Error::archive("Could not acquire fetcher lock"));
|
||||||
})?;
|
};
|
||||||
|
|
||||||
let mut file = archive.by_name(path).map_err(|e| match e {
|
let mut file = archive.by_name(path).map_err(|e| match e {
|
||||||
FileNotFound => Error::archive("Missing story data"),
|
FileNotFound => Error::archive("Missing story data"),
|
||||||
|
@ -87,9 +90,9 @@ impl<T: Read + Seek> Fetcher<T> {
|
||||||
let size = file.size() as usize;
|
let size = file.size() as usize;
|
||||||
let mut buf = Vec::with_capacity(size);
|
let mut buf = Vec::with_capacity(size);
|
||||||
|
|
||||||
file.read_to_end(&mut buf).map_err(|e| match e {
|
let Ok(_) = file.read_to_end(&mut buf) else {
|
||||||
_ => Error::archive("Could not read story data"),
|
return Err(Error::archive("Could not read story data"));
|
||||||
})?;
|
};
|
||||||
|
|
||||||
Ok(buf)
|
Ok(buf)
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,7 @@ where
|
||||||
fn get(&self, value: &T) -> Option<&'static T> {
|
fn get(&self, value: &T) -> Option<&'static T> {
|
||||||
let store = self.0.read().unwrap();
|
let store = self.0.read().unwrap();
|
||||||
|
|
||||||
store.get(value).map(|value| *value)
|
store.get(value).copied()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set(&self, value: T) -> &'static T {
|
fn set(&self, value: T) -> &'static T {
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
//! Index parser.
|
//! Index parser.
|
||||||
|
|
||||||
use std::io::BufRead;
|
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 std::thread::spawn;
|
||||||
|
|
||||||
use rayon::prelude::*;
|
use rayon::prelude::*;
|
||||||
|
@ -20,9 +21,9 @@ pub fn parse(reader: impl BufRead) -> Result<Vec<Story>> {
|
||||||
let rx = spawn_parser(rx);
|
let rx = spawn_parser(rx);
|
||||||
|
|
||||||
for line in reader.lines() {
|
for line in reader.lines() {
|
||||||
let line = line.map_err(|e| match e {
|
let Ok(line) = line else {
|
||||||
_ => Error::custom("Could not read line"),
|
return Err(Error::custom("Could not read line"));
|
||||||
})?;
|
};
|
||||||
|
|
||||||
if line.len() == 1 {
|
if line.len() == 1 {
|
||||||
wrappers.push_str(&line);
|
wrappers.push_str(&line);
|
||||||
|
@ -46,9 +47,11 @@ pub fn parse(reader: impl BufRead) -> Result<Vec<Story>> {
|
||||||
return Err(Error::custom("Invalid file structure"));
|
return Err(Error::custom("Invalid file structure"));
|
||||||
}
|
}
|
||||||
|
|
||||||
rx.recv().map_err(|e| match e {
|
let Ok(result) = rx.recv() else {
|
||||||
_ => Error::custom("Missing parser result"),
|
return Err(Error::custom("Missing parser result"));
|
||||||
})?
|
};
|
||||||
|
|
||||||
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
fn spawn_parser(stream: Receiver<String>) -> Receiver<Result<Vec<Story>>> {
|
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 story: Story = from_str(json)?;
|
||||||
|
|
||||||
let key: i64 = skey.parse().map_err(|e| match e {
|
let Ok(key) = skey.parse::<i64>() else {
|
||||||
_ => Error::custom("Invalid line key"),
|
return Err(Error::custom("Invalid line key"));
|
||||||
})?;
|
};
|
||||||
|
|
||||||
if key != story.id {
|
if key != story.id {
|
||||||
return Err(Error::custom("Line key mismatch"));
|
return Err(Error::custom("Line key mismatch"));
|
||||||
|
|
|
@ -3,7 +3,8 @@
|
||||||
use chrono::prelude::*;
|
use chrono::prelude::*;
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use serde::de::Error;
|
use serde::de::Error;
|
||||||
use serde::{Deserialize, Deserializer};
|
use serde::Deserialize;
|
||||||
|
use serde::Deserializer;
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
|
||||||
use super::interner::Interner;
|
use super::interner::Interner;
|
||||||
|
@ -184,16 +185,16 @@ fn string_to_id<'de, D>(d: D) -> Result<i64, D::Error>
|
||||||
where
|
where
|
||||||
D: Deserializer<'de>,
|
D: Deserializer<'de>,
|
||||||
{
|
{
|
||||||
let value = Value::deserialize(d)?;
|
match Value::deserialize(d)? {
|
||||||
|
Value::Number(value) => match value.as_i64() {
|
||||||
if value.is_i64() {
|
Some(value) => Ok(value),
|
||||||
Ok(value.as_i64().unwrap())
|
_ => Err(Error::custom("Could not parse ID number")),
|
||||||
} else if value.is_string() {
|
},
|
||||||
value.as_str().unwrap().parse().map_err(|e| match e {
|
Value::String(value) => match value.parse::<i64>() {
|
||||||
_ => Error::custom("Could not parse ID string"),
|
Ok(value) => Ok(value),
|
||||||
})
|
_ => Err(Error::custom("Could not parse ID string")),
|
||||||
} else {
|
},
|
||||||
Err(Error::custom("Invalid type for ID value"))
|
_ => Err(Error::custom("Invalid type for ID value")),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -218,9 +219,9 @@ impl<'de> Deserialize<'de> for Color {
|
||||||
.and_then(|value| value.as_str())
|
.and_then(|value| value.as_str())
|
||||||
.ok_or_else(|| Error::custom("Color is missing hex value"))?;
|
.ok_or_else(|| Error::custom("Color is missing hex value"))?;
|
||||||
|
|
||||||
let array = hex::decode(text).map_err(|e| match e {
|
let Ok(array) = hex::decode(text) else {
|
||||||
_ => Error::custom("Color hex has invalid value"),
|
return Err(Error::custom("Color hex has invalid value"));
|
||||||
})?;
|
};
|
||||||
|
|
||||||
match array[..] {
|
match array[..] {
|
||||||
[red, green, blue] => Ok(Color { red, green, blue }),
|
[red, green, blue] => Ok(Color { red, green, blue }),
|
||||||
|
|
Loading…
Reference in a new issue