1use core::error::Error;
2use core::fmt;
3
4#[derive(Clone, Copy)]
6pub struct Chain<'a>(pub &'a dyn Error);
7
8#[derive(Clone, Copy, Debug)]
10pub struct Hex<'a>(pub &'a [u8]);
11
12#[derive(Clone, Copy, Debug)]
14pub struct OrNone<T>(pub Option<T>);
15
16#[derive(Clone, Copy, Debug)]
18pub struct Outcome<'a, E>(pub &'a Result<(), E>);
19
20impl fmt::Debug for Chain<'_> {
21 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22 fmt::Display::fmt(self, f)
23 }
24}
25
26impl fmt::Display for Chain<'_> {
27 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28 write!(f, "{}", self.0)?;
29 let mut source = self.0.source();
30 while let Some(cause) = source {
31 write!(f, ": {cause}")?;
32 source = cause.source();
33 }
34 Ok(())
35 }
36}
37
38impl fmt::Display for Hex<'_> {
39 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40 self.0.iter().try_for_each(|byte| write!(f, "{byte:02x}"))
41 }
42}
43
44impl<T: fmt::Display> fmt::Display for OrNone<T> {
45 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46 match &self.0 {
47 Some(value) => value.fmt(f),
48 None => f.write_str("none"),
49 }
50 }
51}
52
53impl<E: Error> fmt::Display for Outcome<'_, E> {
54 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55 match self.0 {
56 Ok(()) => f.write_str("ok"),
57 Err(err) => Chain(err).fmt(f),
58 }
59 }
60}