Cargo fmt + clippy

This commit is contained in:
Pyry Kontio 2023-02-01 04:47:11 +09:00
parent 9ded46f49c
commit bb6c0cdec0
No known key found for this signature in database
9 changed files with 33 additions and 37 deletions

View file

@ -1,4 +1,4 @@
use std::{path::PathBuf, ops::Range, fmt::Display}; use std::{fmt::Display, ops::Range, path::PathBuf};
use crate::{ use crate::{
dict::Paths, dict::Paths,
@ -47,9 +47,7 @@ impl Audio {
self.init()?; self.init()?;
let Some(res) = self.res.as_mut() else { unreachable!() }; let Some(res) = self.res.as_mut() else { unreachable!() };
match res { match res {
AudioResource::Rsc(rsc) => { AudioResource::Rsc(rsc) => rsc.get(id.parse::<u32>().map_err(|_| Error::InvalidIndex)?),
rsc.get(u32::from_str_radix(id, 10).map_err(|_| Error::InvalidIndex)?)
}
AudioResource::Nrsc(nrsc) => nrsc.get(id), AudioResource::Nrsc(nrsc) => nrsc.get(id),
} }
} }
@ -61,11 +59,11 @@ impl Audio {
AudioResource::Rsc(rsc) => { AudioResource::Rsc(rsc) => {
let (id, page) = rsc.get_by_idx(idx)?; let (id, page) = rsc.get_by_idx(idx)?;
(AudioId::Num(id), page) (AudioId::Num(id), page)
}, }
AudioResource::Nrsc(nrsc) => { AudioResource::Nrsc(nrsc) => {
let (id, page) = nrsc.get_by_idx(idx)?; let (id, page) = nrsc.get_by_idx(idx)?;
(AudioId::Str(id), page) (AudioId::Str(id), page)
}, }
}) })
} }
@ -82,7 +80,7 @@ impl Audio {
#[derive(Debug)] #[derive(Debug)]
pub enum AudioId<'a> { pub enum AudioId<'a> {
Str(&'a str), Str(&'a str),
Num(u32) Num(u32),
} }
impl Display for AudioId<'_> { impl Display for AudioId<'_> {

View file

@ -102,7 +102,6 @@ fn main() {
} }
} */ } */
/* /*
let idx_list = [ let idx_list = [
0, 0,

View file

@ -1,17 +1,17 @@
use std::{ use std::{
fmt::Write as _,
fs::{create_dir_all, File}, fs::{create_dir_all, File},
io::Write, io::Write,
fmt::Write as _,
}; };
use monokakido::{Error, MonokakidoDict, KeyIndex, PageItemId}; use monokakido::{Error, KeyIndex, MonokakidoDict, PageItemId};
fn out_dir(dict: &MonokakidoDict) -> String { fn out_dir(dict: &MonokakidoDict) -> String {
dict.name().to_owned() + "_out/" dict.name().to_owned() + "_out/"
} }
fn write_index(dict: &MonokakidoDict, index: &KeyIndex, tsv_fname: &str) -> Result<(), Error> { 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() { for i in 0..index.len() {
let (id, pages) = dict.keys.get_idx(index, i)?; let (id, pages) = dict.keys.get_idx(index, i)?;
index_tsv.write_all(id.as_bytes())?; index_tsv.write_all(id.as_bytes())?;
@ -66,6 +66,5 @@ fn explode() -> Result<(), Error> {
fn main() { fn main() {
if let Err(err) = explode() { if let Err(err) = explode() {
eprintln!("{err:?}"); eprintln!("{err:?}");
return;
}; };
} }

View file

@ -77,16 +77,12 @@ impl Paths {
fn parse_dict_name(fname: &OsStr) -> Option<&str> { fn parse_dict_name(fname: &OsStr) -> Option<&str> {
let fname = fname.to_str()?; let fname = fname.to_str()?;
let dict_prefix = "jp.monokakido.Dictionaries."; let dict_prefix = "jp.monokakido.Dictionaries.";
if fname.starts_with(dict_prefix) { fname.strip_prefix(dict_prefix)
Some(&fname[dict_prefix.len()..])
} else {
None
}
} }
impl MonokakidoDict { impl MonokakidoDict {
pub fn list() -> Result<impl Iterator<Item = Result<String, Error>>, Error> { pub fn list() -> Result<impl Iterator<Item = Result<String, Error>>, 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| { Ok(iter.filter_map(|entry| {
entry entry
.map_err(|_| Error::IOError) .map_err(|_| Error::IOError)
@ -97,7 +93,7 @@ impl MonokakidoDict {
pub fn open(name: &str) -> Result<Self, Error> { pub fn open(name: &str) -> Result<Self, Error> {
let std_path = Paths::std_dict_path(name); 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 { pub fn name(&self) -> &str {
@ -108,7 +104,7 @@ impl MonokakidoDict {
let path: PathBuf = path.into(); let path: PathBuf = path.into();
let dir_name = path.file_name().ok_or(Error::FopenError)?.to_string_lossy(); 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) Self::open_with_path_name(&path, dict_name)
} }

View file

@ -84,7 +84,7 @@ use abi::{FileHeader, IndexHeader};
#[derive(Debug)] #[derive(Debug)]
pub struct KeyIndex { pub struct KeyIndex {
index: Option<Vec<LE32>> index: Option<Vec<LE32>>,
} }
pub struct Keys { pub struct Keys {
@ -99,7 +99,9 @@ impl KeyIndex {
fn get(&self, i: usize) -> Result<usize, Error> { fn get(&self, i: usize) -> Result<usize, Error> {
let Some(index) = &self.index else { return Err(Error::IndexDoesntExist) }; let Some(index) = &self.index else { return Err(Error::IndexDoesntExist) };
let i = i + 1; // Because the the index is prefixed by its legth 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()) 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, // 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 unreachable is never reached. `self.count` is also checked to correspond,
// so overflow never happens. // so overflow never happens.
let (id, tail) = match self.span { let (id, tail) = match *self.span {
&[1, hi, ref tail @ ..] => (pid([0, 0, hi], 0), tail), [1, hi, ref tail @ ..] => (pid([0, 0, hi], 0), tail),
&[2, hi, lo, ref tail @ ..] => (pid([0, hi, lo], 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), [4, hi, mid, lo, ref tail @ ..] => (pid([hi, mid, lo], 0), tail),
&[17, hi, item, ref tail @ ..] => (pid([0, 0, hi], item), tail), [17, hi, item, ref tail @ ..] => (pid([0, 0, hi], item), tail),
&[18, hi, lo, item, ref tail @ ..] => (pid([0, hi, lo], item), tail), [18, hi, lo, item, ref tail @ ..] => (pid([0, hi, lo], item), tail),
&[] => return None, [] => return None,
_ => unreachable!(), _ => unreachable!(),
}; };
self.count -= 1; self.count -= 1;
@ -346,5 +348,8 @@ pub struct PageItemId {
} }
fn pid([hi, mid, lo]: [u8; 3], item: u8) -> 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,
}
} }

View file

@ -9,5 +9,5 @@ mod resource;
pub use audio::Audio; pub use audio::Audio;
pub use dict::MonokakidoDict; pub use dict::MonokakidoDict;
pub use error::Error; pub use error::Error;
pub use key::{Keys, KeyIndex, PageItemId}; pub use key::{KeyIndex, Keys, PageItemId};
pub use pages::Pages; pub use pages::Pages;

View file

@ -27,7 +27,7 @@ impl Pages {
pub fn get(&mut self, id: u32) -> Result<&str, Error> { pub fn get(&mut self, id: u32) -> Result<&str, Error> {
self.init()?; self.init()?;
let Some(res) = self.res.as_mut() else { unreachable!() }; 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> { pub fn get_by_idx(&mut self, idx: usize) -> Result<(u32, &str), Error> {

View file

@ -177,7 +177,7 @@ impl Nrsc {
let fname = fname.to_str()?; let fname = fname.to_str()?;
if fname.ends_with(".nrsc") { if fname.ends_with(".nrsc") {
let secnum_end = fname.len() - ".nrsc".len(); let secnum_end = fname.len() - ".nrsc".len();
u32::from_str_radix(&fname[..secnum_end], 10).ok() fname[..secnum_end].parse().ok()
} else { } else {
None None
} }
@ -241,7 +241,7 @@ impl Nrsc {
impl NrscData { impl NrscData {
fn get_by_nidx_rec(&mut self, idx: NrscIdxRecord) -> Result<&[u8], Error> { 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 file.file
.seek(SeekFrom::Start(idx.file_offset())) .seek(SeekFrom::Start(idx.file_offset()))
@ -265,5 +265,4 @@ impl NrscData {
} }
} }
} }
} }

View file

@ -247,7 +247,7 @@ impl Rsc {
if fname.starts_with(rsc_name) && fname.ends_with(ext) && fname.len() > min_len { if fname.starts_with(rsc_name) && fname.ends_with(ext) && fname.len() > min_len {
let seqnum_start = rsc_name.len() + 1; let seqnum_start = rsc_name.len() + 1;
let seqnum_end = fname.len() - ext.len(); 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 { } else {
None None
} }