Browse Source

docs: more require macro docs (#1268)

Paul 3 years ago
parent
commit
49ad086edc
1 changed files with 22 additions and 12 deletions
  1. 22 12
      lang/src/lib.rs

+ 22 - 12
lang/src/lib.rs

@@ -317,24 +317,34 @@ pub mod __private {
 /// Use this with a custom error type.
 /// Use this with a custom error type.
 ///
 ///
 /// # Example
 /// # Example
-///
-/// After defining an `ErrorCode`
-///
 /// ```ignore
 /// ```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]
 /// #[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_export]
 macro_rules! require {
 macro_rules! require {
     ($invariant:expr, $error:tt $(,)?) => {
     ($invariant:expr, $error:tt $(,)?) => {