1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use std::fmt;

use serde::Serialize;
use serde_json as json;

mod account;
mod dynamo;
mod ebs;
mod ec2;
mod eks;
mod iam;
mod pricing;
mod s3;
mod smithy;
mod sso;
mod sts;

pub trait Show: fmt::Debug {
    fn _fmt(&self) -> Box<dyn fmt::Display + '_>;

    fn text(&self) -> String {
        self._fmt().to_string()
    }

    fn debug(&self) -> String {
        fmtools::format!({self:?})
    }

    fn json(&self) -> String {
        "JSON view is not implemented yet".to_string()
    }

    fn table(&self) -> String {
        "Table view is not implemented yet".to_string()
    }

    fn yaml(&self) -> String {
        "YAML view is not implemented yet".to_string()
    }

    fn yaml_stream(&self) -> String {
        "YAML stream view is not implemented yet".to_string()
    }
}

impl<T: Show> Show for &T {
    fn _fmt(&self) -> Box<dyn fmt::Display + '_> {
        (*self)._fmt()
    }

    fn debug(&self) -> String {
        (*self).debug()
    }

    fn json(&self) -> String {
        (*self).json()
    }

    fn table(&self) -> String {
        (*self).table()
    }

    fn yaml(&self) -> String {
        (*self).yaml()
    }

    fn yaml_stream(&self) -> String {
        (*self).yaml_stream()
    }
}

impl<T: Show> Show for Vec<T> {
    fn _fmt(&self) -> Box<dyn fmt::Display + '_> {
        Box::new(fmtools::join("\n", self.iter().map(|item| item._fmt())))
    }

    fn debug(&self) -> String {
        self.as_slice().debug()
    }
}

impl<T: Show> Show for &[T] {
    fn _fmt(&self) -> Box<dyn fmt::Display + '_> {
        Box::new(fmtools::join("\n", self.iter().map(|item| item._fmt())))
    }

    fn debug(&self) -> String {
        let items = self.iter().map(|item| item.debug());
        fmtools::join("\n", items).to_string()
    }
}

impl<T: Show> Show for Option<T> {
    fn _fmt(&self) -> Box<dyn fmt::Display + '_> {
        self.as_ref().map_or_else(|| ()._fmt(), |item| item._fmt())
    }

    fn debug(&self) -> String {
        self.as_ref().map(|item| item.debug()).unwrap_or_default()
    }
}

impl Show for () {
    fn _fmt(&self) -> Box<dyn fmt::Display + '_> {
        Box::new("")
    }

    fn debug(&self) -> String {
        String::new()
    }
}

impl Show for String {
    fn _fmt(&self) -> Box<dyn fmt::Display + '_> {
        Box::new(self)
    }
}

impl Show for &str {
    fn _fmt(&self) -> Box<dyn fmt::Display + '_> {
        Box::new(self)
    }
}

impl Show for str {
    fn _fmt(&self) -> Box<dyn fmt::Display + '_> {
        Box::new(self)
    }
}

impl Show for bool {
    fn _fmt(&self) -> Box<dyn fmt::Display + '_> {
        Box::new(self)
    }
}

impl Show for i32 {
    fn _fmt(&self) -> Box<dyn fmt::Display + '_> {
        Box::new(self)
    }
}

impl Show for i64 {
    fn _fmt(&self) -> Box<dyn fmt::Display + '_> {
        Box::new(self)
    }
}

fn prefixed_items<'a, T>(prefix: &'static str, items: &'a [T]) -> Box<dyn fmt::Display + 'a>
where
    T: Show + 'a,
{
    Box::new(fmtools::join(
        "\n",
        items
            .iter()
            .map(move |item| fmtools::fmt!(move {prefix} "\t" { item._fmt() })),
    ))
}

fn prefixed_item<'a, T>(prefix: &'static str, item: Option<T>) -> Box<dyn fmt::Display + 'a>
where
    T: Show + 'a,
{
    item.map_or_else(|| ()._fmt(), |item| prefixed_item0(prefix, item))
}

fn prefixed_item0<'a, T>(prefix: &'static str, item: T) -> Box<dyn fmt::Display + 'a>
where
    T: Show + 'a,
{
    Box::new(fmtools::fmt!(move {prefix} "\t" { item._fmt() }))
}