stringlet/
fmt.rs

1//! `Display` and `Debug` for Stringlet
2
3use crate::{methods::TAIL_TAG, *};
4
5use core::fmt::{Debug, Display, Error, Formatter};
6
7impl<const SIZE: usize, const FIXED: bool, const ALIGN: u8> Display
8    for Stringlet<SIZE, FIXED, ALIGN>
9where
10    Self: Config<SIZE, ALIGN>,
11{
12    fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
13        write!(fmt, "{}", self.as_str())
14    }
15}
16
17impl<const SIZE: usize, const FIXED: bool, const ALIGN: u8> Debug for Stringlet<SIZE, FIXED, ALIGN>
18where
19    Self: Config<SIZE, ALIGN>,
20{
21    fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
22        if fmt.alternate() {
23            write!(fmt, "{} {{ ", std::any::type_name::<Self>(),)?;
24            let len = self.len();
25            write!(fmt, "SIZE: {}, len(): {len}, [u8]: {:?}, ", SIZE, o!(self))?;
26            if option_env!("STRINGLET_RAW_DEBUG").is_none() {
27                if len < SIZE {
28                    write!(fmt, "str: [{:?}", self.as_str())?;
29                    for i in len..SIZE {
30                        write!(fmt, ", 0b11_{:06b}", o!(self)[i] ^ TAIL_TAG)?;
31                    }
32                    write!(fmt, "]")?;
33                } else {
34                    write!(fmt, "str: {:?}", self.as_str())?;
35                }
36            } else if SIZE > 0 {
37                let last = self.last();
38                if last >= TAIL_TAG {
39                    write!(
40                        fmt,
41                        "last_tagged: ({}, {0:08b}; {}, {1:06b})",
42                        last,
43                        last ^ TAIL_TAG
44                    )?;
45                }
46            }
47        } else {
48            write!(fmt, "{} {{ str: {:?}", Self::type_name(), self.as_str())?;
49        }
50        write!(fmt, " }}")
51    }
52}