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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
use crate::{Unit, Width}; use core::fmt::{Display, Error, Formatter}; use fmt_iter::repeat; /// Pad a value knowing the number of blocks. /// /// Values that implement this trait are to be passed /// to `pad` field of [`PaddedValue`](crate::PaddedValue) /// or [`PaddedColumn`](crate::PaddedColumn). pub trait Pad<Value: Width, PadBlock: Display> { /// Pad a value knowing the number of blocks. fn fmt( &self, formatter: &mut Formatter<'_>, value: &Value, pad_block: &PadBlock, pad_width: usize, ) -> Result<(), Error>; } impl<Value, PadBlock, X> Pad<Value, PadBlock> for &X where Value: Width, PadBlock: Display, X: Pad<Value, PadBlock> + Sized, { fn fmt( &self, formatter: &mut Formatter<'_>, value: &Value, pad_block: &PadBlock, pad_width: usize, ) -> Result<(), Error> { X::fmt(*self, formatter, value, pad_block, pad_width) } } /// All pre-defined zero-sized [`Pad`] types in this [crate] implement this trait. pub trait UnitPad<Value: Width, PadBlock: Display>: Unit + Pad<Value, PadBlock> {} macro_rules! unit_pad { ($name:ident) => { impl Unit for $name { const VALUE: Self = $name; } impl<Value: Width, PadBlock: Display> UnitPad<Value, PadBlock> for $name {} }; } /// Pad to the right, content to the left. /// /// **Example:** /// /// ``` /// # use pretty_assertions::assert_eq; /// use zero_copy_pads::{AlignLeft, PaddedValue, PanicOnExcess}; /// let padded_value = PaddedValue { /// pad: AlignLeft, /// value: "abcdef", /// pad_block: '-', /// total_width: 9, /// handle_excess: PanicOnExcess, /// }; /// assert_eq!(padded_value.to_string(), "abcdef---"); /// ``` #[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] pub struct AlignLeft; unit_pad!(AlignLeft); impl<Value: Width, PadBlock: Display> Pad<Value, PadBlock> for AlignLeft { fn fmt( &self, formatter: &mut Formatter<'_>, value: &Value, pad_block: &PadBlock, pad_width: usize, ) -> Result<(), Error> { let pad = repeat(pad_block, pad_width); write!(formatter, "{}{}", value, pad) } } /// Pad to the left, content to the right. /// /// **Example:** /// /// ``` /// # use pretty_assertions::assert_eq; /// use zero_copy_pads::{AlignRight, PaddedValue, PanicOnExcess}; /// let padded_value = PaddedValue { /// pad: AlignRight, /// value: "abcdef", /// pad_block: '-', /// total_width: 9, /// handle_excess: PanicOnExcess, /// }; /// assert_eq!(padded_value.to_string(), "---abcdef"); /// ``` #[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] pub struct AlignRight; unit_pad!(AlignRight); impl<Value: Width, PadBlock: Display> Pad<Value, PadBlock> for AlignRight { fn fmt( &self, formatter: &mut Formatter<'_>, value: &Value, pad_block: &PadBlock, pad_width: usize, ) -> Result<(), Error> { let pad = repeat(pad_block, pad_width); write!(formatter, "{}{}", pad, value) } } /// Pad to both sides, place content in the middle, but shift to the left one /// block if it can't be exactly central. /// /// **Example:** /// /// ``` /// # #[cfg(feature = "std")] fn main() { /// # use pretty_assertions::assert_eq; /// use zero_copy_pads::{AlignCenterLeft, PaddedColumn, PanicOnExcess}; /// let values = [ /// "Rust", "C", "C++", "C#", "JavaScript", /// "TypeScript", "Java", "Kotlin", "Go", /// ]; /// let padded_column = PaddedColumn { /// pad: AlignCenterLeft, /// values: values.iter(), /// pad_block: '-', /// }; /// let padded_values: Vec<_> = padded_column /// .into_iter() /// .map(|x| x.to_string()) /// .collect(); /// let expected = [ /// "---Rust---", "----C-----", "---C++----", /// "----C#----", "JavaScript", "TypeScript", /// "---Java---", "--Kotlin--", "----Go----", /// ]; /// assert_eq!(padded_values, expected); /// # } /// # #[cfg(not(feature = "std"))] fn main() {} /// ``` #[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] pub struct AlignCenterLeft; unit_pad!(AlignCenterLeft); impl<Value: Width, PadBlock: Display> Pad<Value, PadBlock> for AlignCenterLeft { fn fmt( &self, formatter: &mut Formatter<'_>, value: &Value, pad_block: &PadBlock, pad_width: usize, ) -> Result<(), Error> { let pad = repeat(pad_block, pad_width >> 1); let remainder = repeat(pad_block, pad_width & 1); write!(formatter, "{}{}{}{}", pad, value, pad, remainder) } } /// Pad to both sides, place content in the middle, but shift to the right one /// block if it can't be exactly central. /// /// **Example:** /// /// ``` /// # #[cfg(feature = "std")] fn main() { /// # use pretty_assertions::assert_eq; /// use zero_copy_pads::{AlignCenterRight, PaddedColumn, PanicOnExcess}; /// let values = [ /// "Rust", "C", "C++", "C#", "JavaScript", /// "TypeScript", "Java", "Kotlin", "Go", /// ]; /// let padded_column = PaddedColumn { /// pad: AlignCenterRight, /// values: values.iter(), /// pad_block: '-', /// }; /// let padded_values: Vec<_> = padded_column /// .into_iter() /// .map(|x| x.to_string()) /// .collect(); /// let expected = [ /// "---Rust---", "-----C----", "----C++---", /// "----C#----", "JavaScript", "TypeScript", /// "---Java---", "--Kotlin--", "----Go----", /// ]; /// assert_eq!(padded_values, expected); /// # } /// # #[cfg(not(feature = "std"))] fn main() {} /// ``` #[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] pub struct AlignCenterRight; unit_pad!(AlignCenterRight); impl<Value: Width, PadBlock: Display> Pad<Value, PadBlock> for AlignCenterRight { fn fmt( &self, formatter: &mut Formatter<'_>, value: &Value, pad_block: &PadBlock, pad_width: usize, ) -> Result<(), Error> { let pad = repeat(pad_block, pad_width >> 1); let remainder = repeat(pad_block, pad_width & 1); write!(formatter, "{}{}{}{}", pad, remainder, value, pad) } }