11//! Various assertions.
22
3- /// Asserts that two accounts share the same key.
3+ /// Formats an error as a `&str`.
4+ #[ macro_export]
5+ macro_rules! format_err {
6+ ( $err: expr) => {
7+ & * format!( "{:?}: {}" , $err, $err)
8+ } ;
9+ }
10+
11+ /// Returns the given error as a program error.
412///
5- /// Deprecated in favor of [assert_keys_eq].
6- #[ deprecated]
13+ /// # Example
14+ ///
15+ /// ```
16+ /// # use anchor_lang::prelude::*;
17+ /// # impl From<ErrorCode> for ProgramError { fn from(code: ErrorCode) -> Self { ProgramError::Custom(10) } }
18+ /// # pub enum ErrorCode { MyError }
19+ /// # #[macro_use] extern crate vipers; fn main() -> ProgramResult {
20+ /// let fail = false;
21+ /// if fail {
22+ /// return program_err!(MyError);
23+ /// }
24+ /// Ok(())
25+ /// # }
26+ /// ```
727#[ macro_export]
8- macro_rules! assert_keys {
9- ( $account_a : expr , $account_b : expr $( , ) ?) => {
10- $ crate:: assert_keys_eq! ( $account_a , $account_b , "key mismatch" )
28+ macro_rules! program_err {
29+ ( $error : tt $( , ) ?) => {
30+ Err ( crate :: ErrorCode :: $error . into ( ) )
1131 } ;
12- ( $account_a: expr, $account_b: expr, $msg: expr $( , ) ?) => {
13- $crate:: assert_keys_eq!( $account_a, $account_b, $msg)
32+ }
33+
34+ /// Logs where in the code the macro was invoked.
35+ #[ macro_export]
36+ macro_rules! log_code_location {
37+ ( ) => {
38+ msg!( "Error thrown at {}:{}" , file!( ) , line!( ) ) ;
39+ } ;
40+ }
41+
42+ /// Throws an error.
43+ ///
44+ /// # Example
45+ ///
46+ /// ```
47+ /// # use anchor_lang::prelude::*;
48+ /// # impl From<ErrorCode> for ProgramError { fn from(code: ErrorCode) -> Self { ProgramError::Custom(10) } }
49+ /// # pub enum ErrorCode { MyError }
50+ /// # #[macro_use] extern crate vipers; fn main() -> ProgramResult {
51+ /// let fail = false;
52+ /// if fail {
53+ /// throw_err!(MyError);
54+ /// }
55+ /// Ok(())
56+ /// # }
57+ /// ```
58+ #[ macro_export]
59+ macro_rules! throw_err {
60+ ( $error: ident $( , ) ?) => {
61+ throw_err!( crate :: ErrorCode :: $error) ;
62+ } ;
63+ ( $error: expr $( , ) ?) => {
64+ $crate:: log_code_location!( ) ;
65+ return Err ( $error. into( ) ) ;
1466 } ;
1567}
1668
@@ -148,7 +200,7 @@ macro_rules! assert_keys_eq {
148200 ) ;
149201 } ;
150202 ( $account_a: expr, $account_b: expr, $err: expr $( , ) ?) => {
151- assert_keys_eq!( $account_a, $account_b, $err, format_err!( $err) ) ;
203+ assert_keys_eq!( $account_a, $account_b, $err, $crate :: format_err!( $err) ) ;
152204 } ;
153205 ( $account_a: expr, $account_b: expr, $err: expr, $msg: expr $( , ) ?) => {
154206 let __account_a = $account_a. key( ) ;
@@ -199,7 +251,7 @@ macro_rules! assert_keys_neq {
199251 ) ;
200252 } ;
201253 ( $account_a: expr, $account_b: expr, $err: expr $( , ) ?) => {
202- assert_keys_neq!( $account_a, $account_b, $err, format_err!( $err) ) ;
254+ assert_keys_neq!( $account_a, $account_b, $err, $crate :: format_err!( $err) ) ;
203255 } ;
204256 ( $account_a: expr, $account_b: expr, $err: expr, $msg: expr $( , ) ?) => {
205257 let __account_a = $account_a. key( ) ;
@@ -252,7 +304,7 @@ macro_rules! unwrap_or_err {
252304#[ macro_export]
253305macro_rules! unwrap_int {
254306 ( $option: expr $( , ) ?) => {
255- unwrap_opt!( $option, $crate:: VipersError :: IntegerOverflow )
307+ $crate :: unwrap_opt!( $option, $crate:: VipersError :: IntegerOverflow )
256308 } ;
257309}
258310
@@ -279,64 +331,6 @@ macro_rules! try_or_err {
279331 } ;
280332}
281333
282- /// Returns the given error as a program error.
283- ///
284- /// # Example
285- ///
286- /// ```
287- /// # use anchor_lang::prelude::*;
288- /// # impl From<ErrorCode> for ProgramError { fn from(code: ErrorCode) -> Self { ProgramError::Custom(10) } }
289- /// # pub enum ErrorCode { MyError }
290- /// # #[macro_use] extern crate vipers; fn main() -> ProgramResult {
291- /// let fail = false;
292- /// if fail {
293- /// return program_err!(MyError);
294- /// }
295- /// Ok(())
296- /// # }
297- /// ```
298- #[ macro_export]
299- macro_rules! program_err {
300- ( $error: tt $( , ) ?) => {
301- Err ( crate :: ErrorCode :: $error. into( ) )
302- } ;
303- }
304-
305- /// Throws an error.
306- ///
307- /// # Example
308- ///
309- /// ```
310- /// # use anchor_lang::prelude::*;
311- /// # impl From<ErrorCode> for ProgramError { fn from(code: ErrorCode) -> Self { ProgramError::Custom(10) } }
312- /// # pub enum ErrorCode { MyError }
313- /// # #[macro_use] extern crate vipers; fn main() -> ProgramResult {
314- /// let fail = false;
315- /// if fail {
316- /// throw_err!(MyError);
317- /// }
318- /// Ok(())
319- /// # }
320- /// ```
321- #[ macro_export]
322- macro_rules! throw_err {
323- ( $error: ident $( , ) ?) => {
324- throw_err!( crate :: ErrorCode :: $error) ;
325- } ;
326- ( $error: expr $( , ) ?) => {
327- $crate:: log_code_location!( ) ;
328- return Err ( $error. into( ) ) ;
329- } ;
330- }
331-
332- /// Logs where in the code the macro was invoked.
333- #[ macro_export]
334- macro_rules! log_code_location {
335- ( ) => {
336- msg!( "Error thrown at {}:{}" , file!( ) , line!( ) ) ;
337- } ;
338- }
339-
340334/// Asserts that an invariant holds, otherwise logs the given message.
341335/// This is a drop-in replacement for `require!`.
342336///
@@ -357,24 +351,16 @@ macro_rules! invariant {
357351 invariant!( $invariant, $crate:: VipersError :: InvariantFailed , $msg) ;
358352 } ;
359353 ( $invariant: expr, $err: expr $( , ) ?) => {
360- invariant!( $invariant, $err, format_err!( $err) ) ;
354+ invariant!( $invariant, $err, $crate :: format_err!( $err) ) ;
361355 } ;
362356 ( $invariant: expr, $err: expr, $msg: expr $( , ) ?) => {
363357 if !( $invariant) {
364358 msg!( "Invariant failed: {:?}" , $err) ;
365- throw_err!( $crate:: VipersError :: InvariantFailed ) ;
359+ $crate :: throw_err!( $crate:: VipersError :: InvariantFailed ) ;
366360 }
367361 } ;
368362}
369363
370- /// Formats an error as a `&str`.
371- #[ macro_export]
372- macro_rules! format_err {
373- ( $err: expr) => {
374- & * format!( "{:?}: {}" , $err, $err)
375- } ;
376- }
377-
378364/// Attempts to unwrap an [Option], and if it fails, prints an error.
379365///
380366/// # Example
@@ -396,7 +382,7 @@ macro_rules! unwrap_opt {
396382 unwrap_opt!( $option, $crate:: VipersError :: OptionUnwrapFailed , $msg)
397383 } ;
398384 ( $option: expr, $err: expr $( , ) ?) => {
399- unwrap_opt!( $option, $err, format_err!( $err) )
385+ unwrap_opt!( $option, $err, $crate :: format_err!( $err) )
400386 } ;
401387 ( $option: expr, $err: expr, $msg: expr $( , ) ?) => {
402388 $option. ok_or_else( || -> ProgramError {
@@ -407,6 +393,20 @@ macro_rules! unwrap_opt {
407393 } ;
408394}
409395
396+ /// Asserts that two accounts share the same key.
397+ ///
398+ /// Deprecated in favor of [assert_keys_eq].
399+ #[ deprecated]
400+ #[ macro_export]
401+ macro_rules! assert_keys {
402+ ( $account_a: expr, $account_b: expr $( , ) ?) => {
403+ $crate:: assert_keys_eq!( $account_a, $account_b, "key mismatch" )
404+ } ;
405+ ( $account_a: expr, $account_b: expr, $msg: expr $( , ) ?) => {
406+ $crate:: assert_keys_eq!( $account_a, $account_b, $msg)
407+ } ;
408+ }
409+
410410#[ cfg( test) ]
411411mod tests {
412412 use anchor_lang:: prelude:: * ;
0 commit comments