Skip to content

Commit

Permalink
dev: fix most clippy lints
Browse files Browse the repository at this point in the history
  • Loading branch information
BlueGreenMagick committed Apr 16, 2024
1 parent 48e0752 commit 4efaf76
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 32 deletions.
2 changes: 1 addition & 1 deletion dictionary/src/jmdict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
6 changes: 3 additions & 3 deletions dictionary/src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use log::debug;
use wasm_bindgen::prelude::*;

#[cfg(wasm)]
impl Into<JsValue> for Error {
fn into(self) -> JsValue {
JsValue::from_str(&self.to_string())
impl From<Error> for JsValue {
fn from(value: Error) -> JsValue {
JsValue::from_str(&value.to_string())
}
}

Expand Down
10 changes: 5 additions & 5 deletions jmdict/src/jmdict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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::<u16>()
.expect("could not parse XX as number where priority nfXX");
priority += 50 - freq;
Expand All @@ -52,7 +52,7 @@ impl JMForm {
return true;
}
}
return false;
false
}
}

Expand All @@ -75,7 +75,7 @@ impl JMReading {
return true;
}
}
return false;
false
}
}

Expand Down
2 changes: 1 addition & 1 deletion jmdict/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::xml::{parse_xml, remove_doctype, unescape_entity};
pub type Result<T> = core::result::Result<T, Error>;

pub fn parse_jmdict_xml(xml: &str) -> Result<Vec<JMEntry>> {
let xml = remove_doctype(&xml);
let xml = remove_doctype(xml);
let xml = unescape_entity(&xml);
let jm_entries = parse_xml(&xml)?;
Ok(jm_entries)
Expand Down
15 changes: 6 additions & 9 deletions jmdict/src/xml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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=")
}
Expand All @@ -24,13 +24,10 @@ pub fn parse_xml(xml_string: &str) -> Result<Vec<JMEntry>> {
parser.feed_str(xml_string);

loop {
match parser.next().ok_or("<JMDict> not found")?? {
Event::ElementStart(tag) => {
if &tag.name == "JMdict" {
return parse_jmdict(&mut parser);
}
if let Event::ElementStart(tag) = parser.next().ok_or("<JMDict> not found")?? {
if &tag.name == "JMdict" {
return parse_jmdict(&mut parser);
}
_ => {}
}
}
}
Expand Down Expand Up @@ -223,7 +220,7 @@ fn parse_sense(parser: &mut Parser) -> Result<JMSense> {

pub fn parse_characters(parser: &mut Parser, in_tag: &str) -> Result<String> {
let mut characters = String::new();
while let Some(event) = parser.next() {
for event in parser {
match event? {
Event::ElementStart(tag) => {
return Err(Error::Unexpected {
Expand All @@ -232,7 +229,7 @@ pub fn parse_characters(parser: &mut Parser, in_tag: &str) -> Result<String> {
});
}
Event::ElementEnd(tag) => {
if &tag.name == in_tag {
if tag.name == in_tag {
return Ok(characters);
} else {
return Err(Error::Unexpected {
Expand Down
7 changes: 3 additions & 4 deletions rust/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,9 @@ err_from!(ureq::Error);
err_from!(yomikiri_dictionary::Error);
err_from!(FromUtf8Error);


#[cfg(wasm)]
impl Into<JsValue> for YomikiriError {
fn into(self) -> JsValue {
JsValue::from_str(&self.to_string())
impl From<YomikiriError> for JsValue {
fn from(value: YomikiriError) -> Self {
JsValue::from_str(&value.to_string())
}
}
4 changes: 2 additions & 2 deletions rust/src/tokenize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,15 @@ 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", "*"));
let pos = pos.to_string();
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("*")
Expand Down
13 changes: 6 additions & 7 deletions rust/src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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#"
Expand Down Expand Up @@ -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
))
})
}
Expand All @@ -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
))
})
}
Expand All @@ -97,7 +96,7 @@ impl Dictionary<Cursor<&[u8]>> {
let index: Vec<DictTermIndex> = 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);
Expand Down

0 comments on commit 4efaf76

Please sign in to comment.