fix: add pedantic lints

This commit is contained in:
ThetaDev 2023-05-13 02:40:26 +02:00
parent 81280200f7
commit cbeb14f3fd
41 changed files with 520 additions and 447 deletions

View file

@ -1,13 +1,17 @@
// This file is automatically generated. DO NOT EDIT.
// See codegen/gen_dictionary.rs for the generation code.
#![allow(clippy::unreadable_literal)]
//! The dictionary contains the information required to parse dates and numbers
//! in all supported languages.
use crate::{
model::AlbumType,
param::Language,
util::timeago::{DateCmp, TaToken, TimeUnit},
};
/// The dictionary contains the information required to parse dates and numbers
/// in all supported languages.
/// Dictionary entry containing language-specific parsing information
pub(crate) struct Entry {
/// Tokens for parsing timeago strings.
///

View file

@ -91,7 +91,7 @@ pub fn random_uuid() -> String {
rng.gen::<u16>(),
rng.gen::<u16>(),
rng.gen::<u16>(),
rng.gen::<u64>() & 0xffffffffffff,
rng.gen::<u64>() & 0xffff_ffff_ffff,
)
}
@ -315,10 +315,7 @@ where
let dict_entry = dictionary::entry(lang);
let by_char = lang_by_char(lang) || lang == Language::Ko;
let decimal_point = match dict_entry.comma_decimal {
true => ',',
false => '.',
};
let decimal_point = if dict_entry.comma_decimal { ',' } else { '.' };
let mut digits = String::new();
let mut filtered = String::new();
@ -345,14 +342,14 @@ where
if digits.is_empty() {
SplitTokens::new(&filtered, by_char)
.find_map(|token| dict_entry.number_nd_tokens.get(token))
.and_then(|n| (*n as u64).try_into().ok())
.and_then(|n| (u64::from(*n)).try_into().ok())
} else {
let num = digits.parse::<u64>().ok()?;
exp += SplitTokens::new(&filtered, by_char)
.filter_map(|token| match token {
"k" => Some(3),
_ => dict_entry.number_tokens.get(token).map(|t| *t as i32),
_ => dict_entry.number_tokens.get(token).map(|t| i32::from(*t)),
})
.sum::<i32>();
@ -447,9 +444,10 @@ pub enum SplitTokens<'a> {
impl<'a> SplitTokens<'a> {
pub fn new(s: &'a str, by_char: bool) -> Self {
match by_char {
true => Self::Char(SplitChar::from(s)),
false => Self::Word(s.split_whitespace()),
if by_char {
Self::Char(SplitChar::from(s))
} else {
Self::Word(s.split_whitespace())
}
}
}

View file

@ -33,8 +33,8 @@ impl ProtoBuilder {
///
/// Reference: <https://developers.google.com/protocol-buffers/docs/encoding?hl=en#structure>
fn _field(&mut self, field: u32, wire: u8) {
let fbits: u64 = (field as u64) << 3;
let wbits = wire as u64 & 0x07;
let fbits = u64::from(field) << 3;
let wbits = u64::from(wire) & 0x07;
let val: u64 = fbits | wbits;
self._varint(val);
}
@ -74,7 +74,7 @@ fn parse_varint<P: Iterator<Item = u8>>(pb: &mut P) -> Option<u64> {
for b in pb.by_ref() {
let value = b & 0x7f;
result |= (value as u64) << (7 * num_read);
result |= u64::from(value) << (7 * num_read);
num_read += 1;
if b & 0x80 == 0 {
@ -118,9 +118,8 @@ pub fn string_from_pb<P: IntoIterator<Item = u8>>(pb: P, field: u32) -> Option<S
buf.push(pb.next()?);
}
return String::from_utf8(buf).ok();
} else {
len
}
len
}
_ => return None,
};

View file

@ -77,7 +77,7 @@ pub enum DateCmp {
}
impl TimeUnit {
pub fn secs(&self) -> i64 {
pub fn secs(self) -> i64 {
match self {
TimeUnit::Second => 1,
TimeUnit::Minute => 60,
@ -91,7 +91,7 @@ impl TimeUnit {
}
impl TimeAgo {
fn secs(&self) -> i64 {
fn secs(self) -> i64 {
i64::from(self.n) * self.unit.secs()
}
}
@ -117,8 +117,8 @@ impl From<TimeAgo> for OffsetDateTime {
fn from(ta: TimeAgo) -> Self {
let ts = util::now_sec();
match ta.unit {
TimeUnit::Month => ts.replace_date(util::shift_months(ts.date(), -(ta.n as i32))),
TimeUnit::Year => ts.replace_date(util::shift_years(ts.date(), -(ta.n as i32))),
TimeUnit::Month => ts.replace_date(util::shift_months(ts.date(), -i32::from(ta.n))),
TimeUnit::Year => ts.replace_date(util::shift_years(ts.date(), -i32::from(ta.n))),
_ => ts - Duration::from(ta),
}
}
@ -156,9 +156,10 @@ struct TaTokenParser<'a> {
impl<'a> TaTokenParser<'a> {
fn new(entry: &'a dictionary::Entry, by_char: bool, nd: bool, filtered_str: &'a str) -> Self {
let tokens = match nd {
true => &entry.timeago_nd_tokens,
false => &entry.timeago_tokens,
let tokens = if nd {
&entry.timeago_nd_tokens
} else {
&entry.timeago_tokens
};
Self {
iter: SplitTokens::new(filtered_str, by_char),
@ -209,7 +210,7 @@ pub fn parse_timeago(lang: Language, textual_date: &str) -> Option<TimeAgo> {
///
/// Returns [`None`] if the date could not be parsed.
pub fn parse_timeago_dt(lang: Language, textual_date: &str) -> Option<OffsetDateTime> {
parse_timeago(lang, textual_date).map(|ta| ta.into())
parse_timeago(lang, textual_date).map(OffsetDateTime::from)
}
pub fn parse_timeago_dt_or_warn(
@ -260,7 +261,7 @@ pub fn parse_textual_date(lang: Language, textual_date: &str) -> Option<ParsedDa
// Chinese/Japanese dont use textual months
if m.is_none() && !by_char {
m = parse_textual_month(&entry, &filtered_str).map(|n| n as u16);
m = parse_textual_month(&entry, &filtered_str).map(u16::from);
}
match (y, m, d) {
@ -282,7 +283,7 @@ pub fn parse_textual_date(lang: Language, textual_date: &str) -> Option<ParsedDa
///
/// Returns None if the date could not be parsed.
pub fn parse_textual_date_to_dt(lang: Language, textual_date: &str) -> Option<OffsetDateTime> {
parse_textual_date(lang, textual_date).map(|ta| ta.into())
parse_textual_date(lang, textual_date).map(OffsetDateTime::from)
}
pub fn parse_textual_date_or_warn(