Skip to content

Commit

Permalink
get rid of thiserror
Browse files Browse the repository at this point in the history
  • Loading branch information
Maccraft123 committed Nov 29, 2023
1 parent eeca64c commit 3a095b4
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 69 deletions.
33 changes: 12 additions & 21 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cytryna/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ bmp = { version = "0.5", optional = true }
cbc = { version = "0.1", features = ["alloc"], optional = true }
ctr = { version = "0.9", features = ["std"], optional = true }
derivative = { version = "2.2", optional = true }
derive_more = { version = "0.99.17", features = ["from", "display", "error"], default-features = false }
hex = "0.4"
hex-literal = "0.4"
image = { version = "0.24", default-features = false, optional = true }
memoffset = "0.9"
sha2 = { version = "0.10", optional = true }
static_assertions = "1.1"
thiserror = "1.0"

[dev-dependencies]
rand = "0.8"
16 changes: 10 additions & 6 deletions cytryna/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::sync::OnceLock;
use crate::string::SizedCString;
use crate::{CytrynaError, CytrynaResult, FromBytes};

use thiserror::Error;
use derive_more::{Display, Error, From};

pub mod aes128_ctr {
pub use aes::cipher::block_padding::NoPadding;
Expand Down Expand Up @@ -126,13 +126,17 @@ impl fmt::Display for KeyIndex {
}

/// An error type for KeyIndex parsing
#[derive(Error, Debug)]
#[derive(Debug, Display, Error, From)]
pub enum KeyIndexParseError {
#[error("Failed to parse a hex number")]
NumberParseError(#[from] num::ParseIntError),
#[error("Invalid key type \"{0}\"")]
#[display(fmt = "Failed to parse a hex number")]
NumberParseError(num::ParseIntError),
#[error(ignore)]
#[from(ignore)]
#[display(fmt = "Invalid key type \"{_0}\"")]
InvalidKeyType(String),
#[error("Invalid X/Y/Z key type \"{0}\"")]
#[error(ignore)]
#[from(ignore)]
#[display(fmt = "Invalid X/Y/N key type \"{_0}\"")]
InvalidKeyXYNType(String),
}

Expand Down
78 changes: 78 additions & 0 deletions cytryna/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use core::fmt;

use crate::crypto;

/// Low-effort catch-all error type for cytryna library
#[non_exhaustive]
#[derive(Debug)]
pub enum CytrynaError {
InvalidMagic,
MissingRegion,
InvalidHeaderSize,
InvalidHash,
SignatureCorrupted,
InvalidRegionPosition,
UnsupportedHeaderVersion,
#[cfg(feature = "crypto")]
MissingKey(crypto::KeyIndex),
#[cfg(feature = "crypto")]
NoKeyBag,
EnumValueOutOfRange(&'static str),
SliceTooSmall,
InvalidLength{
what: &'static str,
actual: usize,
expected: usize,
},
#[cfg(feature = "crypto")]
KeyIndexFail(crypto::KeyIndexParseError),
#[cfg(feature = "crypto")]
StreamCrypt(ctr::cipher::StreamCipherError),
HexError(hex::FromHexError),
BadAlign,
}

impl fmt::Display for CytrynaError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidMagic => f.write_str("Invalid magic bytes"),
Self::MissingRegion => f.write_str("Missing region"),
Self::InvalidHeaderSize => f.write_str("Invalid header size"),
Self::InvalidHash => f.write_str("Invalid hash"),
Self::SignatureCorrupted => f.write_str("Signature corrupted"),
Self::InvalidRegionPosition => f.write_str("Invalid region position"),
Self::UnsupportedHeaderVersion => f.write_str("Unsupported header version"),
#[cfg(feature = "crypto")]
Self::MissingKey(idx) => f.write_str(&format!("Missing {idx} key")),
#[cfg(feature = "crypto")]
Self::NoKeyBag => f.write_str("Unitialized keybag"),
Self::EnumValueOutOfRange(name) => f.write_str(&format!("Value of out range for {name} enum")),
Self::SliceTooSmall => f.write_str("Byte slice passed is too small"),
Self::InvalidLength {what, actual, expected} => f.write_str(&format!("Invalid length of {what}: {actual} (expected {expected})")),
#[cfg(feature = "crypto")]
Self::KeyIndexFail(_) => f.write_str("Key index parse error"),
#[cfg(feature = "crypto")]
Self::StreamCrypt(_) => f.write_str("Encryption/Decryption error"),
Self::HexError(_) => f.write_str("Failed to decode hex string"),
Self::BadAlign => f.write_str("Incorrect alignment"),
}
}
}

impl From<ctr::cipher::StreamCipherError> for CytrynaError {
fn from(err: ctr::cipher::StreamCipherError) -> Self {
CytrynaError::StreamCrypt(err)
}
}

impl From<crypto::KeyIndexParseError> for CytrynaError {
fn from(err: crypto::KeyIndexParseError) -> Self {
Self::KeyIndexFail(err)
}
}

impl From<hex::FromHexError> for CytrynaError {
fn from(err: hex::FromHexError) -> Self {
Self::HexError(err)
}
}
14 changes: 7 additions & 7 deletions cytryna/src/firm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use crate::string::SizedCString;
use crate::FromBytes;
use crate::{align_up, CytrynaError, CytrynaResult};

use derive_more::{Display, Error};
use hex_literal::hex;
use static_assertions::assert_eq_size;
use thiserror::Error;

// source: https://gist.github.com/SciresM/cdd2266efb80175d37eabbe86f9d8c52
static RETAIL_NAND_FIRM: [u8; 0x100] = hex!("B6724531C448657A2A2EE306457E350A10D544B42859B0E5B0BED27534CCCC2A4D47EDEA60A7DD99939950A6357B1E35DFC7FAC773B7E12E7C1481234AF141B31CF08E9F62293AA6BAAE246C15095F8B78402A684D852C680549FA5B3F14D9E838A2FB9C09A15ABB40DCA25E40A3DDC1F58E79CEC901974363A946E99B4346E8A372B6CD55A707E1EAB9BEC0200B5BA0B661236A8708D704517F43C6C38EE9560111E1405E5E8ED356C49C4FF6823D1219AFAEEB3DF3C36B62BBA88FC15BA8648F9333FD9FC092B8146C3D908F73155D48BE89D72612E18E4AA8EB9B7FD2A5F7328C4ECBFB0083833CBD5C983A25CEB8B941CC68EB017CE87F5D793ACA09ACF7");
Expand Down Expand Up @@ -122,17 +122,17 @@ pub enum CopyMethod {
}

/// An error type for FirmBuilder
#[derive(Error, Debug)]
#[derive(Debug, Error, Display)]
pub enum FirmBuilderError {
#[error("Tried to add more than firware 4 sections")]
#[display(fmt = "Tried to add more than firware 4 sections")]
TooManySections,
#[error("Arm9 entry point is missing")]
#[display(fmt = "Arm9 entry point is missing")]
NoArm9Entry,
#[error("Arm11 entry point is missing")]
#[display(fmt = "Arm11 entry point is missing")]
NoArm11Entry,
#[error("Firmware sections are missing")]
#[display(fmt = "Firmware sections are missing")]
NoSections,
#[error("Signature type is missing")]
#[display(fmt = "Signature type is missing")]
NoSig,
}

Expand Down
46 changes: 25 additions & 21 deletions cytryna/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,51 +24,55 @@ pub mod tmd;

use core::ops::Deref;

use thiserror::Error;
use derive_more::{Display, Error, From};

/// Low-effort catch-all error type for cytryna library
#[non_exhaustive]
#[derive(Error, Debug)]
#[derive(Display, Debug, Error, From)]
pub enum CytrynaError {
#[error("Invalid magic bytes")]
#[display(fmt = "Invalid magic bytes")]
InvalidMagic,
#[error("Missing region")]
#[display(fmt = "Missing region")]
MissingRegion,
#[error("Invalid size of header")]
#[display(fmt = "Invalid size of header")]
InvalidHeaderSize,
#[error("Invalid hash")]
#[display(fmt = "Invalid hash")]
InvalidHash,
#[error("Signature corrupted/forged")]
#[display(fmt = "Signature corrupted/forged")]
SignatureCorrupted,
#[error("Invalid region position")]
#[display(fmt = "Invalid region position")]
InvalidRegionPosition,
#[error("Unsupported version of header")]
#[display(fmt = "Unsupported version of header")]
UnsupportedHeaderVersion,
#[cfg(feature = "crypto")]
#[error("Missing {0} key")]
#[error(ignore)]
#[display(fmt = "Missing {_0} key")]
MissingKey(crypto::KeyIndex),
#[cfg(feature = "crypto")]
#[error("Uninitialized keybag")]
#[display(fmt = "Uninitialized keybag")]
NoKeyBag,
#[error("Value out of range for {0} enum")]
#[error(ignore)]
#[from(ignore)]
#[display(fmt = "Value out of range for {_0} enum")]
EnumValueOutOfRange(&'static str),
#[error("Byte slice passed is too small")]
#[display(fmt = "Byte slice passed is too small")]
SliceTooSmall,
#[error("Invalid length of {what}: {actual} (expected {expected})")]
#[from(ignore)]
#[display(fmt = "Invalid length of {what}: {actual} (expected {expected})")]
InvalidLength{
what: &'static str,
actual: usize,
expected: usize,
},
#[cfg(feature = "crypto")]
#[error("Failed to parse keyindex")]
KeyIndexFail(#[from] crypto::KeyIndexParseError),
#[display(fmt = "Failed to parse keyindex")]
KeyIndexFail(crypto::KeyIndexParseError),
#[cfg(feature = "crypto")]
#[error("Failed to stream-encrypt/decrypt data")]
StreamCrypt(#[from] ctr::cipher::StreamCipherError),
#[error("Failed to decode hex string")]
HexError(#[from] hex::FromHexError),
#[error("Incorrect alignment")]
#[display(fmt = "Failed to stream-encrypt/decrypt data")]
StreamCrypt(ctr::cipher::StreamCipherError),
#[display(fmt = "Failed to decode hex string")]
HexError(hex::FromHexError),
#[display(fmt = "Incorrect alignment")]
BadAlign,
}

Expand Down
20 changes: 10 additions & 10 deletions cytryna/src/smdh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,25 @@ use crate::{CytrynaError, CytrynaResult, FromBytes};
use bitfield_struct::bitfield;
use bitflags::bitflags;
use bmp::{px, Pixel};
use derive_more::{Display, Error, From};
use static_assertions::assert_eq_size;
use thiserror::Error;

/// SMDH error type
#[derive(Error, Debug)]
#[derive(Debug, Display, Error, From)]
pub enum SmdhError {
#[error("Missing short description")]
#[display(fmt = "Missing short description")]
MissingShortDesc,
#[error("Missing long description")]
#[display(fmt = "Missing long description")]
MissingLongDesc,
#[error("Missing publisher name")]
#[display(fmt = "Missing publisher name")]
MissingPublisher,
#[error("Missing icon data")]
#[display(fmt = "Missing icon data")]
MissingIcon,
#[error("SizedCString error: {0}")]
StringErr(#[from] SizedCStringError),
#[error("Invalid image size, got: {got}, expected: {expected}")]
#[display(fmt = "SizedCString error: {_0}")]
StringErr(SizedCStringError),
#[display(fmt = "Invalid image size, got: {got}, expected: {expected}")]
InvalidImageSize { got: u32, expected: u32 },
#[error("Only square images can be SMDH icons")]
#[display(fmt = "Only square images can be SMDH icons")]
OnlySquaresAllowed,
}

Expand Down
6 changes: 3 additions & 3 deletions cytryna/src/string.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use alloc::{borrow::Cow, string};
use core::{fmt, str};

use thiserror::Error;
use derive_more::{Display, Error};

/// An error for SizedCString construction
#[derive(Error, Debug)]
#[derive(Debug, Display, Error)]
pub enum SizedCStringError {
#[error("Input string too big to fit into storage")]
#[display(fmt = "Input string too big to fit into storage")]
TooBig,
}

Expand Down

0 comments on commit 3a095b4

Please sign in to comment.