derive(Code) simplifies error handling by providing an easy-to-use enumeration of error codes

Overview

enum-code

Crates.io docs.rs GitHub

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

  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」

LICENSE

MIT

You might also like...
Rust library for hardware accelerated drawing of 2D shapes, images, and text, with an easy to use API.
Rust library for hardware accelerated drawing of 2D shapes, images, and text, with an easy to use API.

Speedy2D Hardware-accelerated drawing of shapes, images, and text, with an easy to use API. Speedy2D aims to be: The simplest Rust API for creating a

Small, clean, easy to use programming language

Thistle A modern, simplistic multi-paradigm language supporting object-oriented features Hello World! import IO object Main def main(): unit

Grimsby is an Erlang Port written in Rust that can close its standard input while retaining standard output (and error)

Grimsby An Erlang Port provides the basic mechanism for communication from Erlang with the external world. From the Ports and Port Drivers: Erlang Ref

A typesafe, flexible, simple, and user-friendly unit system library for Rust that has good error messages.

uy A typesafe, flexible, simple, and user-friendly unit system library for Rust that has good error messages. Usage uy not only stores the unit of a v

A simpler and 5x faster alternative to HashMap in Rust, which doesn't use hashing and doesn't use heap

At least 5x faster alternative of HashMap, for very small maps. It is also faster than FxHashMap, hashbrown, ArrayMap, and nohash-hasher. The smaller

An inquiry into nondogmatic software development. An experiment showing double performance of the code running on JVM comparing to equivalent native C code.
An inquiry into nondogmatic software development. An experiment showing double performance of the code running on JVM comparing to equivalent native C code.

java-2-times-faster-than-c An experiment showing double performance of the code running on JVM comparing to equivalent native C code ⚠️ The title of t

Rust explained using easy English
Rust explained using easy English

Update 22 December 2020: mdBook can be found here. 28 November 2020: Now also available in simplified Chinese thanks to kumakichi! 1 February 2021: No

Extracting archives made easy for Rust 🦀

Decompress A library that supports decompression of archives in multiple formats, inspired by ergonomics from Node's decompress. Includes a default st

VoceChat is a superlight rust written social server. Easy integration to your site/app.

Re-decentralized the Internet through personal cloud computing. VoceChat is the lightest chat server prioritizes private hosting! Easy integratation t

Releases(0.1.1)
Owner
Bay
I love document
Bay
🚀simple server that returns error codes with their respective messages and debug information, written in rust 🦀

ErrorServer ?? A simple & lightweight server that returns a HTML page of the error code with its respective message and debug information, written in

Jakob 2 Dec 15, 2022
🚃 lib for CLI utilities, printing, and error handling

axocli Common code for setting up a CLI App and handling errors/printing. Example See examples/axoapp.rs for a walkthrough/example. Some various inter

axo 5 Apr 4, 2023
Converts between country names, ISO 3166-1 codes and flag emojis.

country-emoji Converts between country names, ISO 3166-1 codes and flag emojis. Usage use country_emoji::{flag, code, name, countries}; flag("CL") /

Leo Dutra 4 Oct 25, 2022
Derive AsyncRead and AsyncWrite.

derive-tokio-io Derive AsyncRead and AsyncWrite. Usage If the struct has only one field, AsyncRead and AsyncWrite are derived for that field. use deri

Eray Karatay 3 Nov 17, 2022
Procedural macro to derive Serde serializer-deserializer for Prost

prost-serde-derive prost-serde-derive is a procedural macro to generate Serde serializers and deserializers for Prost-generated structs. Rationale Cur

Park Joon-Kyu 4 Dec 15, 2022
Derive `clone_from` for `Clone` in Rust

Derive Clone including clone_from This crate offers a derive macro called CloneFrom to derive Clone with a specialized clone_from implementation. When

Moritz Hoffmann 7 Mar 13, 2023
Derive conversion traits when items are structurally similar.

structural-convert Derive conversion traits when items are structurally similar. Inspired by serde and struct-convert crates. Features One to one fiel

Julius Lungys 3 Feb 22, 2024
Simple time handling in Rust

time Documentation: latest release main branch book Minimum Rust version policy The time crate is guaranteed to compile with any release of rustc from

Time 680 Dec 31, 2022
A simple endian-handling library for Rust

endi Yet another endian handling library for Rust. The approach is very similar to that of byteordered crate with its Endianness enum, except that end

Zeeshan Ali Khan 5 Dec 11, 2023
A Rust crate providing utility functions and macros.

介绍 此库提供四类功能:异常处理、http post收发对象、格式转换、语法糖。 在 Cargo.toml 里添加如下依赖项 [dependencies.xuanmi_base_support] git = "https://github.com/taiyi-research-institute/x

null 17 Mar 22, 2023