valust/error/
display.rs

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
//! Display trait for error types.

use std::fmt::{self, Write};

use sealed::sealed;

/// In-one error display trait.
///
/// This trait is `pub` so that you can stringify the error,
/// but you're not supposed to implement this trait for external types.
#[sealed(pub(crate))]
pub trait ErrorDisplay {
    /// Full description of the error.
    fn full_display(&self, f: &mut impl Write) -> fmt::Result;

    /// Brief description of the error.
    fn brief_display(&self, f: &mut impl Write) -> fmt::Result {
        self.full_display(f)
    }

    /// Human-readable display of the error.
    fn human_readable_display(&self, f: &mut impl Write) -> fmt::Result {
        self.brief_display(f)
    }

    /// Display the error fully and write it to a `String`.
    fn full_stringify(&self) -> String {
        let mut s = String::new();
        self.full_display(&mut s).unwrap();
        s
    }

    /// Display the error briefly and write it to a `String`.
    fn brief_stringify(&self) -> String {
        let mut s = String::new();
        self.brief_display(&mut s).unwrap();
        s
    }

    /// Display the error in a human-readable way and write it to a `String`.
    fn human_readable_stringify(&self) -> String {
        let mut s = String::new();
        self.human_readable_display(&mut s).unwrap();
        s
    }
}