AttachExt

Trait AttachExt 

Source
pub trait AttachExt {
    // Required methods
    fn attach_kv<K, V>(self, key: K, value: V) -> Self
       where K: Display,
             V: Display;
    fn attach_kv_dbg<K, V>(self, key: K, value: V) -> Self
       where K: Display,
             V: Debug;
    fn attach_field_status<S>(self, name: &'static str, status: S) -> Self
       where S: Display;
    fn attach_dbg<A>(self, value: A) -> Self
       where A: Debug;

    // Provided methods
    fn attach_ty_val<A>(self, value: A) -> Self
       where Self: Sized,
             A: Display { ... }
    fn attach_path<P: AsRef<Path>>(self, path: P) -> Self
       where Self: Sized { ... }
}
Expand description

Extension trait that adds attachment methods to error reports and results.

This trait provides convenient methods for attaching various types of context information to error reports.

Required Methods§

Source

fn attach_kv<K, V>(self, key: K, value: V) -> Self
where K: Display, V: Display,

Attach a key-value pair to the error.

This method adds contextual information in the form of a key-value pair to the error report using the KeyValue type. Both the key and value must implement Display and will be formatted using their Display implementations in error messages.

§Example
use bigerror::{AttachExt, ThinContext, Report};

#[derive(ThinContext)]
struct MyError;

fn process_user(user_id: u64) -> Result<String, Report<MyError>> {
    // Simulate an error with user context
    Err(MyError::attach("processing failed"))
        .attach_kv("user_id", user_id)
        .attach_kv("operation", "data_processing")
}
Source

fn attach_kv_dbg<K, V>(self, key: K, value: V) -> Self
where K: Display, V: Debug,

Attach a key-value pair where the value is debug-formatted.

This method is similar to attach_kv but uses the Debug implementation of the value instead of Display. This is useful for types that don’t implement Display or when you want the debug representation for diagnostic purposes.

§Example
use bigerror::{AttachExt, ThinContext, Report};

#[derive(ThinContext)]
struct ValidationError;

#[derive(Debug)]
struct Config {
    debug_mode: bool,
    max_connections: usize,
}

fn validate_config(config: Config) -> Result<(), Report<ValidationError>> {
    if config.max_connections == 0 {
        return Err(ValidationError::attach("invalid max_connections"))
            .attach_kv_dbg("config", config); // Uses Debug formatting
    }
    Ok(())
}
Source

fn attach_field_status<S>(self, name: &'static str, status: S) -> Self
where S: Display,

Attach a field with its status.

This method attaches information about a specific field or property and its status. It’s particularly useful for validation errors, data processing failures, or when indicating the state of specific fields in data structures.

§Example
use bigerror::{AttachExt, ThinContext, Report, attachment::Missing};

#[derive(ThinContext)]
struct ValidationError;

fn validate_user_data(email: Option<&str>, age: Option<u32>) -> Result<(), Report<ValidationError>> {
    let mut error = None;

    if email.is_none() {
        error = Some(ValidationError::attach("validation failed")
            .attach_field_status("email", Missing));
    }
    Ok(())
}
Source

fn attach_dbg<A>(self, value: A) -> Self
where A: Debug,

Attach a debug-formatted value.

This method attaches a value to the error using its Debug implementation for formatting. This is useful for types that don’t implement Display or when you want the detailed debug representation rather than the user-friendly display.

§Example
use bigerror::{AttachExt, ThinContext, Report, DecodeError};

fn decode_data(data: &mut Vec<u8>) -> Result<&str, Report<DecodeError>> {
    if data.is_empty() {
        return Err(DecodeError::attach("no data found"))
            .attach_dbg(data.clone());
    }

    // ...

    Ok("data processed")
}

Provided Methods§

Source

fn attach_ty_val<A>(self, value: A) -> Self
where Self: Sized, A: Display,

Attach a type-value pair where the type name is the key.

This is a convenience method that creates a key-value attachment where the key is the type name (using the ty! macro) and the value is the provided value. This is useful for adding context about what type of value was involved in an error.

§Example
use bigerror::{AttachExt, ThinContext, Report};

#[derive(ThinContext)]
struct ProcessingError;

fn process_count(count: usize) -> Result<String, Report<ProcessingError>> {
    if count == 0 {
        return Err(ProcessingError::attach("invalid count"))
            .attach_ty_val(count); // Attaches "<usize>: 0"
    }
    Ok(format!("Processing {} items", count))
}
Source

fn attach_path<P: AsRef<Path>>(self, path: P) -> Self
where Self: Sized,

Attach a file system path to the error.

This method attaches a file system path as a key-value pair where the key is “path” and the value is the string representation of the path. This is commonly used for file I/O operations to provide context about which file caused the error.

Note: The path is converted to a string immediately when this method is called (not lazily), so it’s recommended to use this in .map_err() chains to avoid unnecessary string conversions when no error occurs.

§Example
use bigerror::{AttachExt, ThinContext, Report, ResultIntoContext, FsError};
use std::path::Path;

fn read_config_file<P: AsRef<Path>>(path: P) -> Result<String, Report<FsError>> {
    std::fs::read_to_string(&path)
        .into_ctx()
        .attach_path(path) // Attaches "path: /etc/config.toml"
}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<T, C> AttachExt for Result<T, Report<C>>

Source§

fn attach_kv<K, V>(self, key: K, value: V) -> Self
where K: Display, V: Display,

Source§

fn attach_kv_dbg<K, V>(self, key: K, value: V) -> Self
where K: Display, V: Debug,

Source§

fn attach_field_status<S>(self, name: &'static str, status: S) -> Self
where S: Display,

Source§

fn attach_dbg<A>(self, value: A) -> Self
where A: Debug,

Implementors§

Source§

impl<C> AttachExt for Report<C>