Skip to main content

ErrorCode

Trait ErrorCode 

Source
pub trait ErrorCode:
    Copy
    + Send
    + Sync
    + 'static {
    // Required methods
    fn to_i32(self) -> i32;
    fn from_i32(value: i32) -> Option<Self>;
    fn name(self) -> &'static str;
}
Expand description

A type whose values are error codes.

A contract declares the codes its callers may branch on as a protobuf enum next to its service. Every enum that protoc-gen-buffa generates is an ErrorCode already, through buffa::Enumeration. A Rust enum implements the three methods by hand.

use protocontract::{Error, ErrorCode};

#[derive(Clone, Copy, Debug, PartialEq)]
enum StoreCode {
    Full = 1,
}

impl ErrorCode for StoreCode {
    fn to_i32(self) -> i32 {
        self as i32
    }
    fn from_i32(value: i32) -> Option<StoreCode> {
        (value == 1).then_some(StoreCode::Full)
    }
    fn name(self) -> &'static str {
        "STORE_FULL"
    }
}

let error = Error::new(StoreCode::Full, "no room for the record");
assert_eq!(error.code_as::<StoreCode>(), Some(StoreCode::Full));

Required Methods§

Source

fn to_i32(self) -> i32

The number of this code, unique within its type.

Source

fn from_i32(value: i32) -> Option<Self>

The code of this type with the number value, or None when the type has no such code.

Source

fn name(self) -> &'static str

The name of this code, as a developer reads it in a message.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§