| 1 |
//! Methods for displaying [`Inspectable`]s. |
| 2 |
//! |
| 3 |
//! Refer to the [documentation here][crate::inspect#displaying--outputting]. |
| 4 |
|
| 5 |
use core::fmt::{Display, Write}; |
| 6 |
|
| 7 |
use crate::inspect::*; |
| 8 |
|
| 9 |
impl<'a> Display for Inspectable<'a> { |
| 10 |
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 11 |
match self { |
| 12 |
Inspectable::String(x) => Display::fmt(x, f), |
| 13 |
Inspectable::Dict(x) => Display::fmt(x, f), |
| 14 |
Inspectable::List(x) => Display::fmt(x, f), |
| 15 |
Inspectable::Int(x) => Display::fmt(x, f), |
| 16 |
Inspectable::Raw(x) => { |
| 17 |
for b in x.iter() { |
| 18 |
write!(f, "\\x{b:02X}")?; |
| 19 |
} |
| 20 |
Ok(()) |
| 21 |
}, |
| 22 |
} |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
impl<'a> Display for InString<'a> { |
| 27 |
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 28 |
let bytes = self.content(); |
| 29 |
let all_printable = bytes.iter().all(|b| is_printable_byte(*b)); |
| 30 |
write!(f, "{}:", self.len())?; |
| 31 |
if all_printable { |
| 32 |
for &b in bytes.iter() { |
| 33 |
let c = char::from_u32(b as u32).expect("Already ensured all chars are printable"); |
| 34 |
f.write_char(c)?; |
| 35 |
} |
| 36 |
Ok(()) |
| 37 |
} else { |
| 38 |
for b in bytes.iter() { |
| 39 |
write!(f, "\\x{b:02X}")?; |
| 40 |
} |
| 41 |
Ok(()) |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
impl<'a> Display for InInt<'a> { |
| 47 |
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 48 |
f.write_char('i')?; |
| 49 |
f.write_str(&self.bytes)?; |
| 50 |
f.write_char('e') |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
impl<'a> Display for InDict<'a> { |
| 55 |
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 56 |
f.write_char('d')?; |
| 57 |
for InDictEntry { key, value } in self.items.iter() { |
| 58 |
Display::fmt(key, f)?; |
| 59 |
Display::fmt(value, f)?; |
| 60 |
} |
| 61 |
f.write_char('e') |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
impl<'a> Display for InList<'a> { |
| 66 |
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 67 |
f.write_char('l')?; |
| 68 |
for inspectable in self.items.iter() { |
| 69 |
Display::fmt(inspectable, f)?; |
| 70 |
} |
| 71 |
f.write_char('e') |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
impl<'a> Inspectable<'a> { |
| 76 |
/// Provides a [`String`] of pretty-printed bencode in a format |
| 77 |
/// suitable for copy-pasting as a Rust string literal. |
| 78 |
/// |
| 79 |
/// # Example |
| 80 |
/// |
| 81 |
/// ``` |
| 82 |
/// # use bendy::inspect::*; |
| 83 |
/// let i = inspect(b"li0ei1ee"); |
| 84 |
/// let lit = i.as_rust_string_literal(); |
| 85 |
/// println!("{lit}"); |
| 86 |
/// // Prints: |
| 87 |
/// // let pretty_bencode = b"\ |
| 88 |
/// // l\ |
| 89 |
/// // i0e\ |
| 90 |
/// // i1e\ |
| 91 |
/// // e\ |
| 92 |
/// // " |
| 93 |
/// ``` |
| 94 |
#[must_use] |
| 95 |
pub fn as_rust_string_literal(&self) -> String { |
| 96 |
self.internal_pretty_print(true) |
| 97 |
} |
| 98 |
|
| 99 |
/// Provides a [`String`] of pretty-printed bencode. |
| 100 |
/// |
| 101 |
/// # Example |
| 102 |
/// |
| 103 |
/// ``` |
| 104 |
/// # use bendy::inspect::*; |
| 105 |
/// let i = inspect(b"li0ei1ee"); |
| 106 |
/// let lit = i.as_pretty_printed(); |
| 107 |
/// println!("{lit}"); |
| 108 |
/// // Prints: |
| 109 |
/// // l |
| 110 |
/// // i0e |
| 111 |
/// // i1e |
| 112 |
/// // e |
| 113 |
/// ``` |
| 114 |
#[must_use] |
| 115 |
pub fn as_pretty_printed(&self) -> String { |
| 116 |
self.internal_pretty_print(false) |
| 117 |
} |
| 118 |
|
| 119 |
fn internal_pretty_print(&self, as_literal: bool) -> String { |
| 120 |
fn dispatch( |
| 121 |
inspectable: &Inspectable, |
| 122 |
indent: &mut usize, |
| 123 |
as_literal: bool, |
| 124 |
out: &mut String, |
| 125 |
) { |
| 126 |
let newline = |indent: &usize, out: &mut String| { |
| 127 |
if as_literal { |
| 128 |
out.push('\\'); |
| 129 |
} |
| 130 |
out.push('\n'); |
| 131 |
for _ in 0..*indent { |
| 132 |
out.push('\t'); |
| 133 |
} |
| 134 |
}; |
| 135 |
|
| 136 |
match inspectable { |
| 137 |
x @ Inspectable::Raw(_) | x @ Inspectable::String(_) | x @ Inspectable::Int(_) => { |
| 138 |
out.push_str(x.to_string().as_str()); |
| 139 |
}, |
| 140 |
Inspectable::List(InList { items }) => { |
| 141 |
out.push('l'); |
| 142 |
*indent += 1; |
| 143 |
for i in items.iter() { |
| 144 |
newline(indent, out); |
| 145 |
dispatch(i, indent, as_literal, out); |
| 146 |
} |
| 147 |
*indent -= 1; |
| 148 |
newline(indent, out); |
| 149 |
out.push('e'); |
| 150 |
}, |
| 151 |
Inspectable::Dict(InDict { items }) => { |
| 152 |
out.push('d'); |
| 153 |
*indent += 1; |
| 154 |
for i in items.iter() { |
| 155 |
let InDictEntry { key, value } = i; |
| 156 |
newline(indent, out); |
| 157 |
out.push_str(key.to_string().as_str()); |
| 158 |
newline(indent, out); |
| 159 |
dispatch(value, indent, as_literal, out); |
| 160 |
} |
| 161 |
*indent -= 1; |
| 162 |
newline(indent, out); |
| 163 |
out.push('e'); |
| 164 |
}, |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
let mut output = String::new(); |
| 169 |
let mut indent = 0; |
| 170 |
|
| 171 |
if as_literal { |
| 172 |
output.push_str("let pretty_bencode = b\"\\\n"); |
| 173 |
} |
| 174 |
dispatch(self, &mut indent, as_literal, &mut output); |
| 175 |
if as_literal { |
| 176 |
output.push_str("\\\n\""); |
| 177 |
} |
| 178 |
output |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
#[allow(clippy::manual_range_contains)] |
| 183 |
fn is_printable_byte(b: u8) -> bool { |
| 184 |
40 <= b && b < 127 |
| 185 |
} |
| 186 |
|
| 187 |
#[cfg(test)] |
| 188 |
mod tests { |
| 189 |
use super::*; |
| 190 |
|
| 191 |
#[test] |
| 192 |
fn valid_printing_works() { |
| 193 |
fn ensure_prints_as(b: impl AsRef<[u8]>, expected: Option<&str>) { |
| 194 |
let b = b.as_ref(); |
| 195 |
let i = inspect(b).to_string(); |
| 196 |
if let Some(e) = expected { |
| 197 |
assert_eq!(i.as_str(), e); |
| 198 |
} else { |
| 199 |
let e = String::from_utf8(b.to_vec()).unwrap(); |
| 200 |
assert_eq!(i, e); |
| 201 |
} |
| 202 |
} |
| 203 |
fn ensure_roundtrip(b: impl AsRef<[u8]>) { |
| 204 |
ensure_prints_as(b, None); |
| 205 |
} |
| 206 |
ensure_roundtrip(b"i200e"); |
| 207 |
ensure_roundtrip(b"4:helo"); |
| 208 |
ensure_roundtrip(b"de"); |
| 209 |
ensure_roundtrip(b"le"); |
| 210 |
ensure_roundtrip(b"li2e6:inliste"); |
| 211 |
ensure_roundtrip(b"d1:ai1e1:bi2ee"); |
| 212 |
ensure_prints_as( |
| 213 |
b"d5:countli1ei2ei3ee4:null1:\x00e", |
| 214 |
Some("d5:countli1ei2ei3ee4:null1:\\x00e"), |
| 215 |
); |
| 216 |
} |
| 217 |
|
| 218 |
#[ignore] |
| 219 |
#[test] |
| 220 |
fn as_rust_string_literal_test() { |
| 221 |
fn print(b: impl AsRef<[u8]>) { |
| 222 |
let b = b.as_ref(); |
| 223 |
let i = inspect(b).as_rust_string_literal(); |
| 224 |
println!("-------\n{}", i); |
| 225 |
} |
| 226 |
print(b"i200e"); |
| 227 |
print(b"4:helo"); |
| 228 |
print(b"de"); |
| 229 |
print(b"le"); |
| 230 |
print(b"li2e6:inliste"); |
| 231 |
print(b"d1:ai1e1:bi2ee"); |
| 232 |
print(b"d5:countli1ei2ei3ee4:null1:\x00e"); |
| 233 |
let ap = all_printables_bencode_list(); |
| 234 |
print(&ap); |
| 235 |
|
| 236 |
panic!() |
| 237 |
} |
| 238 |
|
| 239 |
#[ignore] |
| 240 |
#[test] |
| 241 |
fn debug_print_test() { |
| 242 |
let i = inspect(b"i200e"); |
| 243 |
dbg!(i); |
| 244 |
let i = inspect(b"4:helo"); |
| 245 |
dbg!(i); |
| 246 |
let i = inspect(b"de"); |
| 247 |
dbg!(i); |
| 248 |
let i = inspect(b"le"); |
| 249 |
dbg!(i); |
| 250 |
let i = inspect(b"li2e4:liste"); |
| 251 |
dbg!(i); |
| 252 |
let i = inspect(b"d1:ai1e1:bi2ee"); |
| 253 |
dbg!(i); |
| 254 |
panic!() |
| 255 |
} |
| 256 |
|
| 257 |
#[test] |
| 258 |
fn as_rust_string_literal_doesnt_break_quoting() { |
| 259 |
// We must not considuer double quote symbols printable, since |
| 260 |
// we don't escape them or turn them into \x22 in as_rust_string_literal |
| 261 |
assert!(!is_printable_byte(b'"')); |
| 262 |
} |
| 263 |
|
| 264 |
fn all_printables_bencode_list() -> Vec<u8> { |
| 265 |
let mut ap = Vec::new(); |
| 266 |
ap.push(b'l'); |
| 267 |
for b in 40_u8..127 { |
| 268 |
// Just checking that the printable byte ranges are synced |
| 269 |
assert!(is_printable_byte(b)); |
| 270 |
ap.push(b'1'); |
| 271 |
ap.push(b':'); |
| 272 |
ap.push(b); |
| 273 |
} |
| 274 |
ap.push(b'e'); |
| 275 |
ap |
| 276 |
} |
| 277 |
} |