1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//! ## 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.
//!
//! ## Usage
//!
//! #### 1. Add the `Code` attribute to the `enum` type:
//!
//! ```
//! #[derive(enum_code::Code)]
//! enum TestError {
//! #[code(1)]
//! Tuple(String),
//! #[code(2)]
//! Struct { message: String },
//! #[code(3)]
//! Simple,
//! }
//! ```
//!
//! #### 2. Code Generation
//!
//! For the `TestError` enum above, an associated `impl TestError` struct is generated, which includes a `get_code` method that returns the corresponding error code based on the variant value.
//!
//! ```
//! impl TestError {
//! pub const fn get_code(&self) -> u32 {
//! match self {
//! TestError::Tuple(..) => 1u32,
//! TestError::Struct { .. } => 2u32,
//! TestError::Simple => 3u32,
//! }
//! }
//! }
//! ```
//!
//! #### 3. Retrieving Error Codes
//!
//! Error codes can be retrieved by calling `get_code`:
//!
//! ```
//! let err = TestError::Tuple("error message".to_owned());
//! let code = err.get_code();
//! println!("error code: {}", code); // should print 「error code: 1」
//! ```
use crateparse_code_stream;