From 4efaf764a8b6c57d80c946255a14216bf6f048e0 Mon Sep 17 00:00:00 2001 From: Yoonchae Lee Date: Tue, 16 Apr 2024 17:00:04 +0900 Subject: [PATCH] dev: fix most clippy lints --- dictionary/src/jmdict.rs | 2 +- dictionary/src/wasm.rs | 6 +++--- jmdict/src/jmdict.rs | 10 +++++----- jmdict/src/lib.rs | 2 +- jmdict/src/xml.rs | 15 ++++++--------- rust/src/error.rs | 7 +++---- rust/src/tokenize.rs | 4 ++-- rust/src/wasm.rs | 13 ++++++------- 8 files changed, 27 insertions(+), 32 deletions(-) diff --git a/dictionary/src/jmdict.rs b/dictionary/src/jmdict.rs index 34e7b9ae..d0a3baaf 100644 --- a/dictionary/src/jmdict.rs +++ b/dictionary/src/jmdict.rs @@ -78,7 +78,7 @@ fn parse_part_of_speech(value: &str) -> PartOfSpeech { "adj-pn" => PartOfSpeech::Adnomial, "exp" => PartOfSpeech::Expression, "unc" => PartOfSpeech::Unclassified, - s if s.starts_with("v") => PartOfSpeech::Verb, + s if s.starts_with('v') => PartOfSpeech::Verb, s if s.starts_with("adj-") => PartOfSpeech::Adjective, s if s.starts_with("n-") => PartOfSpeech::Noun, other => panic!("Unknown part of speech: {}", other), diff --git a/dictionary/src/wasm.rs b/dictionary/src/wasm.rs index e47cb6e3..7d4307e6 100644 --- a/dictionary/src/wasm.rs +++ b/dictionary/src/wasm.rs @@ -10,9 +10,9 @@ use log::debug; use wasm_bindgen::prelude::*; #[cfg(wasm)] -impl Into for Error { - fn into(self) -> JsValue { - JsValue::from_str(&self.to_string()) +impl From for JsValue { + fn from(value: Error) -> JsValue { + JsValue::from_str(&value.to_string()) } } diff --git a/jmdict/src/jmdict.rs b/jmdict/src/jmdict.rs index 4229c8c4..e7bc3253 100644 --- a/jmdict/src/jmdict.rs +++ b/jmdict/src/jmdict.rs @@ -10,7 +10,7 @@ pub struct JMEntry { impl JMEntry { pub fn priority(&self) -> u16 { - let priorities = &self.readings.get(0).unwrap().priority; + let priorities = &self.readings.first().unwrap().priority; let mut priority: u16 = 0; for p in priorities { @@ -25,8 +25,8 @@ impl JMEntry { } else if ["news2", "ichi2", "spec2", "gai2"].contains(&p.as_str()) { priority += 5 // 01 ~ 48, each with ~500 entries - } else if p.starts_with("nf") { - let freq = p[2..] + } else if let Some(stripped) = p.strip_prefix("nf") { + let freq = stripped .parse::() .expect("could not parse XX as number where priority nfXX"); priority += 50 - freq; @@ -52,7 +52,7 @@ impl JMForm { return true; } } - return false; + false } } @@ -75,7 +75,7 @@ impl JMReading { return true; } } - return false; + false } } diff --git a/jmdict/src/lib.rs b/jmdict/src/lib.rs index 5de25df2..0fa6900b 100644 --- a/jmdict/src/lib.rs +++ b/jmdict/src/lib.rs @@ -9,7 +9,7 @@ use crate::xml::{parse_xml, remove_doctype, unescape_entity}; pub type Result = core::result::Result; pub fn parse_jmdict_xml(xml: &str) -> Result> { - let xml = remove_doctype(&xml); + let xml = remove_doctype(xml); let xml = unescape_entity(&xml); let jm_entries = parse_xml(&xml)?; Ok(jm_entries) diff --git a/jmdict/src/xml.rs b/jmdict/src/xml.rs index 759310f4..f0060dd2 100644 --- a/jmdict/src/xml.rs +++ b/jmdict/src/xml.rs @@ -8,7 +8,7 @@ use crate::{Error, Result}; /// RustyXML errors upon custom entity `&xx;` /// So unescape to `=xx=` before parsing -pub fn unescape_entity<'a>(xml: &'a str) -> Cow<'a, str> { +pub fn unescape_entity(xml: &str) -> Cow<'_, str> { let re = Regex::new(r#"&([\w\d-]+);"#).unwrap(); re.replace_all(xml, "=$1=") } @@ -24,13 +24,10 @@ pub fn parse_xml(xml_string: &str) -> Result> { parser.feed_str(xml_string); loop { - match parser.next().ok_or(" not found")?? { - Event::ElementStart(tag) => { - if &tag.name == "JMdict" { - return parse_jmdict(&mut parser); - } + if let Event::ElementStart(tag) = parser.next().ok_or(" not found")?? { + if &tag.name == "JMdict" { + return parse_jmdict(&mut parser); } - _ => {} } } } @@ -223,7 +220,7 @@ fn parse_sense(parser: &mut Parser) -> Result { pub fn parse_characters(parser: &mut Parser, in_tag: &str) -> Result { let mut characters = String::new(); - while let Some(event) = parser.next() { + for event in parser { match event? { Event::ElementStart(tag) => { return Err(Error::Unexpected { @@ -232,7 +229,7 @@ pub fn parse_characters(parser: &mut Parser, in_tag: &str) -> Result { }); } Event::ElementEnd(tag) => { - if &tag.name == in_tag { + if tag.name == in_tag { return Ok(characters); } else { return Err(Error::Unexpected { diff --git a/rust/src/error.rs b/rust/src/error.rs index 8e58f5ef..c576407e 100644 --- a/rust/src/error.rs +++ b/rust/src/error.rs @@ -47,10 +47,9 @@ err_from!(ureq::Error); err_from!(yomikiri_dictionary::Error); err_from!(FromUtf8Error); - #[cfg(wasm)] -impl Into for YomikiriError { - fn into(self) -> JsValue { - JsValue::from_str(&self.to_string()) +impl From for JsValue { + fn from(value: YomikiriError) -> Self { + JsValue::from_str(&value.to_string()) } } diff --git a/rust/src/tokenize.rs b/rust/src/tokenize.rs index edcda26d..fa74ceb3 100644 --- a/rust/src/tokenize.rs +++ b/rust/src/tokenize.rs @@ -124,7 +124,7 @@ impl TokenDetails { let mut details = details.iter(); let (pos, pos2) = details .next() - .and_then(|p| p.as_bytes().get(0)) + .and_then(|p| p.as_bytes().first()) .and_then(|short| UnidicPos::from_short(*short).ok()) .map(|pos| pos.to_unidic()) .unwrap_or(("UNK", "*")); @@ -132,7 +132,7 @@ impl TokenDetails { let pos2 = pos2.to_string(); let conj_form = details .next() - .and_then(|p| p.as_bytes().get(0)) + .and_then(|p| p.as_bytes().first()) .and_then(|short| UnidicConjugationForm::from_short(*short).ok()) .map(|conj| conj.to_unidic()) .unwrap_or("*") diff --git a/rust/src/wasm.rs b/rust/src/wasm.rs index d8cfe334..783e0e19 100644 --- a/rust/src/wasm.rs +++ b/rust/src/wasm.rs @@ -4,11 +4,10 @@ use crate::tokenize::create_tokenizer; use crate::utils; use crate::SharedBackend; use bincode::Options; -use js_sys::{Array, Uint8Array}; -use log::debug; -use std::io::{Cursor, Read}; +use js_sys::Uint8Array; +use std::io::Cursor; use wasm_bindgen::prelude::*; -use yomikiri_dictionary::file::{parse_jmdict_xml, write_entries, write_indexes, DictTermIndex}; +use yomikiri_dictionary::file::DictTermIndex; #[wasm_bindgen(typescript_custom_section)] const TS_CUSTOM: &'static str = r#" @@ -69,7 +68,7 @@ impl Backend { serde_wasm_bindgen::to_value(&result).map_err(|e| { YomikiriError::ConversionError(format!( "Failed to serialize tokenizer result.\n{}", - e.to_string() + e )) }) } @@ -81,7 +80,7 @@ impl Backend { serde_wasm_bindgen::to_value(&entries_json).map_err(|e| { YomikiriError::ConversionError(format!( "Failed to serialize dictionary entries.\n{}", - e.to_string() + e )) }) } @@ -97,7 +96,7 @@ impl Dictionary> { let index: Vec = options.deserialize_from(index_bytes).map_err(|e| { YomikiriError::InvalidDictionaryFile(format!( "Failed to parse dictionary index file. {}", - e.to_string() + e )) })?; let cursor = Cursor::new(entries_bytes);