athena_cli/commands/
common.rs1use aws_sdk_athena::primitives::DateTime;
2use byte_unit::Byte;
3use std::time::Duration;
4
5pub trait DisplayValue {
7 fn to_display_value(&self) -> String;
8}
9
10pub trait OptionDisplayValue<T> {
12 fn to_display_value_or_default(&self) -> String;
13}
14
15impl<T: DisplayValue> OptionDisplayValue<T> for Option<T> {
17 fn to_display_value_or_default(&self) -> String {
18 self.as_ref()
19 .map(|v| v.to_display_value())
20 .unwrap_or_else(|| "-".to_string())
21 }
22}
23
24impl DisplayValue for String {
26 fn to_display_value(&self) -> String {
27 self.clone()
28 }
29}
30
31impl DisplayValue for &str {
32 fn to_display_value(&self) -> String {
33 self.to_string()
34 }
35}
36
37impl DisplayValue for i64 {
38 fn to_display_value(&self) -> String {
39 self.to_string()
40 }
41}
42
43impl DisplayValue for DateTime {
45 fn to_display_value(&self) -> String {
46 self.to_string()
47 }
48}
49
50impl DisplayValue for &DateTime {
51 fn to_display_value(&self) -> String {
52 (*self).to_string()
53 }
54}
55
56impl DisplayValue for Duration {
58 fn to_display_value(&self) -> String {
59 humantime::format_duration(*self).to_string()
60 }
61}
62
63pub trait ByteDisplay {
65 fn format_bytes(&self) -> String;
66}
67
68impl ByteDisplay for i64 {
69 fn format_bytes(&self) -> String {
70 Byte::from_i64(*self)
71 .map(|b| {
72 b.get_appropriate_unit(byte_unit::UnitType::Decimal)
73 .to_string()
74 })
75 .unwrap_or_else(|| "-".to_string())
76 }
77}
78
79pub trait OptionByteDisplay {
80 fn format_bytes_or_default(&self) -> String;
81}
82
83impl<T: Into<i64> + Copy> OptionByteDisplay for Option<T> {
84 fn format_bytes_or_default(&self) -> String {
85 self.map(|b| b.into().format_bytes())
86 .unwrap_or_else(|| "-".to_string())
87 }
88}
89
90pub trait DurationFormat {
92 fn format_duration_ms(&self) -> String;
93}
94
95impl DurationFormat for i64 {
96 fn format_duration_ms(&self) -> String {
97 Duration::from_millis(*self as u64).to_display_value()
98 }
99}
100
101pub trait OptionDurationFormat {
103 fn format_duration_ms_or_default(&self) -> String;
104}
105
106impl<T: Into<i64> + Copy> OptionDurationFormat for Option<T> {
107 fn format_duration_ms_or_default(&self) -> String {
108 self.map(|ms| ms.into().format_duration_ms())
109 .unwrap_or_else(|| "-".to_string())
110 }
111}