Skip to content

Commit

Permalink
More metavariable reordering
Browse files Browse the repository at this point in the history
  • Loading branch information
macalinao committed Nov 13, 2021
1 parent 21aff4c commit 553f80d
Showing 1 changed file with 80 additions and 80 deletions.
160 changes: 80 additions & 80 deletions src/assert.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,68 @@
//! Various assertions.
/// Asserts that two accounts share the same key.
/// Formats an error as a `&str`.
#[macro_export]
macro_rules! format_err {
($err: expr) => {
&*format!("{:?}: {}", $err, $err)
};
}

/// Returns the given error as a program error.
///
/// Deprecated in favor of [assert_keys_eq].
#[deprecated]
/// # Example
///
/// ```
/// # use anchor_lang::prelude::*;
/// # impl From<ErrorCode> for ProgramError { fn from(code: ErrorCode) -> Self { ProgramError::Custom(10) } }
/// # pub enum ErrorCode { MyError }
/// # #[macro_use] extern crate vipers; fn main() -> ProgramResult {
/// let fail = false;
/// if fail {
/// return program_err!(MyError);
/// }
/// Ok(())
/// # }
/// ```
#[macro_export]
macro_rules! assert_keys {
($account_a: expr, $account_b: expr $(,)?) => {
$crate::assert_keys_eq!($account_a, $account_b, "key mismatch")
macro_rules! program_err {
($error:tt $(,)?) => {
Err(crate::ErrorCode::$error.into())
};
($account_a: expr, $account_b: expr, $msg: expr $(,)?) => {
$crate::assert_keys_eq!($account_a, $account_b, $msg)
}

/// Logs where in the code the macro was invoked.
#[macro_export]
macro_rules! log_code_location {
() => {
msg!("Error thrown at {}:{}", file!(), line!());
};
}

/// Throws an error.
///
/// # Example
///
/// ```
/// # use anchor_lang::prelude::*;
/// # impl From<ErrorCode> for ProgramError { fn from(code: ErrorCode) -> Self { ProgramError::Custom(10) } }
/// # pub enum ErrorCode { MyError }
/// # #[macro_use] extern crate vipers; fn main() -> ProgramResult {
/// let fail = false;
/// if fail {
/// throw_err!(MyError);
/// }
/// Ok(())
/// # }
/// ```
#[macro_export]
macro_rules! throw_err {
($error:ident $(,)?) => {
throw_err!(crate::ErrorCode::$error);
};
($error:expr $(,)?) => {
$crate::log_code_location!();
return Err($error.into());
};
}

Expand Down Expand Up @@ -148,7 +200,7 @@ macro_rules! assert_keys_eq {
);
};
($account_a: expr, $account_b: expr, $err: expr $(,)?) => {
assert_keys_eq!($account_a, $account_b, $err, format_err!($err));
assert_keys_eq!($account_a, $account_b, $err, $crate::format_err!($err));
};
($account_a: expr, $account_b: expr, $err: expr, $msg: expr $(,)?) => {
let __account_a = $account_a.key();
Expand Down Expand Up @@ -199,7 +251,7 @@ macro_rules! assert_keys_neq {
);
};
($account_a: expr, $account_b: expr, $err: expr $(,)?) => {
assert_keys_neq!($account_a, $account_b, $err, format_err!($err));
assert_keys_neq!($account_a, $account_b, $err, $crate::format_err!($err));
};
($account_a: expr, $account_b: expr, $err: expr, $msg: expr $(,)?) => {
let __account_a = $account_a.key();
Expand Down Expand Up @@ -252,7 +304,7 @@ macro_rules! unwrap_or_err {
#[macro_export]
macro_rules! unwrap_int {
($option:expr $(,)?) => {
unwrap_opt!($option, $crate::VipersError::IntegerOverflow)
$crate::unwrap_opt!($option, $crate::VipersError::IntegerOverflow)
};
}

Expand All @@ -279,64 +331,6 @@ macro_rules! try_or_err {
};
}

/// Returns the given error as a program error.
///
/// # Example
///
/// ```
/// # use anchor_lang::prelude::*;
/// # impl From<ErrorCode> for ProgramError { fn from(code: ErrorCode) -> Self { ProgramError::Custom(10) } }
/// # pub enum ErrorCode { MyError }
/// # #[macro_use] extern crate vipers; fn main() -> ProgramResult {
/// let fail = false;
/// if fail {
/// return program_err!(MyError);
/// }
/// Ok(())
/// # }
/// ```
#[macro_export]
macro_rules! program_err {
($error:tt $(,)?) => {
Err(crate::ErrorCode::$error.into())
};
}

/// Throws an error.
///
/// # Example
///
/// ```
/// # use anchor_lang::prelude::*;
/// # impl From<ErrorCode> for ProgramError { fn from(code: ErrorCode) -> Self { ProgramError::Custom(10) } }
/// # pub enum ErrorCode { MyError }
/// # #[macro_use] extern crate vipers; fn main() -> ProgramResult {
/// let fail = false;
/// if fail {
/// throw_err!(MyError);
/// }
/// Ok(())
/// # }
/// ```
#[macro_export]
macro_rules! throw_err {
($error:ident $(,)?) => {
throw_err!(crate::ErrorCode::$error);
};
($error:expr $(,)?) => {
$crate::log_code_location!();
return Err($error.into());
};
}

/// Logs where in the code the macro was invoked.
#[macro_export]
macro_rules! log_code_location {
() => {
msg!("Error thrown at {}:{}", file!(), line!());
};
}

/// Asserts that an invariant holds, otherwise logs the given message.
/// This is a drop-in replacement for `require!`.
///
Expand All @@ -357,24 +351,16 @@ macro_rules! invariant {
invariant!($invariant, $crate::VipersError::InvariantFailed, $msg);
};
($invariant:expr, $err:expr $(,)?) => {
invariant!($invariant, $err, format_err!($err));
invariant!($invariant, $err, $crate::format_err!($err));
};
($invariant:expr, $err:expr, $msg: expr $(,)?) => {
if !($invariant) {
msg!("Invariant failed: {:?}", $err);
throw_err!($crate::VipersError::InvariantFailed);
$crate::throw_err!($crate::VipersError::InvariantFailed);
}
};
}

/// Formats an error as a `&str`.
#[macro_export]
macro_rules! format_err {
($err: expr) => {
&*format!("{:?}: {}", $err, $err)
};
}

/// Attempts to unwrap an [Option], and if it fails, prints an error.
///
/// # Example
Expand All @@ -396,7 +382,7 @@ macro_rules! unwrap_opt {
unwrap_opt!($option, $crate::VipersError::OptionUnwrapFailed, $msg)
};
($option:expr, $err:expr $(,)?) => {
unwrap_opt!($option, $err, format_err!($err))
unwrap_opt!($option, $err, $crate::format_err!($err))
};
($option:expr, $err:expr, $msg: expr $(,)?) => {
$option.ok_or_else(|| -> ProgramError {
Expand All @@ -407,6 +393,20 @@ macro_rules! unwrap_opt {
};
}

/// Asserts that two accounts share the same key.
///
/// Deprecated in favor of [assert_keys_eq].
#[deprecated]
#[macro_export]
macro_rules! assert_keys {
($account_a: expr, $account_b: expr $(,)?) => {
$crate::assert_keys_eq!($account_a, $account_b, "key mismatch")
};
($account_a: expr, $account_b: expr, $msg: expr $(,)?) => {
$crate::assert_keys_eq!($account_a, $account_b, $msg)
};
}

#[cfg(test)]
mod tests {
use anchor_lang::prelude::*;
Expand Down

0 comments on commit 553f80d

Please sign in to comment.