fix: add pedantic lints
This commit is contained in:
parent
81280200f7
commit
cbeb14f3fd
41 changed files with 520 additions and 447 deletions
|
|
@ -102,10 +102,10 @@ pub async fn run_test(
|
|||
let count = results.iter().filter(|(p, _)| *p).count();
|
||||
let vd_present = results
|
||||
.iter()
|
||||
.find_map(|(p, vd)| if *p { Some(vd.to_owned()) } else { None });
|
||||
.find_map(|(p, vd)| if *p { Some(vd.clone()) } else { None });
|
||||
let vd_absent = results
|
||||
.iter()
|
||||
.find_map(|(p, vd)| if !*p { Some(vd.to_owned()) } else { None });
|
||||
.find_map(|(p, vd)| if *p { None } else { Some(vd.clone()) });
|
||||
|
||||
(count, vd_present, vd_absent)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -58,7 +58,7 @@ pub fn write_samples_to_dict() {
|
|||
let collected: BTreeMap<Language, BTreeMap<AlbumType, String>> =
|
||||
serde_json::from_reader(BufReader::new(json_file)).unwrap();
|
||||
let mut dict = util::read_dict();
|
||||
let langs = dict.keys().map(|k| k.to_owned()).collect::<Vec<_>>();
|
||||
let langs = dict.keys().copied().collect::<Vec<_>>();
|
||||
|
||||
for lang in langs {
|
||||
let dict_entry = dict.entry(lang).or_default();
|
||||
|
|
@ -66,13 +66,13 @@ pub fn write_samples_to_dict() {
|
|||
let mut e_langs = dict_entry.equivalent.clone();
|
||||
e_langs.push(lang);
|
||||
|
||||
e_langs.iter().for_each(|lang| {
|
||||
for lang in &e_langs {
|
||||
collected.get(lang).unwrap().iter().for_each(|(t, v)| {
|
||||
dict_entry
|
||||
.album_types
|
||||
.insert(v.to_lowercase().trim().to_owned(), *t);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
util::write_dict(dict);
|
||||
|
|
|
|||
|
|
@ -111,7 +111,7 @@ pub async fn collect_large_numbers(concurrency: usize) {
|
|||
.unwrap();
|
||||
|
||||
channel.view_counts.iter().for_each(|(num, txt)| {
|
||||
entry.insert(txt.to_owned(), *num);
|
||||
entry.insert(txt.clone(), *num);
|
||||
});
|
||||
entry.insert(channel.subscriber_count, subscriber_counts[*ch_id]);
|
||||
|
||||
|
|
@ -147,7 +147,7 @@ pub fn write_samples_to_dict() {
|
|||
let collected_nums: CollectedNumbers =
|
||||
serde_json::from_reader(BufReader::new(json_file)).unwrap();
|
||||
let mut dict = util::read_dict();
|
||||
let langs = dict.keys().map(|k| k.to_owned()).collect::<Vec<_>>();
|
||||
let langs = dict.keys().copied().collect::<Vec<_>>();
|
||||
|
||||
static POINT_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"\d(\.|,)\d{1,3}(?:\D|$)").unwrap());
|
||||
|
||||
|
|
@ -176,10 +176,7 @@ pub fn write_samples_to_dict() {
|
|||
})
|
||||
.unwrap();
|
||||
|
||||
let decimal_point = match comma_decimal {
|
||||
true => ",",
|
||||
false => ".",
|
||||
};
|
||||
let decimal_point = if comma_decimal { "," } else { "." };
|
||||
|
||||
// Search for tokens
|
||||
|
||||
|
|
@ -217,13 +214,17 @@ pub fn write_samples_to_dict() {
|
|||
for lang in e_langs {
|
||||
let entry = collected_nums.get(&lang).unwrap();
|
||||
|
||||
entry.iter().for_each(|(txt, val)| {
|
||||
for (txt, val) in entry.iter() {
|
||||
let filtered = util::filter_largenumstr(txt);
|
||||
let mag = get_mag(*val);
|
||||
|
||||
let tokens: Vec<String> = match dict_entry.by_char || lang == Language::Ko {
|
||||
true => filtered.chars().map(|c| c.to_string()).collect(),
|
||||
false => filtered.split_whitespace().map(|c| c.to_string()).collect(),
|
||||
let tokens: Vec<String> = if dict_entry.by_char || lang == Language::Ko {
|
||||
filtered.chars().map(|c| c.to_string()).collect()
|
||||
} else {
|
||||
filtered
|
||||
.split_whitespace()
|
||||
.map(std::string::ToString::to_string)
|
||||
.collect()
|
||||
};
|
||||
|
||||
match util::parse_numeric::<u64>(txt.split(decimal_point).next().unwrap()) {
|
||||
|
|
@ -231,7 +232,7 @@ pub fn write_samples_to_dict() {
|
|||
let mag_before_point = get_mag(num_before_point);
|
||||
let mut mag_remaining = mag - mag_before_point;
|
||||
|
||||
tokens.iter().for_each(|t| {
|
||||
for t in &tokens {
|
||||
// These tokens are correct in all languages
|
||||
// and are used to parse combined prefixes like `1.1K crore` (en-IN)
|
||||
let known_tmag: u8 = if t.len() == 1 {
|
||||
|
|
@ -251,26 +252,26 @@ pub fn write_samples_to_dict() {
|
|||
.checked_sub(known_tmag)
|
||||
.expect("known magnitude incorrect");
|
||||
} else {
|
||||
insert_token(t.to_owned(), mag_remaining);
|
||||
insert_token(t.clone(), mag_remaining);
|
||||
}
|
||||
insert_nd_token(t.to_owned(), None);
|
||||
});
|
||||
insert_nd_token(t.clone(), None);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
if matches!(e.kind(), std::num::IntErrorKind::Empty) {
|
||||
// Text does not contain any digits, search for nd_tokens
|
||||
tokens.iter().for_each(|t| {
|
||||
for t in &tokens {
|
||||
insert_nd_token(
|
||||
t.to_owned(),
|
||||
t.clone(),
|
||||
Some((*val).try_into().expect("nd_token value too large")),
|
||||
);
|
||||
});
|
||||
}
|
||||
} else {
|
||||
panic!("{e}, txt: {txt}")
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Insert collected data into dictionary
|
||||
|
|
@ -369,7 +370,7 @@ async fn get_channel(query: &RustyPipeQuery, channel_id: &str) -> Result<Channel
|
|||
.navigation_endpoint
|
||||
.continuation_command
|
||||
.token
|
||||
.to_owned()
|
||||
.clone()
|
||||
})
|
||||
});
|
||||
|
||||
|
|
@ -380,7 +381,7 @@ async fn get_channel(query: &RustyPipeQuery, channel_id: &str) -> Result<Channel
|
|||
let v = &itm.rich_item_renderer.content.video_renderer;
|
||||
(
|
||||
util::parse_numeric(&v.view_count_text.text).unwrap_or_default(),
|
||||
v.short_view_count_text.text.to_owned(),
|
||||
v.short_view_count_text.text.clone(),
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
|
|
@ -399,21 +400,19 @@ async fn get_channel(query: &RustyPipeQuery, channel_id: &str) -> Result<Channel
|
|||
|
||||
let continuation = serde_json::from_str::<ContinuationResponse>(&resp)?;
|
||||
|
||||
continuation
|
||||
.on_response_received_actions
|
||||
.iter()
|
||||
.for_each(|a| {
|
||||
a.reload_continuation_items_command
|
||||
.continuation_items
|
||||
.iter()
|
||||
.for_each(|itm| {
|
||||
let v = &itm.rich_item_renderer.content.video_renderer;
|
||||
view_counts.insert(
|
||||
util::parse_numeric(&v.view_count_text.text).unwrap(),
|
||||
v.short_view_count_text.text.to_owned(),
|
||||
);
|
||||
})
|
||||
});
|
||||
for action in &continuation.on_response_received_actions {
|
||||
action
|
||||
.reload_continuation_items_command
|
||||
.continuation_items
|
||||
.iter()
|
||||
.for_each(|itm| {
|
||||
let v = &itm.rich_item_renderer.content.video_renderer;
|
||||
view_counts.insert(
|
||||
util::parse_numeric(&v.view_count_text.text).unwrap(),
|
||||
v.short_view_count_text.text.clone(),
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Ok(ChannelData {
|
||||
|
|
|
|||
|
|
@ -118,7 +118,7 @@ pub fn write_samples_to_dict() {
|
|||
let collected_dates: CollectedDates =
|
||||
serde_json::from_reader(BufReader::new(json_file)).unwrap();
|
||||
let mut dict = util::read_dict();
|
||||
let langs = dict.keys().map(|k| k.to_owned()).collect::<Vec<_>>();
|
||||
let langs = dict.keys().copied().collect::<Vec<_>>();
|
||||
|
||||
let months = [
|
||||
DateCase::Jan,
|
||||
|
|
@ -159,7 +159,7 @@ pub fn write_samples_to_dict() {
|
|||
.for_each(|l| datestr_tables.push(collected_dates.get(l).unwrap()));
|
||||
|
||||
let dict_entry = dict.entry(lang).or_default();
|
||||
let mut num_order = "".to_owned();
|
||||
let mut num_order = String::new();
|
||||
|
||||
let collect_nd_tokens = !matches!(
|
||||
lang,
|
||||
|
|
@ -236,30 +236,30 @@ pub fn write_samples_to_dict() {
|
|||
});
|
||||
});
|
||||
|
||||
month_words.iter().for_each(|(word, m)| {
|
||||
for (word, m) in &month_words {
|
||||
if *m != 0 {
|
||||
dict_entry.months.insert(word.to_owned(), *m as u8);
|
||||
dict_entry.months.insert(word.clone(), *m as u8);
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
if collect_nd_tokens {
|
||||
td_words.iter().for_each(|(word, n)| {
|
||||
for (word, n) in &td_words {
|
||||
match n {
|
||||
// Today
|
||||
1 => {
|
||||
dict_entry
|
||||
.timeago_nd_tokens
|
||||
.insert(word.to_owned(), "0D".to_owned());
|
||||
.insert(word.clone(), "0D".to_owned());
|
||||
}
|
||||
// Yesterday
|
||||
2 => {
|
||||
dict_entry
|
||||
.timeago_nd_tokens
|
||||
.insert(word.to_owned(), "1D".to_owned());
|
||||
.insert(word.clone(), "1D".to_owned());
|
||||
}
|
||||
_ => {}
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
if datestr_tables.len() == 1 && dict_entry.timeago_nd_tokens.len() > 2 {
|
||||
println!(
|
||||
|
|
|
|||
|
|
@ -67,7 +67,7 @@ pub fn parse_video_durations() {
|
|||
let durations: CollectedDurations = serde_json::from_reader(BufReader::new(json_file)).unwrap();
|
||||
|
||||
let mut dict = util::read_dict();
|
||||
let langs = dict.keys().map(|k| k.to_owned()).collect::<Vec<_>>();
|
||||
let langs = dict.keys().copied().collect::<Vec<_>>();
|
||||
|
||||
for lang in langs {
|
||||
let dict_entry = dict.entry(lang).or_default();
|
||||
|
|
@ -83,7 +83,7 @@ pub fn parse_video_durations() {
|
|||
by_char: bool,
|
||||
val: u32,
|
||||
expect: u32,
|
||||
w: String,
|
||||
w: &str,
|
||||
unit: TimeUnit,
|
||||
) -> bool {
|
||||
let ok = val == expect || val * 2 == expect;
|
||||
|
|
@ -168,23 +168,23 @@ pub fn parse_video_durations() {
|
|||
let p2_n = p2.digits.parse::<u32>().unwrap_or(1);
|
||||
|
||||
assert!(
|
||||
check_add_word(words, by_char, p1_n, m, p1.word, TimeUnit::Minute),
|
||||
check_add_word(words, by_char, p1_n, m, &p1.word, TimeUnit::Minute),
|
||||
"{txt}: min parse error"
|
||||
);
|
||||
assert!(
|
||||
check_add_word(words, by_char, p2_n, s, p2.word, TimeUnit::Second),
|
||||
check_add_word(words, by_char, p2_n, s, &p2.word, TimeUnit::Second),
|
||||
"{txt}: sec parse error"
|
||||
);
|
||||
}
|
||||
None => {
|
||||
if s == 0 {
|
||||
assert!(
|
||||
check_add_word(words, by_char, p1_n, m, p1.word, TimeUnit::Minute),
|
||||
check_add_word(words, by_char, p1_n, m, &p1.word, TimeUnit::Minute),
|
||||
"{txt}: min parse error"
|
||||
);
|
||||
} else if m == 0 {
|
||||
assert!(
|
||||
check_add_word(words, by_char, p1_n, s, p1.word, TimeUnit::Second),
|
||||
check_add_word(words, by_char, p1_n, s, &p1.word, TimeUnit::Second),
|
||||
"{txt}: sec parse error"
|
||||
);
|
||||
} else {
|
||||
|
|
@ -206,11 +206,11 @@ pub fn parse_video_durations() {
|
|||
|
||||
// dbg!(&words);
|
||||
|
||||
words.into_iter().for_each(|(k, v)| {
|
||||
for (k, v) in words {
|
||||
if let Some(v) = v {
|
||||
dict_entry.timeago_tokens.insert(k, v.to_string());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -345,7 +345,8 @@ mod tests {
|
|||
let ul: LanguageIdentifier =
|
||||
lang.to_string().split('-').next().unwrap().parse().unwrap();
|
||||
|
||||
let pr = PluralRules::create(ul, PluralRuleType::CARDINAL).expect(&lang.to_string());
|
||||
let pr = PluralRules::create(ul, PluralRuleType::CARDINAL)
|
||||
.unwrap_or_else(|_| panic!("{}", lang.to_string()));
|
||||
|
||||
let mut plurals_m: HashSet<PluralCategory> = HashSet::new();
|
||||
for n in 1..60 {
|
||||
|
|
@ -353,11 +354,11 @@ mod tests {
|
|||
}
|
||||
let mut plurals_s = plurals_m.clone();
|
||||
|
||||
durations.values().for_each(|v| {
|
||||
for v in durations.values() {
|
||||
let (m, s) = split_duration(*v);
|
||||
plurals_m.remove(&pr.select(m).unwrap().into());
|
||||
plurals_s.remove(&pr.select(s).unwrap().into());
|
||||
});
|
||||
}
|
||||
|
||||
if !plurals_m.is_empty() {
|
||||
println!("{lang}: missing minutes {plurals_m:?}");
|
||||
|
|
|
|||
|
|
@ -35,14 +35,18 @@ pub fn generate_dictionary() {
|
|||
|
||||
let code_head = r#"// 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.
|
||||
///
|
||||
|
|
@ -90,11 +94,11 @@ pub(crate) fn entry(lang: Language) -> Entry {
|
|||
"#
|
||||
.to_owned();
|
||||
|
||||
dict.iter().for_each(|(lang, entry)| {
|
||||
for (lang, entry) in &dict {
|
||||
// Match selector
|
||||
let mut selector = format!("Language::{lang:?}");
|
||||
entry.equivalent.iter().for_each(|eq| {
|
||||
let _ = write!(selector, " | Language::{eq:?}");
|
||||
write!(selector, " | Language::{eq:?}").unwrap();
|
||||
});
|
||||
|
||||
// Timeago tokens
|
||||
|
|
@ -132,7 +136,7 @@ pub(crate) fn entry(lang: Language) -> Entry {
|
|||
// Date order
|
||||
let mut date_order = "&[".to_owned();
|
||||
entry.date_order.chars().for_each(|c| {
|
||||
let _ = write!(date_order, "DateCmp::{c}, ");
|
||||
write!(date_order, "DateCmp::{c}, ").unwrap();
|
||||
});
|
||||
date_order = date_order.trim_end_matches([' ', ',']).to_owned() + "]";
|
||||
|
||||
|
|
@ -154,16 +158,31 @@ pub(crate) fn entry(lang: Language) -> Entry {
|
|||
album_types.entry(txt, &format!("AlbumType::{album_type:?}"));
|
||||
});
|
||||
|
||||
let code_ta_tokens = &ta_tokens.build().to_string().replace('\n', "\n ");
|
||||
let code_ta_nd_tokens = &ta_nd_tokens.build().to_string().replace('\n', "\n ");
|
||||
let code_ta_tokens = &ta_tokens
|
||||
.build()
|
||||
.to_string()
|
||||
.replace('\n', "\n ");
|
||||
let code_ta_nd_tokens = &ta_nd_tokens
|
||||
.build()
|
||||
.to_string()
|
||||
.replace('\n', "\n ");
|
||||
let code_months = &months.build().to_string().replace('\n', "\n ");
|
||||
let code_number_tokens = &number_tokens.build().to_string().replace('\n', "\n ");
|
||||
let code_number_nd_tokens = &number_nd_tokens.build().to_string().replace('\n', "\n ");
|
||||
let code_album_types = &album_types.build().to_string().replace('\n', "\n ");
|
||||
let code_number_tokens = &number_tokens
|
||||
.build()
|
||||
.to_string()
|
||||
.replace('\n', "\n ");
|
||||
let code_number_nd_tokens = &number_nd_tokens
|
||||
.build()
|
||||
.to_string()
|
||||
.replace('\n', "\n ");
|
||||
let code_album_types = &album_types
|
||||
.build()
|
||||
.to_string()
|
||||
.replace('\n', "\n ");
|
||||
|
||||
write!(code_timeago_tokens, "{} => Entry {{\n timeago_tokens: {},\n date_order: {},\n months: {},\n timeago_nd_tokens: {},\n comma_decimal: {:?},\n number_tokens: {},\n number_nd_tokens: {},\n album_types: {},\n }},\n ",
|
||||
selector, code_ta_tokens, date_order, code_months, code_ta_nd_tokens, entry.comma_decimal, code_number_tokens, code_number_nd_tokens, code_album_types).unwrap();
|
||||
});
|
||||
}
|
||||
|
||||
code_timeago_tokens = code_timeago_tokens.trim_end().to_owned() + "\n }\n}\n";
|
||||
|
||||
|
|
|
|||
|
|
@ -227,7 +227,7 @@ pub enum Country {
|
|||
"#
|
||||
.to_owned();
|
||||
|
||||
languages.iter().for_each(|(code, native_name)| {
|
||||
for (code, native_name) in &languages {
|
||||
let enum_name = code
|
||||
.split('-')
|
||||
.map(|c| {
|
||||
|
|
@ -262,10 +262,10 @@ pub enum Country {
|
|||
" Language::{enum_name} => \"{native_name}\","
|
||||
)
|
||||
.unwrap();
|
||||
});
|
||||
}
|
||||
code_langs += "}\n";
|
||||
|
||||
countries.iter().for_each(|(c, n)| {
|
||||
for (c, n) in &countries {
|
||||
let enum_name = c[0..1].to_owned().to_uppercase() + &c[1..].to_owned().to_lowercase();
|
||||
|
||||
// Country enum
|
||||
|
|
@ -281,7 +281,7 @@ pub enum Country {
|
|||
" Country::{enum_name} => \"{n}\","
|
||||
)
|
||||
.unwrap();
|
||||
});
|
||||
}
|
||||
|
||||
// Add Country::Zz / Global
|
||||
code_countries += " /// Global (can only be used for music charts)\n";
|
||||
|
|
@ -368,8 +368,8 @@ fn map_language_section(section: &CompactLinkRendererWrap) -> BTreeMap<String, S
|
|||
.actions[0]
|
||||
.select_language_command
|
||||
.hl
|
||||
.to_owned(),
|
||||
i.compact_link_renderer.title.text.to_owned(),
|
||||
.clone(),
|
||||
i.compact_link_renderer.title.text.clone(),
|
||||
)
|
||||
})
|
||||
.collect()
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
#![warn(clippy::todo)]
|
||||
|
||||
mod abtest;
|
||||
mod collect_album_types;
|
||||
mod collect_large_numbers;
|
||||
|
|
@ -90,7 +92,7 @@ async fn main() {
|
|||
}
|
||||
None => {
|
||||
let res = abtest::run_all_tests(n, cli.concurrency).await;
|
||||
println!("{}", serde_json::to_string_pretty(&res).unwrap())
|
||||
println!("{}", serde_json::to_string_pretty(&res).unwrap());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue