From bb6c0cdec0a6bb6449a6e0583983308e5077985e Mon Sep 17 00:00:00 2001 From: Pyry Kontio Date: Wed, 1 Feb 2023 04:47:11 +0900 Subject: [PATCH] Cargo fmt + clippy --- src/audio.rs | 12 +++++------- src/bin/cli.rs | 1 - src/bin/explode.rs | 7 +++---- src/dict.rs | 12 ++++-------- src/key.rs | 27 ++++++++++++++++----------- src/lib.rs | 2 +- src/pages.rs | 2 +- src/resource/nrsc.rs | 5 ++--- src/resource/rsc.rs | 2 +- 9 files changed, 33 insertions(+), 37 deletions(-) diff --git a/src/audio.rs b/src/audio.rs index cd33aff..3e767af 100644 --- a/src/audio.rs +++ b/src/audio.rs @@ -1,4 +1,4 @@ -use std::{path::PathBuf, ops::Range, fmt::Display}; +use std::{fmt::Display, ops::Range, path::PathBuf}; use crate::{ dict::Paths, @@ -47,9 +47,7 @@ impl Audio { self.init()?; let Some(res) = self.res.as_mut() else { unreachable!() }; match res { - AudioResource::Rsc(rsc) => { - rsc.get(u32::from_str_radix(id, 10).map_err(|_| Error::InvalidIndex)?) - } + AudioResource::Rsc(rsc) => rsc.get(id.parse::().map_err(|_| Error::InvalidIndex)?), AudioResource::Nrsc(nrsc) => nrsc.get(id), } } @@ -61,11 +59,11 @@ impl Audio { AudioResource::Rsc(rsc) => { let (id, page) = rsc.get_by_idx(idx)?; (AudioId::Num(id), page) - }, + } AudioResource::Nrsc(nrsc) => { let (id, page) = nrsc.get_by_idx(idx)?; (AudioId::Str(id), page) - }, + } }) } @@ -82,7 +80,7 @@ impl Audio { #[derive(Debug)] pub enum AudioId<'a> { Str(&'a str), - Num(u32) + Num(u32), } impl Display for AudioId<'_> { diff --git a/src/bin/cli.rs b/src/bin/cli.rs index 804629e..4e9a12b 100644 --- a/src/bin/cli.rs +++ b/src/bin/cli.rs @@ -102,7 +102,6 @@ fn main() { } } */ - /* let idx_list = [ 0, diff --git a/src/bin/explode.rs b/src/bin/explode.rs index 1ed7b6e..6fb2706 100644 --- a/src/bin/explode.rs +++ b/src/bin/explode.rs @@ -1,17 +1,17 @@ use std::{ + fmt::Write as _, fs::{create_dir_all, File}, io::Write, - fmt::Write as _, }; -use monokakido::{Error, MonokakidoDict, KeyIndex, PageItemId}; +use monokakido::{Error, KeyIndex, MonokakidoDict, PageItemId}; fn out_dir(dict: &MonokakidoDict) -> String { dict.name().to_owned() + "_out/" } fn write_index(dict: &MonokakidoDict, index: &KeyIndex, tsv_fname: &str) -> Result<(), Error> { - let mut index_tsv = File::create(out_dir(&dict) + tsv_fname)?; + let mut index_tsv = File::create(out_dir(dict) + tsv_fname)?; for i in 0..index.len() { let (id, pages) = dict.keys.get_idx(index, i)?; index_tsv.write_all(id.as_bytes())?; @@ -66,6 +66,5 @@ fn explode() -> Result<(), Error> { fn main() { if let Err(err) = explode() { eprintln!("{err:?}"); - return; }; } diff --git a/src/dict.rs b/src/dict.rs index 061e8aa..eaca303 100644 --- a/src/dict.rs +++ b/src/dict.rs @@ -77,16 +77,12 @@ impl Paths { fn parse_dict_name(fname: &OsStr) -> Option<&str> { let fname = fname.to_str()?; let dict_prefix = "jp.monokakido.Dictionaries."; - if fname.starts_with(dict_prefix) { - Some(&fname[dict_prefix.len()..]) - } else { - None - } + fname.strip_prefix(dict_prefix) } impl MonokakidoDict { pub fn list() -> Result>, Error> { - let iter = fs::read_dir(&Paths::std_list_path()).map_err(|_| Error::IOError)?; + let iter = fs::read_dir(Paths::std_list_path()).map_err(|_| Error::IOError)?; Ok(iter.filter_map(|entry| { entry .map_err(|_| Error::IOError) @@ -97,7 +93,7 @@ impl MonokakidoDict { pub fn open(name: &str) -> Result { let std_path = Paths::std_dict_path(name); - Self::open_with_path_name(&std_path, name) + Self::open_with_path_name(std_path, name) } pub fn name(&self) -> &str { @@ -108,7 +104,7 @@ impl MonokakidoDict { let path: PathBuf = path.into(); let dir_name = path.file_name().ok_or(Error::FopenError)?.to_string_lossy(); - let dict_name = dir_name.rsplit_once(".").ok_or(Error::FopenError)?.0; + let dict_name = dir_name.rsplit_once('.').ok_or(Error::FopenError)?.0; Self::open_with_path_name(&path, dict_name) } diff --git a/src/key.rs b/src/key.rs index d85af70..2909e4b 100644 --- a/src/key.rs +++ b/src/key.rs @@ -84,7 +84,7 @@ use abi::{FileHeader, IndexHeader}; #[derive(Debug)] pub struct KeyIndex { - index: Option> + index: Option>, } pub struct Keys { @@ -99,7 +99,9 @@ impl KeyIndex { fn get(&self, i: usize) -> Result { let Some(index) = &self.index else { return Err(Error::IndexDoesntExist) }; let i = i + 1; // Because the the index is prefixed by its legth - if i >= index.len() { return Err(Error::InvalidIndex) } + if i >= index.len() { + return Err(Error::InvalidIndex); + } Ok(index[i].us()) } @@ -246,7 +248,7 @@ impl Keys { } } - return Err(Error::NotFound); + Err(Error::NotFound) } } @@ -325,13 +327,13 @@ impl<'a> Iterator for PageIter<'a> { // USE INVARIANT B: `self.span` is checked to conform to this shape, // so unreachable is never reached. `self.count` is also checked to correspond, // so overflow never happens. - let (id, tail) = match self.span { - &[1, hi, ref tail @ ..] => (pid([0, 0, hi], 0), tail), - &[2, hi, lo, ref tail @ ..] => (pid([0, hi, lo], 0), tail), - &[4, hi, mid, lo, ref tail @ ..] => (pid([hi, mid, lo], 0), tail), - &[17, hi, item, ref tail @ ..] => (pid([0, 0, hi], item), tail), - &[18, hi, lo, item, ref tail @ ..] => (pid([0, hi, lo], item), tail), - &[] => return None, + let (id, tail) = match *self.span { + [1, hi, ref tail @ ..] => (pid([0, 0, hi], 0), tail), + [2, hi, lo, ref tail @ ..] => (pid([0, hi, lo], 0), tail), + [4, hi, mid, lo, ref tail @ ..] => (pid([hi, mid, lo], 0), tail), + [17, hi, item, ref tail @ ..] => (pid([0, 0, hi], item), tail), + [18, hi, lo, item, ref tail @ ..] => (pid([0, hi, lo], item), tail), + [] => return None, _ => unreachable!(), }; self.count -= 1; @@ -346,5 +348,8 @@ pub struct PageItemId { } fn pid([hi, mid, lo]: [u8; 3], item: u8) -> PageItemId { - PageItemId { page: u32::from_be_bytes([0, hi, mid, lo]), item } + PageItemId { + page: u32::from_be_bytes([0, hi, mid, lo]), + item, + } } diff --git a/src/lib.rs b/src/lib.rs index df347be..6afd41c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,5 +9,5 @@ mod resource; pub use audio::Audio; pub use dict::MonokakidoDict; pub use error::Error; -pub use key::{Keys, KeyIndex, PageItemId}; +pub use key::{KeyIndex, Keys, PageItemId}; pub use pages::Pages; diff --git a/src/pages.rs b/src/pages.rs index e4ecabc..2ebe60c 100644 --- a/src/pages.rs +++ b/src/pages.rs @@ -27,7 +27,7 @@ impl Pages { pub fn get(&mut self, id: u32) -> Result<&str, Error> { self.init()?; let Some(res) = self.res.as_mut() else { unreachable!() }; - Ok(std::str::from_utf8(res.get(id)?).map_err(|_| Error::Utf8Error)?) + std::str::from_utf8(res.get(id)?).map_err(|_| Error::Utf8Error) } pub fn get_by_idx(&mut self, idx: usize) -> Result<(u32, &str), Error> { diff --git a/src/resource/nrsc.rs b/src/resource/nrsc.rs index 919e386..cab1bba 100644 --- a/src/resource/nrsc.rs +++ b/src/resource/nrsc.rs @@ -177,7 +177,7 @@ impl Nrsc { let fname = fname.to_str()?; if fname.ends_with(".nrsc") { let secnum_end = fname.len() - ".nrsc".len(); - u32::from_str_radix(&fname[..secnum_end], 10).ok() + fname[..secnum_end].parse().ok() } else { None } @@ -241,7 +241,7 @@ impl Nrsc { impl NrscData { fn get_by_nidx_rec(&mut self, idx: NrscIdxRecord) -> Result<&[u8], Error> { - let file = &mut self.files[idx.fileseq() as usize]; + let file = &mut self.files[idx.fileseq()]; file.file .seek(SeekFrom::Start(idx.file_offset())) @@ -265,5 +265,4 @@ impl NrscData { } } } - } diff --git a/src/resource/rsc.rs b/src/resource/rsc.rs index ceaae98..759fee8 100644 --- a/src/resource/rsc.rs +++ b/src/resource/rsc.rs @@ -247,7 +247,7 @@ impl Rsc { if fname.starts_with(rsc_name) && fname.ends_with(ext) && fname.len() > min_len { let seqnum_start = rsc_name.len() + 1; let seqnum_end = fname.len() - ext.len(); - u32::from_str_radix(&fname[seqnum_start..seqnum_end], 10).ok() + fname[seqnum_start..seqnum_end].parse().ok() } else { None }