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§
Sourcefn with_path(self, path: impl AsRef<Path>) -> Result<T>
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)?;Sourcefn with_hint(self, hint: impl Display) -> Result<T>
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")?;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.