use std::fmt;
use crate::DisplayIntoIter;
pub struct DisplaySlice<'a, T: fmt::Display> {
inner: DisplayIntoIter<'a, T, std::slice::Iter<'a, T>>,
}
impl<'a, T: fmt::Display> DisplaySlice<'a, T> {
pub fn new(slice: &'a [T]) -> Self {
Self {
inner: DisplayIntoIter::new(slice.iter()),
}
}
pub fn at_most(mut self, limit: Option<usize>) -> Self {
self.inner = self.inner.at_most(limit);
self
}
pub fn sep(mut self, separator: &'a str) -> Self {
self.inner = self.inner.sep(separator);
self
}
pub fn braces(mut self, left: &'a str, right: &'a str) -> Self {
self.inner = self.inner.braces(left, right);
self
}
pub fn ellipsis(mut self, s: &'a str) -> Self {
self.inner = self.inner.ellipsis(s);
self
}
pub fn elem(mut self, prefix: &'a str, suffix: &'a str) -> Self {
self.inner = self.inner.elem(prefix, suffix);
self
}
pub fn show_count(mut self) -> Self {
self.inner = self.inner.show_count();
self
}
pub fn limit(&self) -> usize {
self.inner.limit()
}
}
impl<T: fmt::Display> fmt::Display for DisplaySlice<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.inner.fmt(f)
}
}
pub trait DisplaySliceExt<'a, T: fmt::Display> {
fn display(&'a self) -> DisplaySlice<'a, T>;
fn display_n(&'a self, n: usize) -> DisplaySlice<'a, T> {
self.display().at_most(Some(n))
}
}
impl<T> DisplaySliceExt<'_, T> for [T]
where T: fmt::Display
{
fn display(&self) -> DisplaySlice<T> {
DisplaySlice::new(self)
}
}
#[cfg(test)]
mod tests {
use super::DisplaySlice;
use crate::DisplaySliceExt;
#[test]
fn test_display_slice() {
let a = vec![1, 2, 3, 4];
assert_eq!("[1,2,3,4]", DisplaySlice::new(&a).to_string());
let a = vec![1, 2, 3, 4, 5];
assert_eq!("[1,2,3,4,5]", DisplaySlice::new(&a).to_string());
let a = vec![1, 2, 3, 4, 5, 6];
assert_eq!("[1,2,3,4,..,6]", DisplaySlice::new(&a).to_string());
let a = [1, 2, 3, 4, 5, 6, 7];
assert_eq!("[1,2,3,4,..,7]", DisplaySlice::new(&a).to_string());
let a = [1, 2, 3, 4, 5, 6, 7];
assert_eq!(
"[1,..,7]",
DisplaySlice::new(&a).at_most(Some(2)).to_string()
);
assert_eq!("[1,..,7]", a.display().at_most(Some(2)).to_string());
assert_eq!("[1,..,7]", a.display_n(2).to_string());
assert_eq!("[..,7]", a.display_n(1).to_string());
assert_eq!("[..]", a.display_n(0).to_string());
}
#[test]
fn test_display_slice_separator() {
let a = [1, 2, 3];
assert_eq!("[1, 2, 3]", a.display().sep(", ").to_string());
let a = [1, 2, 3, 4, 5, 6];
assert_eq!("[1, 2, 3, 4, .., 6]", a.display().sep(", ").to_string());
assert_eq!("[1|..|6]", a.display_n(2).sep("|").to_string());
assert_eq!("[1 2 3 4 .. 6]", a.display().sep(" ").to_string());
assert_eq!("[1234..6]", a.display().sep("").to_string());
assert_eq!("[1..6]", a.display_n(2).sep("").to_string());
assert_eq!("[.. 6]", a.display_n(1).sep(" ").to_string());
assert_eq!("[..]", a.display_n(0).sep(" ").to_string());
}
#[test]
fn test_display_slice_ellipsis() {
let a = [1, 2, 3, 4, 5, 6];
assert_eq!("[1,2,3,4,...,6]", a.display().ellipsis("...").to_string());
assert_eq!(
"[1,2,3,4,\u{2026},6]",
a.display().ellipsis("\u{2026}").to_string()
);
assert_eq!("[1,2,3,4,,6]", a.display().ellipsis("").to_string());
assert_eq!("[...]", a.display_n(0).ellipsis("...").to_string());
assert_eq!(
"[1, 2, 3, 4, ..., 6]",
a.display().ellipsis("...").sep(", ").to_string()
);
}
#[test]
fn test_display_slice_elem() {
let a = [1, 2, 3];
assert_eq!("['1','2','3']", a.display().elem("'", "'").to_string());
let b = [1, 2, 3, 4, 5, 6];
assert_eq!(
"['1','2','3','4',..,'6']",
b.display().elem("'", "'").to_string()
);
assert_eq!("[<1>,<2>,<3>]", a.display().elem("<", ">").to_string());
assert_eq!(
"['1', '2', '3']",
a.display().elem("'", "'").sep(", ").to_string()
);
assert_eq!("[1,2,3]", a.display().elem("", "").to_string());
}
#[test]
fn test_display_slice_show_count() {
let a = [1, 2, 3, 4, 5, 6, 7];
assert_eq!(
"[1,2,3,4,..(7 total),7]",
a.display().show_count().to_string()
);
assert_eq!("[..(7 total)]", a.display_n(0).show_count().to_string());
assert_eq!("[..(7 total),7]", a.display_n(1).show_count().to_string());
let c = [1, 2, 3];
assert_eq!("[1,2,3]", c.display().show_count().to_string());
assert_eq!(
"[1,2,3,4,...(7 total),7]",
a.display().ellipsis("...").show_count().to_string()
);
assert_eq!(
"{'1', '2', '3', '4', ...(7 total), '7'}",
a.display()
.ellipsis("...")
.show_count()
.elem("'", "'")
.sep(", ")
.braces("{", "}")
.to_string()
);
}
#[test]
fn test_display_slice_braces() {
let a = [1, 2, 3];
assert_eq!("{1,2,3}", a.display().braces("{", "}").to_string());
let b = [1, 2, 3, 4, 5, 6];
assert_eq!("{1,2,3,4,..,6}", b.display().braces("{", "}").to_string());
assert_eq!(
"{1, 2, 3, 4, .., 6}",
b.display().braces("{", "}").sep(", ").to_string()
);
assert_eq!("{..}", b.display_n(0).braces("{", "}").to_string());
assert_eq!("(1,2,3)", a.display().braces("(", ")").to_string());
assert_eq!("<1,2,3>", a.display().braces("<", ">").to_string());
assert_eq!("1,2,3", a.display().braces("", "").to_string());
}
}