From 49ad086edc0558ed4866e447687cf3566bfa4c34 Mon Sep 17 00:00:00 2001 From: Paul Date: Sat, 8 Jan 2022 21:24:58 +0100 Subject: [PATCH] docs: more require macro docs (#1268) --- lang/src/lib.rs | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/lang/src/lib.rs b/lang/src/lib.rs index a5e15a6ce..f69a77dd8 100644 --- a/lang/src/lib.rs +++ b/lang/src/lib.rs @@ -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, 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 +/// } +/// +/// // An account definition +/// #[account] +/// #[derive(Default)] +/// pub struct MyData { +/// mutation_allowed: bool, +/// data: u64 +/// } +/// +/// // An account validation struct +/// #[derive(Accounts)] +/// pub struct SetData<'info> { +/// pub data: Account<'info, MyData> /// } /// ``` -/// -/// One can write a `require` assertion as -/// -/// ```ignore -/// require!(condition, InvalidArgument); -/// ``` -/// -/// which would exit the program with the `InvalidArgument` error code if -/// `condition` is false. #[macro_export] macro_rules! require { ($invariant:expr, $error:tt $(,)?) => {