Code

Struct Code 

Source
pub struct Code<C: ComponentId, P: PrimaryId> { /* private fields */ }
Expand description

Waddling diagnostic code: SEVERITY.COMPONENT.PRIMARY.SEQUENCE

Format: E.CRYPTO.SALT.001

This type is generic over component and primary types that implement the ComponentId and PrimaryId traits. This allows full type safety while maintaining extensibility.

§Examples

Define your own component and primary enums:

use waddling_errors::{Code, ComponentId, PrimaryId};

#[derive(Debug, Clone, Copy)]
enum Component { Crypto }
impl ComponentId for Component {
    fn as_str(&self) -> &'static str { "CRYPTO" }
}

#[derive(Debug, Clone, Copy)]
enum Primary { Salt }
impl PrimaryId for Primary {
    fn as_str(&self) -> &'static str { "SALT" }
}

const ERR: Code<Component, Primary> = Code::error(Component::Crypto, Primary::Salt, 1);
assert_eq!(ERR.code(), "E.CRYPTO.SALT.001");

Implementations§

Source§

impl<C: ComponentId, P: PrimaryId> Code<C, P>

Source

pub const fn new( severity: Severity, component: C, primary: P, sequence: u16, ) -> Self

Create a new code with explicit severity

§Panics

Panics if sequence > 999

Source

pub const fn error(component: C, primary: P, sequence: u16) -> Self

Create an error code (E)

Source

pub const fn warning(component: C, primary: P, sequence: u16) -> Self

Create a warning code (W)

Source

pub const fn critical(component: C, primary: P, sequence: u16) -> Self

Create a critical code (C)

Source

pub const fn blocked(component: C, primary: P, sequence: u16) -> Self

Create a blocked code (B)

Source

pub const fn help(component: C, primary: P, sequence: u16) -> Self

Create a help code (H)

Source

pub const fn success(component: C, primary: P, sequence: u16) -> Self

Create a success code (S)

Source

pub const fn completed(component: C, primary: P, sequence: u16) -> Self

Create a completed code (K)

Source

pub const fn info(component: C, primary: P, sequence: u16) -> Self

Create an info code (I)

Source

pub const fn trace(component: C, primary: P, sequence: u16) -> Self

Create a trace code (T)

Source

pub fn code(&self) -> String

Get the full error code (e.g., “E.CRYPTO.SALT.001”)

Source

pub fn write_code(&self, f: &mut impl Write) -> Result

Write error code to formatter without allocating

Use in performance-critical paths to avoid String allocation.

Source

pub const fn severity(&self) -> Severity

Get severity

Source

pub const fn component(&self) -> C

Get component

Source

pub fn component_str(&self) -> &'static str

Get component as string

Source

pub const fn primary(&self) -> P

Get primary category

Source

pub fn primary_str(&self) -> &'static str

Get primary as string

Source

pub const fn sequence(&self) -> u16

Get sequence number

Trait Implementations§

Source§

impl<C: Clone + ComponentId, P: Clone + PrimaryId> Clone for Code<C, P>

Source§

fn clone(&self) -> Code<C, P>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<C: Debug + ComponentId, P: Debug + PrimaryId> Debug for Code<C, P>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<C: ComponentId, P: PrimaryId> Display for Code<C, P>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<C: Hash + ComponentId, P: Hash + PrimaryId> Hash for Code<C, P>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<C: PartialEq + ComponentId, P: PartialEq + PrimaryId> PartialEq for Code<C, P>

Source§

fn eq(&self, other: &Code<C, P>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<C: Copy + ComponentId, P: Copy + PrimaryId> Copy for Code<C, P>

Source§

impl<C: Eq + ComponentId, P: Eq + PrimaryId> Eq for Code<C, P>

Source§

impl<C: ComponentId, P: PrimaryId> StructuralPartialEq for Code<C, P>

Auto Trait Implementations§

§

impl<C, P> Freeze for Code<C, P>
where C: Freeze, P: Freeze,

§

impl<C, P> RefUnwindSafe for Code<C, P>

§

impl<C, P> Send for Code<C, P>
where C: Send, P: Send,

§

impl<C, P> Sync for Code<C, P>
where C: Sync, P: Sync,

§

impl<C, P> Unpin for Code<C, P>
where C: Unpin, P: Unpin,

§

impl<C, P> UnwindSafe for Code<C, P>
where C: UnwindSafe, P: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.