ResultExt

Trait ResultExt 

Source
pub trait ResultExt<T> {
    // Required methods
    fn with_path(self, path: impl AsRef<Path>) -> Result<T>;
    fn with_hint(self, hint: impl Display) -> Result<T>;
    fn context(self, msg: impl Display) -> Result<T>;
}
Expand description

Extension trait for adding context to Result types.

This trait provides convenient methods for enriching errors with additional context like file paths or helpful hints.

Required Methods§

Source

fn with_path(self, path: impl AsRef<Path>) -> Result<T>

Add a file path to the error context.

§Example
let path = Path::new("non_existent_file.txt");
std::fs::read_to_string(path)
    .with_path(path)?;
Source

fn with_hint(self, hint: impl Display) -> Result<T>

Add a helpful hint to the error context.

§Example
fn parse_config(content: &str) -> Result<()> {
    Err(CliError::Custom("parsing failed".into()))
}
let content = r#"{ "key": "value" }"#;
parse_config(&content)
    .with_hint("Check for trailing commas in JSON")?;
Source

fn context(self, msg: impl Display) -> Result<T>

Convert to a custom error message.

§Example
fn operation() -> Result<()> {
    Err(CliError::Custom("something went wrong".into()))
}
operation()
    .context("Failed to initialize bundler")?;

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, E: Into<CliError>> ResultExt<T> for Result<T, E>

Source§

fn with_path(self, path: impl AsRef<Path>) -> Result<T>

Source§

fn with_hint(self, hint: impl Display) -> Result<T>

Source§

fn context(self, msg: impl Display) -> Result<T>

Implementors§