Browse Source

Feature/error equality (#1544)

guibescos 3 years ago
parent
commit
0916361f5e
2 changed files with 19 additions and 1 deletions
  1. 1 0
      CHANGELOG.md
  2. 18 1
      lang/src/error.rs

+ 1 - 0
CHANGELOG.md

@@ -15,6 +15,7 @@ The minor version will be incremented upon a breaking change and the patch versi
 * cli: Add `--program-keypair` to `anchor deploy` ([#1786](https://github.com/project-serum/anchor/pull/1786)).
 * spl: Add more derived traits to `TokenAccount` to `Mint` ([#1818](https://github.com/project-serum/anchor/pull/1818)).
 * cli: Add compilation optimizations to cli template ([#1807](https://github.com/project-serum/anchor/pull/1807)).
+* lang: Add `PartialEq` and `Eq` for `anchor_lang::Error` ([#1544](https://github.com/project-serum/anchor/pull/1544)).
 
 ### Fixes
 

+ 18 - 1
lang/src/error.rs

@@ -195,7 +195,7 @@ pub enum ErrorCode {
     Deprecated = 5000,
 }
 
-#[derive(Debug)]
+#[derive(Debug, PartialEq, Eq)]
 pub enum Error {
     AnchorError(AnchorError),
     ProgramError(ProgramErrorWithOrigin),
@@ -302,6 +302,14 @@ pub struct ProgramErrorWithOrigin {
     pub compared_values: Option<ComparedValues>,
 }
 
+// Two ProgramErrors are equal when they have the same error code
+impl PartialEq for ProgramErrorWithOrigin {
+    fn eq(&self, other: &Self) -> bool {
+        self.program_error == other.program_error
+    }
+}
+impl Eq for ProgramErrorWithOrigin {}
+
 impl Display for ProgramErrorWithOrigin {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
         Display::fmt(&self.program_error, f)
@@ -458,6 +466,15 @@ impl Display for AnchorError {
     }
 }
 
+/// Two `AnchorError`s are equal when they have the same error code
+impl PartialEq for AnchorError {
+    fn eq(&self, other: &Self) -> bool {
+        self.error_code_number == other.error_code_number
+    }
+}
+
+impl Eq for AnchorError {}
+
 impl std::convert::From<Error> for anchor_lang::solana_program::program_error::ProgramError {
     fn from(e: Error) -> anchor_lang::solana_program::program_error::ProgramError {
         match e {