ext4_view/format.rs
1// Copyright 2024 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9use alloc::string::String;
10use core::fmt::{self, Display, Formatter};
11use core::str;
12
13/// Format `bytes` in a way that is suitable for `Debug` implementations.
14///
15/// This is used for string-like data (like a UNIX path) that isn't
16/// required to be ASCII, UTF-8, or any other particular encoding, but
17/// in practice it often is valid UTF-8.
18pub(crate) fn format_bytes_debug(
19 bytes: &[u8],
20 f: &mut Formatter<'_>,
21) -> fmt::Result {
22 if let Ok(s) = str::from_utf8(bytes) {
23 // For valid UTF-8, print it unmodified except for escaping
24 // special characters like newlines.
25 write!(f, "{}", s.escape_debug())
26 } else {
27 // Otherwise, print valid ASCII characters (again, with special
28 // characters like newlines escaped). Non-ASCII bytes are
29 // printed in "\xHH" format.
30 write!(f, "{}", bytes.escape_ascii())
31 }
32}
33
34/// Helper for formatting string-like data.
35///
36/// The data is lossily converted to UTF-8, with invalid UTF-8 sequences
37/// converted to '�'.
38#[must_use]
39pub struct BytesDisplay<'a>(pub(crate) &'a [u8]);
40
41impl Display for BytesDisplay<'_> {
42 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
43 write!(f, "{}", String::from_utf8_lossy(self.0))
44 }
45}
46
47#[cfg(test)]
48mod tests {
49 use super::*;
50 use core::fmt::Debug;
51
52 struct S<'a>(&'a [u8]);
53
54 impl<'a> Debug for S<'a> {
55 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
56 format_bytes_debug(self.0, f)
57 }
58 }
59
60 #[test]
61 fn test_format_bytes_debug() {
62 let f = |b: &[u8]| format!("{:?}", S(b));
63
64 // Valid UTF-8.
65 assert_eq!(f("abc😁".as_bytes()), "abc😁");
66 assert_eq!(f("abc\n".as_bytes()), r"abc\n");
67
68 // Invalid UTF-8.
69 assert_eq!(f(&[0xc3, 0x28]), r"\xc3(");
70 assert_eq!(f(&[0xc3, 0x28, b'\n']), r"\xc3(\n");
71 }
72
73 #[test]
74 fn test_bytes_display() {
75 let f = |b: &[u8]| format!("{}", BytesDisplay(b));
76
77 // Valid UTF-8.
78 assert_eq!(f("abc😁".as_bytes()), "abc😁");
79 assert_eq!(f("abc\n".as_bytes()), "abc\n");
80
81 // Invalid UTF-8.
82 assert_eq!(f(&[0xc3, 0x28]), "�(");
83 }
84}