From 6c7f557cd7375c59e5fbdf09a0824a2cc502d0a7 Mon Sep 17 00:00:00 2001 From: angie Date: Fri, 15 Dec 2023 22:46:34 -0300 Subject: [PATCH] fmt and clippy --- src/rs/checksum.rs | 29 +++++++++++++++++++++++------ src/rs/detect.rs | 29 +++++++++++++++++++++-------- src/rs/error.rs | 24 ++++++++++++++++-------- src/rs/utils.rs | 16 ++++++++++++---- 4 files changed, 72 insertions(+), 26 deletions(-) diff --git a/src/rs/checksum.rs b/src/rs/checksum.rs index b00ed40..f0d2d35 100644 --- a/src/rs/checksum.rs +++ b/src/rs/checksum.rs @@ -31,9 +31,15 @@ fn read_word_from_ram(rom_words: &[u32], entrypoint_ram: u32, ram_addr: u32) -> /// let checksum = ipl3checksum::calculate_checksum(&bytes, &kind).unwrap(); /// println!("{:08X} {:08X}", checksum.0, checksum.1); /// ``` -pub fn calculate_checksum(rom_bytes: &[u8], kind: &CICKind) -> Result<(u32, u32), Ipl3ChecksumError> { +pub fn calculate_checksum( + rom_bytes: &[u8], + kind: &CICKind, +) -> Result<(u32, u32), Ipl3ChecksumError> { if rom_bytes.len() < 0x101000 { - return Err(Ipl3ChecksumError::BufferNotBigEnough{buffer_len: rom_bytes.len(), expected_len: 0x101000}); + return Err(Ipl3ChecksumError::BufferNotBigEnough { + buffer_len: rom_bytes.len(), + expected_len: 0x101000, + }); } let rom_words = utils::read_u32_vec(rom_bytes, 0, 0x101000 / 4)?; @@ -300,22 +306,33 @@ pub(crate) mod python_bindings { use pyo3::prelude::*; #[pyfunction] - pub(crate) fn calculateChecksum(rom_bytes: &[u8], kind: &super::CICKind) -> Result, super::Ipl3ChecksumError> { + pub(crate) fn calculateChecksum( + rom_bytes: &[u8], + kind: &super::CICKind, + ) -> Result, super::Ipl3ChecksumError> { match super::calculate_checksum(rom_bytes, kind) { Ok(checksum) => Ok(Some(checksum)), Err(e) => match e { - super::Ipl3ChecksumError::BufferNotBigEnough { buffer_len, expected_len } => Ok(None), + super::Ipl3ChecksumError::BufferNotBigEnough { + buffer_len: _, + expected_len: _, + } => Ok(None), _ => Err(e), // To trigger an exception on Python's side }, } } #[pyfunction] - pub(crate) fn calculateChecksumAutodetect(rom_bytes: &[u8]) -> Result, super::Ipl3ChecksumError> { + pub(crate) fn calculateChecksumAutodetect( + rom_bytes: &[u8], + ) -> Result, super::Ipl3ChecksumError> { match super::calculate_checksum_autodetect(rom_bytes) { Ok(checksum) => Ok(Some(checksum)), Err(e) => match e { - super::Ipl3ChecksumError::BufferNotBigEnough { buffer_len, expected_len } => Ok(None), + super::Ipl3ChecksumError::BufferNotBigEnough { + buffer_len: _, + expected_len: _, + } => Ok(None), _ => Err(e), // To trigger an exception on Python's side }, } diff --git a/src/rs/detect.rs b/src/rs/detect.rs index 20fe940..ecdf9f7 100644 --- a/src/rs/detect.rs +++ b/src/rs/detect.rs @@ -16,14 +16,17 @@ use crate::{cickinds::CICKind, error::Ipl3ChecksumError, utils}; /// * The detected CIC kind, or `Ipl3ChecksumError` if was not able to detect the CIC kind. pub fn detect_cic_raw(raw_bytes: &[u8]) -> Result { if raw_bytes.len() != 0xFC0 { - return Err(Ipl3ChecksumError::BufferSizeIsWrong{buffer_len:raw_bytes.len() , expected_len: 0xFC0}); + return Err(Ipl3ChecksumError::BufferSizeIsWrong { + buffer_len: raw_bytes.len(), + expected_len: 0xFC0, + }); } let bytes_hash = utils::get_hash_md5(raw_bytes); match CICKind::from_hash_md5(&bytes_hash) { Some(cic) => Ok(cic), - None => Err(Ipl3ChecksumError::UnableToDetectCIC{hash: bytes_hash}), + None => Err(Ipl3ChecksumError::UnableToDetectCIC { hash: bytes_hash }), } } @@ -48,24 +51,34 @@ pub(crate) mod python_bindings { use pyo3::prelude::*; #[pyfunction] - pub(crate) fn detectCICRaw(raw_bytes: &[u8]) -> Result, super::Ipl3ChecksumError> { + pub(crate) fn detectCICRaw( + raw_bytes: &[u8], + ) -> Result, super::Ipl3ChecksumError> { match super::detect_cic_raw(raw_bytes) { Ok(cic) => Ok(Some(cic)), Err(e) => match e { - super::Ipl3ChecksumError::BufferSizeIsWrong { buffer_len, expected_len } => Ok(None), - super::Ipl3ChecksumError::UnableToDetectCIC { hash } => Ok(None), + super::Ipl3ChecksumError::BufferSizeIsWrong { + buffer_len: _, + expected_len: _, + } => Ok(None), + super::Ipl3ChecksumError::UnableToDetectCIC { hash: _ } => Ok(None), _ => Err(e), // To trigger an exception on Python's side }, } } #[pyfunction] - pub(crate) fn detectCIC(rom_bytes: &[u8]) -> Result, super::Ipl3ChecksumError> { + pub(crate) fn detectCIC( + rom_bytes: &[u8], + ) -> Result, super::Ipl3ChecksumError> { match super::detect_cic(rom_bytes) { Ok(cic) => Ok(Some(cic)), Err(e) => match e { - super::Ipl3ChecksumError::BufferSizeIsWrong { buffer_len, expected_len } => Ok(None), - super::Ipl3ChecksumError::UnableToDetectCIC { hash } => Ok(None), + super::Ipl3ChecksumError::BufferSizeIsWrong { + buffer_len: _, + expected_len: _, + } => Ok(None), + super::Ipl3ChecksumError::UnableToDetectCIC { hash: _ } => Ok(None), _ => Err(e), // To trigger an exception on Python's side }, } diff --git a/src/rs/error.rs b/src/rs/error.rs index 315b774..aa7998f 100644 --- a/src/rs/error.rs +++ b/src/rs/error.rs @@ -1,8 +1,6 @@ /* SPDX-FileCopyrightText: © 2023 Decompollaborate */ /* SPDX-License-Identifier: MIT */ -use thiserror; - #[cfg(feature = "python_bindings")] use pyo3::exceptions::PyRuntimeError; #[cfg(feature = "python_bindings")] @@ -15,19 +13,29 @@ pub enum Ipl3ChecksumError { #[error("Not an error")] Okay, #[error("Unaligned read at offset 0x{offset:X}")] - UnalignedRead{ offset: usize }, + UnalignedRead { offset: usize }, #[error("Failed to convert bytes at offset 0x{offset:X}")] - ByteConversion{ offset: usize }, + ByteConversion { offset: usize }, #[error("Tried to access data out of bounds at offset 0x{offset:X}. Requested bytes: 0x{requested_bytes:X}. Buffer length: 0x{buffer_len:X}")] - OutOfBounds{offset: usize, requested_bytes: usize, buffer_len: usize}, + OutOfBounds { + offset: usize, + requested_bytes: usize, + buffer_len: usize, + }, #[error("Pointer is null")] NullPointer, #[error("The input byte buffer is not big enough. It should be at least 0x{expected_len:X} bytes long, but it was 0x{buffer_len:X} bytes")] - BufferNotBigEnough{ buffer_len: usize, expected_len: usize }, + BufferNotBigEnough { + buffer_len: usize, + expected_len: usize, + }, #[error("The input byte buffer didn't have the expected size. It should be exactly 0x{expected_len:X} bytes long, but it was 0x{buffer_len:X} bytes")] - BufferSizeIsWrong{ buffer_len: usize, expected_len: usize }, + BufferSizeIsWrong { + buffer_len: usize, + expected_len: usize, + }, #[error("Unable to detect the CIC variant because the computed hash did not match any of the known variants. Computed hash: {hash}")] - UnableToDetectCIC{ hash: String }, + UnableToDetectCIC { hash: String }, } #[cfg(feature = "python_bindings")] diff --git a/src/rs/utils.rs b/src/rs/utils.rs index 00bb9e7..35a67d1 100644 --- a/src/rs/utils.rs +++ b/src/rs/utils.rs @@ -5,20 +5,28 @@ use crate::error::Ipl3ChecksumError; pub(crate) fn read_u32(bytes: &[u8], offset: usize) -> Result { if offset % 4 != 0 { - return Err(Ipl3ChecksumError::UnalignedRead{ offset }); + return Err(Ipl3ChecksumError::UnalignedRead { offset }); } if offset + 4 > bytes.len() { - return Err(Ipl3ChecksumError::OutOfBounds{offset, requested_bytes: 4, buffer_len: bytes.len()}); + return Err(Ipl3ChecksumError::OutOfBounds { + offset, + requested_bytes: 4, + buffer_len: bytes.len(), + }); } match bytes[offset..offset + 4].try_into() { Ok(bytes) => Ok(u32::from_be_bytes(bytes)), - Err(_error) => Err(Ipl3ChecksumError::ByteConversion{offset}), + Err(_error) => Err(Ipl3ChecksumError::ByteConversion { offset }), } } -pub(crate) fn read_u32_vec(bytes: &[u8], offset: usize, len: usize) -> Result, Ipl3ChecksumError> { +pub(crate) fn read_u32_vec( + bytes: &[u8], + offset: usize, + len: usize, +) -> Result, Ipl3ChecksumError> { let mut ret = Vec::with_capacity(len); for i in 0..len {