Truncate `s` to at most `max` bytes without splitting a UTF-8 character. Slicing a `String` at an arbitrary byte offset panics when the index lands inside a multi-byte character, so the naive `&body[..200]` this replaces could take down the CLI on any error body containing non-ASCII text — an accented name or a smart quote in a server message was enough. Walk back to a character boundary instead.
(s: &str, max: usize)
| 38 | /// accented name or a smart quote in a server message was enough. Walk back to |
| 39 | /// a character boundary instead. |
| 40 | fn preview(s: &str, max: usize) -> String { |
| 41 | if s.len() <= max { |
| 42 | return s.to_string(); |
| 43 | } |
| 44 | let mut end = max; |
| 45 | while end > 0 && !s.is_char_boundary(end) { |
| 46 | end -= 1; |
| 47 | } |
| 48 | format!("{}… ({} bytes total)", &s[..end], s.len()) |
| 49 | } |
| 50 | |
| 51 | /// HTTP client for atomic-storage management operations. |
| 52 | /// |