forked from solana-labs/solana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract precompile-error crate (solana-labs#2300)
* extract precompile-error crate * update PrecompileError usage * remove thiserror from precompile-error crate * fmt * remove num-derive * fix imports after rebase * sort deps * sort deps * fmt
- Loading branch information
1 parent
a458839
commit a8aef04
Showing
9 changed files
with
126 additions
and
29 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[package] | ||
name = "solana-precompile-error" | ||
description = "Solana PrecompileError type" | ||
documentation = "https://docs.rs/solana-precompile-error" | ||
version = { workspace = true } | ||
authors = { workspace = true } | ||
repository = { workspace = true } | ||
homepage = { workspace = true } | ||
license = { workspace = true } | ||
edition = { workspace = true } | ||
|
||
[dependencies] | ||
num-traits = { workspace = true } | ||
solana-decode-error = { workspace = true } | ||
|
||
[package.metadata.docs.rs] | ||
targets = ["x86_64-unknown-linux-gnu"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/// Precompile errors | ||
use {core::fmt, solana_decode_error::DecodeError}; | ||
|
||
/// Precompile errors | ||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
pub enum PrecompileError { | ||
InvalidPublicKey, | ||
InvalidRecoveryId, | ||
InvalidSignature, | ||
InvalidDataOffsets, | ||
InvalidInstructionDataSize, | ||
} | ||
|
||
impl num_traits::FromPrimitive for PrecompileError { | ||
#[inline] | ||
fn from_i64(n: i64) -> Option<Self> { | ||
if n == PrecompileError::InvalidPublicKey as i64 { | ||
Some(PrecompileError::InvalidPublicKey) | ||
} else if n == PrecompileError::InvalidRecoveryId as i64 { | ||
Some(PrecompileError::InvalidRecoveryId) | ||
} else if n == PrecompileError::InvalidSignature as i64 { | ||
Some(PrecompileError::InvalidSignature) | ||
} else if n == PrecompileError::InvalidDataOffsets as i64 { | ||
Some(PrecompileError::InvalidDataOffsets) | ||
} else if n == PrecompileError::InvalidInstructionDataSize as i64 { | ||
Some(PrecompileError::InvalidInstructionDataSize) | ||
} else { | ||
None | ||
} | ||
} | ||
#[inline] | ||
fn from_u64(n: u64) -> Option<Self> { | ||
Self::from_i64(n as i64) | ||
} | ||
} | ||
|
||
impl num_traits::ToPrimitive for PrecompileError { | ||
#[inline] | ||
fn to_i64(&self) -> Option<i64> { | ||
Some(match *self { | ||
PrecompileError::InvalidPublicKey => PrecompileError::InvalidPublicKey as i64, | ||
PrecompileError::InvalidRecoveryId => PrecompileError::InvalidRecoveryId as i64, | ||
PrecompileError::InvalidSignature => PrecompileError::InvalidSignature as i64, | ||
PrecompileError::InvalidDataOffsets => PrecompileError::InvalidDataOffsets as i64, | ||
PrecompileError::InvalidInstructionDataSize => { | ||
PrecompileError::InvalidInstructionDataSize as i64 | ||
} | ||
}) | ||
} | ||
#[inline] | ||
fn to_u64(&self) -> Option<u64> { | ||
self.to_i64().map(|x| x as u64) | ||
} | ||
} | ||
|
||
impl std::error::Error for PrecompileError {} | ||
|
||
impl fmt::Display for PrecompileError { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
match self { | ||
PrecompileError::InvalidPublicKey => f.write_str("public key is not valid"), | ||
PrecompileError::InvalidRecoveryId => f.write_str("id is not valid"), | ||
PrecompileError::InvalidSignature => f.write_str("signature is not valid"), | ||
PrecompileError::InvalidDataOffsets => f.write_str("offset not valid"), | ||
PrecompileError::InvalidInstructionDataSize => { | ||
f.write_str("instruction is incorrect size") | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl<T> DecodeError<T> for PrecompileError { | ||
fn type_of() -> &'static str { | ||
"PrecompileError" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters