enum-code
Introduction
enum-code is a derive macro for enum types. This library generates code that associates error codes with error types. It can be used in conjunction with the thiserror crate. Developers can quickly retrieve error codes by calling the get_code method.
Installation
enum-code is published on Cargo and can be installed using:
$ cargo add enum-code
Usage
-
Add the
Codeattribute to theenumtype: -
Code Generation
For the
TestErrorenum above, an associatedimpl TestErrorstruct is generated, which includes aget_codemethod that returns the corresponding error code based on the variant value. -
Retrieving Error Codes
Error codes can be retrieved by calling
get_code:let err = Tuple; let code = err.get_code; println!; // should print 「error code: 1」
LICENSE
MIT