polars_utils/
fmt.rs
1#[macro_export]
2macro_rules! format_list_container {
3 ($e:expr, $start:tt, $end:tt) => {{
4 use std::fmt::Write;
5 let mut out = String::new();
6 out.push($start);
7 let mut iter = $e.into_iter();
8 let mut next = iter.next();
9
10 loop {
11 if let Some(val) = next {
12 write!(out, "{val}").unwrap();
13 };
14 next = iter.next();
15 if next.is_some() {
16 out.push_str(", ")
17 } else {
18 break;
19 }
20 }
21 out.push($end);
22 out
23 };};
24}
25
26#[macro_export]
27macro_rules! format_list {
28 ($e:expr) => {{
29 use polars_utils::format_list_container;
30 format_list_container!($e, '[', ']')
31 };};
32}
33
34#[macro_export]
35macro_rules! format_tuple {
36 ($e:expr) => {{
37 use polars_utils::format_list_container;
38 format_list_container!($e, '(', ')')
39 };};
40}
41
42#[macro_export]
43macro_rules! format_list_container_truncated {
44 ($e:expr, $start:tt, $end:tt, $max:expr, $quote:expr) => {{
45 use std::fmt::Write;
46 let mut out = String::new();
47 out.push($start);
48 let mut iter = $e.into_iter();
49 let mut next = iter.next();
50
51 let mut count = 0;
52 loop {
53 if $max == count {
54 write!(out, "...").unwrap();
55 break;
56 }
57 count += 1;
58
59 if let Some(val) = next {
60 write!(out, "{}{}{}", $quote, val, $quote).unwrap();
61 };
62 next = iter.next();
63 if next.is_some() {
64 out.push_str(", ")
65 } else {
66 break;
67 }
68 }
69 out.push($end);
70 out
71 };};
72}
73
74#[macro_export]
75macro_rules! format_list_truncated {
76 ($e:expr, $max:expr) => {{
77 use polars_utils::format_list_container_truncated;
78 format_list_container_truncated!($e, '[', ']', $max, "")
79 };};
80 ($e:expr, $max:expr, $quote:expr) => {{
81 use polars_utils::format_list_container_truncated;
82 format_list_container_truncated!($e, '[', ']', $max, $quote)
83 };};
84}