dangerous/display/
mod.rs

1//! Display support.
2
3mod error;
4mod input;
5mod section;
6mod unit;
7
8use core::fmt::{Formatter, Result};
9
10pub use self::error::ErrorDisplay;
11pub use self::input::{InputDisplay, PreferredFormat};
12
13/// Library specific display trait that accepts a [`Write`] without requiring a
14/// formatter.
15pub trait DisplayBase {
16    /// Formats `self` given the provided [`Write`].
17    ///
18    /// # Errors
19    ///
20    /// Returns a [`core::fmt::Error`] if failed to write.
21    fn fmt(&self, w: &mut dyn Write) -> Result;
22}
23
24impl<T> DisplayBase for &T
25where
26    T: DisplayBase,
27{
28    fn fmt(&self, w: &mut dyn Write) -> Result {
29        (**self).fmt(w)
30    }
31}
32
33impl DisplayBase for &'static str {
34    fn fmt(&self, w: &mut dyn Write) -> Result {
35        w.write_str(self)
36    }
37}
38
39/// Library specific [`Write`] trait for formatting.
40pub trait Write {
41    /// Writes a string slice into this writer, returning whether the write
42    /// succeeded.
43    ///
44    /// # Errors
45    ///
46    /// Returns a [`core::fmt::Error`] if failed to write.
47    fn write_str(&mut self, s: &str) -> Result;
48
49    /// Writes a char into this writer, returning whether the write succeeded.
50    ///
51    /// # Errors
52    ///
53    /// Returns a [`core::fmt::Error`] if failed to write.
54    fn write_char(&mut self, c: char) -> Result;
55
56    /// Writes a usize into this writer, returning whether the write succeeded.
57    ///
58    /// # Errors
59    ///
60    /// Returns a [`core::fmt::Error`] if failed to write.
61    fn write_usize(&mut self, v: usize) -> Result;
62
63    /// Writes a byte as hex into this writer, returning whether the write
64    /// succeeded.
65    ///
66    /// The byte as hex must be always two characters long (zero-padded).
67    ///
68    /// # Errors
69    ///
70    /// Returns a [`core::fmt::Error`] if failed to write.
71    fn write_hex(&mut self, b: u8) -> Result {
72        fn digit(b: u8) -> char {
73            if b > 9 {
74                (b'a' + (b - 10)) as char
75            } else {
76                (b'0' + b) as char
77            }
78        }
79        self.write_char(digit(b >> 4))?;
80        self.write_char(digit(b & 0x0F))
81    }
82}
83
84impl<T> Write for &mut T
85where
86    T: Write,
87{
88    fn write_str(&mut self, s: &str) -> Result {
89        (**self).write_str(s)
90    }
91
92    fn write_char(&mut self, c: char) -> Result {
93        (**self).write_char(c)
94    }
95
96    fn write_usize(&mut self, v: usize) -> Result {
97        (**self).write_usize(v)
98    }
99
100    fn write_hex(&mut self, b: u8) -> Result {
101        (**self).write_hex(b)
102    }
103}
104
105impl<'a> Write for Formatter<'a> {
106    fn write_str(&mut self, s: &str) -> Result {
107        core::fmt::Write::write_str(self, s)
108    }
109
110    fn write_char(&mut self, c: char) -> Result {
111        core::fmt::Write::write_char(self, c)
112    }
113
114    fn write_usize(&mut self, v: usize) -> Result {
115        core::fmt::Display::fmt(&v, self)
116    }
117}
118
119///////////////////////////////////////////////////////////////////////////////
120
121pub(crate) fn byte_count(w: &mut dyn Write, count: usize) -> Result {
122    match count {
123        0 => w.write_str("no bytes"),
124        1 => w.write_str("1 byte"),
125        n => {
126            w.write_usize(n)?;
127            w.write_str(" bytes")
128        }
129    }
130}