Safely truncates
(unit: &str)
| 255 | |
| 256 | /// Safely truncates |
| 257 | fn to_string_truncate(unit: &str) -> String { |
| 258 | const MAX_UNIT_LEN: usize = 3; |
| 259 | |
| 260 | if unit.len() > MAX_UNIT_LEN { |
| 261 | // TODO(MSRV 1.91): use ceil_char_boundary |
| 262 | |
| 263 | if unit.is_char_boundary(3) { |
| 264 | format!("{}...", &unit[..3]) |
| 265 | } else if unit.is_char_boundary(4) { |
| 266 | format!("{}...", &unit[..4]) |
| 267 | } else if unit.is_char_boundary(5) { |
| 268 | format!("{}...", &unit[..5]) |
| 269 | } else if unit.is_char_boundary(6) { |
| 270 | format!("{}...", &unit[..6]) |
| 271 | } else { |
| 272 | unreachable!("char boundary will be within 4 bytes") |
| 273 | } |
| 274 | } else { |
| 275 | unit.to_owned() |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | /// Error returned when parsing a [`Unit`] fails. |
| 280 | #[derive(Debug)] |