|
| 1 | +// Copyright 2021 Datafuse Labs |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +use std::fmt; |
| 16 | + |
| 17 | +/// Implement `Display` for `&[T]` if T is `Display`. |
| 18 | +/// |
| 19 | +/// It outputs at most `MAX` elements, excluding those from the 5th to the second-to-last one: |
| 20 | +/// - `DisplaySlice(&[1,2,3,4,5,6])` outputs: `"[1,2,3,4,...,6]"`. |
| 21 | +pub struct DisplaySlice<'a, T: fmt::Display, const MAX: usize = 5>(pub &'a [T]); |
| 22 | + |
| 23 | +impl<'a, T: fmt::Display, const MAX: usize> fmt::Display for DisplaySlice<'a, T, MAX> { |
| 24 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 25 | + let slice = self.0; |
| 26 | + let len = slice.len(); |
| 27 | + |
| 28 | + write!(f, "[")?; |
| 29 | + |
| 30 | + if len > MAX { |
| 31 | + for (i, t) in slice[..(MAX - 1)].iter().enumerate() { |
| 32 | + if i > 0 { |
| 33 | + write!(f, ",")?; |
| 34 | + } |
| 35 | + |
| 36 | + write!(f, "{}", t)?; |
| 37 | + } |
| 38 | + |
| 39 | + write!(f, ",..,")?; |
| 40 | + write!(f, "{}", slice.last().unwrap())?; |
| 41 | + } else { |
| 42 | + for (i, t) in slice.iter().enumerate() { |
| 43 | + if i > 0 { |
| 44 | + write!(f, ",")?; |
| 45 | + } |
| 46 | + |
| 47 | + write!(f, "{}", t)?; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + write!(f, "]") |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +pub trait DisplaySliceExt<'a, T: fmt::Display> { |
| 56 | + fn display(&'a self) -> DisplaySlice<'a, T>; |
| 57 | + |
| 58 | + /// Display at most `MAX` elements. |
| 59 | + fn display_n<const MAX: usize>(&'a self) -> DisplaySlice<'a, T, MAX>; |
| 60 | +} |
| 61 | + |
| 62 | +impl<T> DisplaySliceExt<'_, T> for [T] |
| 63 | +where T: fmt::Display |
| 64 | +{ |
| 65 | + fn display(&self) -> DisplaySlice<T> { |
| 66 | + DisplaySlice(self) |
| 67 | + } |
| 68 | + |
| 69 | + fn display_n<const MAX: usize>(&'_ self) -> DisplaySlice<'_, T, MAX> { |
| 70 | + DisplaySlice(self) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +#[cfg(test)] |
| 75 | +mod tests { |
| 76 | + use super::DisplaySlice; |
| 77 | + |
| 78 | + #[test] |
| 79 | + fn test_display_slice() { |
| 80 | + let a = vec![1, 2, 3, 4]; |
| 81 | + assert_eq!("[1,2,3,4]", DisplaySlice::<_>(&a).to_string()); |
| 82 | + |
| 83 | + let a = vec![1, 2, 3, 4, 5]; |
| 84 | + assert_eq!("[1,2,3,4,5]", DisplaySlice::<_>(&a).to_string()); |
| 85 | + |
| 86 | + let a = vec![1, 2, 3, 4, 5, 6]; |
| 87 | + assert_eq!("[1,2,3,4,..,6]", DisplaySlice::<_>(&a).to_string()); |
| 88 | + |
| 89 | + let a = vec![1, 2, 3, 4, 5, 6, 7]; |
| 90 | + assert_eq!("[1,2,3,4,..,7]", DisplaySlice::<_>(&a).to_string()); |
| 91 | + |
| 92 | + let a = vec![1, 2, 3, 4, 5, 6, 7]; |
| 93 | + assert_eq!("[1,..,7]", DisplaySlice::<_, 2>(&a).to_string()); |
| 94 | + } |
| 95 | +} |
0 commit comments