Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: more require macro docs #1268

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 22 additions & 12 deletions lang/src/lib.rs
Expand Up @@ -317,24 +317,34 @@ pub mod __private {
/// Use this with a custom error type.
///
/// # Example
///
/// After defining an `ErrorCode`
///
/// ```ignore
/// // Instruction function
/// pub fn set_data(ctx: Context<SetData>, data: u64) -> ProgramResult {
/// require!(ctx.accounts.data.mutation_allowed, MyError::MutationForbidden);
/// ctx.accounts.data.data = data;
/// Ok(())
/// }
///
/// // An enum for custom error codes
/// #[error]
/// pub struct ErrorCode {
/// InvalidArgument,
/// pub enum MyError {
/// MutationForbidden
/// }
/// ```
///
/// One can write a `require` assertion as
/// // An account definition
/// #[account]
/// #[derive(Default)]
/// pub struct MyData {
/// mutation_allowed: bool,
/// data: u64
/// }
///
/// ```ignore
/// require!(condition, InvalidArgument);
/// // An account validation struct
/// #[derive(Accounts)]
/// pub struct SetData<'info> {
/// pub data: Account<'info, MyData>
/// }
/// ```
///
/// which would exit the program with the `InvalidArgument` error code if
/// `condition` is false.
#[macro_export]
macro_rules! require {
($invariant:expr, $error:tt $(,)?) => {
Expand Down