1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//! Add context to error messages.
use Display;
use io;
/// Trait for adding context to error messages.
///
/// This trait allows adding additional context to error messages by providing
/// a closure that returns a displayable context string.
///
/// Example of adding context to an error:
/// ```rust
/// # use std::io;
/// # use codeq::error_context_ext::ErrorContextExt;
///
/// let err = io::Error::new(io::ErrorKind::Other, "some error");
/// let err = err.context(|| "some context");
/// assert_eq!(err.to_string(), "some error; when:(some context)");
/// ```
///
/// Example of adding context to a result:
/// ```rust
/// # use std::io;
/// # use codeq::error_context_ext::ErrorContextExt;
///
/// let res = Result::<(), io::Error>::Err(io::Error::new(io::ErrorKind::Other, "some error"));
/// let res = res.context(|| "some context");
/// assert_eq!(res.unwrap_err().to_string(), "some error; when:(some context)");
/// ```