Fix clippy errors

This commit is contained in:
2025-02-24 15:02:44 +00:00
parent 03290e52a3
commit 8d57540566
15 changed files with 121 additions and 58 deletions
+38 -8
View File
@@ -1,3 +1,14 @@
#![warn(
clippy::correctness,
clippy::nursery,
clippy::unnecessary_cast,
clippy::all,
clippy::suspicious,
clippy::perf,
rustdoc::missing_errors_doc,
rustdoc::missing_panics_doc
)]
use std::fs::File;
use std::io::{Read, Seek, SeekFrom};
@@ -10,13 +21,18 @@ extern crate proc_macro;
#[proc_macro]
pub fn include_font(item: TokenStream) -> TokenStream {
let filename = parse_macro_input!(item as LitStr);
let file_path = filename.value().to_string();
let file_path = filename.value();
println!("Loading font: [{}]", file_path);
let font_data = match Font::new(load_file(file_path)) {
let font_bytes = match load_file(file_path) {
Ok(bytes) => bytes,
Err(why) => panic!("{}", why),
};
let font_data = match Font::new(font_bytes) {
Ok(font) => font.0,
Err(e) => panic!("{}", e),
Err(why) => panic!("{}", why),
};
quote!(
@@ -34,7 +50,7 @@ struct Font([[u8; 16]; 512]);
impl Font {
const MAGIC: u16 = 0x3604;
pub fn new(data: [u8; (32 + 2) * 512 + 4]) -> Result<Font, &'static str> {
pub fn new(data: [u8; (32 + 2) * 512 + 4]) -> Result<Self, &'static str> {
let magic: u16 = (data[0] as u16) << 8 | data[1] as u16;
let mode = data[2];
let size = data[3];
@@ -55,15 +71,29 @@ impl Font {
glyphs[i] = buff;
}
Ok(Font(glyphs))
Ok(Self(glyphs))
}
}
fn load_file(filename: String) -> [u8; (32 + 2) * 512 + 4] {
type FileContents = [u8; (32 + 2) * 512 + 4];
fn load_file(filename: String) -> Result<FileContents, std::io::Error> {
let mut buf = [0; (32 + 2) * 512 + 4];
let mut f = File::open(filename).unwrap();
f.seek(SeekFrom::Start(0)).unwrap();
f.read(&mut buf).unwrap();
return buf;
loop {
match f.read(&mut buf) {
Ok(read) => {
if read == 0 {
break;
}
}
Err(why) => {
eprintln!("Failed to read PS1 font file: {}", why);
return Err(why);
}
}
}
Ok(buf)
}