-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* SPL errors from hashes * hashed error code is first variant only * add check for collision error codes * address feedback! * stupid `0`!
- Loading branch information
Joe C
authored
Sep 1, 2023
1 parent
25381b2
commit cfaabb5
Showing
17 changed files
with
300 additions
and
108 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
//! Token parsing | ||
use { | ||
proc_macro2::Ident, | ||
syn::{ | ||
parse::{Parse, ParseStream}, | ||
token::Comma, | ||
LitInt, Token, | ||
}, | ||
}; | ||
|
||
/// Possible arguments to the `#[spl_program_error]` attribute | ||
pub struct SplProgramErrorArgs { | ||
/// Whether to hash the error codes using `solana_program::hash` | ||
/// or to use the default error code assigned by `num_traits`. | ||
pub hash_error_code_start: Option<u32>, | ||
} | ||
|
||
impl Parse for SplProgramErrorArgs { | ||
fn parse(input: ParseStream) -> syn::Result<Self> { | ||
if input.is_empty() { | ||
return Ok(Self { | ||
hash_error_code_start: None, | ||
}); | ||
} | ||
match SplProgramErrorArgParser::parse(input)? { | ||
SplProgramErrorArgParser::HashErrorCodes { value, .. } => Ok(Self { | ||
hash_error_code_start: Some(value.base10_parse::<u32>()?), | ||
}), | ||
} | ||
} | ||
} | ||
|
||
/// Parser for args to the `#[spl_program_error]` attribute | ||
/// ie. `#[spl_program_error(hash_error_code_start = 1275525928)]` | ||
enum SplProgramErrorArgParser { | ||
HashErrorCodes { | ||
_ident: Ident, | ||
_equals_sign: Token![=], | ||
value: LitInt, | ||
_comma: Option<Comma>, | ||
}, | ||
} | ||
|
||
impl Parse for SplProgramErrorArgParser { | ||
fn parse(input: ParseStream) -> syn::Result<Self> { | ||
let _ident = { | ||
let ident = input.parse::<Ident>()?; | ||
if ident != "hash_error_code_start" { | ||
return Err(input.error("Expected argument 'hash_error_code_start'")); | ||
} | ||
ident | ||
}; | ||
let _equals_sign = input.parse::<Token![=]>()?; | ||
let value = input.parse::<LitInt>()?; | ||
let _comma: Option<Comma> = input.parse().unwrap_or(None); | ||
Ok(Self::HashErrorCodes { | ||
_ident, | ||
_equals_sign, | ||
value, | ||
_comma, | ||
}) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
//! Tests `#[derive(DecodeError)]` | ||
//! | ||
use spl_program_error::*; | ||
|
||
/// Example error | ||
|
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
//! Tests `#[derive(IntoProgramError)]` | ||
//! | ||
use spl_program_error::*; | ||
|
||
/// Example error | ||
|
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
Oops, something went wrong.