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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
use std::collections::{HashSet, HashMap};

use bc_components::{ARID, PublicKeyBase, PrivateKeyBase};
use bc_envelope::prelude::*;
use bytes::Bytes;

use crate::Receipt;


pub fn prefix(s: &str, len: usize) -> String {
    s.chars().take(len).collect()
}

pub trait Prefix {
    fn prefix(&self, len: usize) -> String;
}

impl<T> Prefix for T
where
    T: AsRef<str>,
{
    fn prefix(&self, len: usize) -> String {
        prefix(self.as_ref(), len)
    }
}


pub fn suffix(s: &str, len: usize) -> String {
    s.chars().rev().take(len).collect::<String>().chars().rev().collect()
}

pub trait Suffix {
    fn suffix(&self, len: usize) -> String;
}

impl<T> Suffix for T
where
    T: AsRef<str>,
{
    fn suffix(&self, len: usize) -> String {
        suffix(self.as_ref(), len)
    }
}


pub fn flanked_by(s: &str, left: &str, right: &str) -> String {
    left.to_owned() + s + right
}

pub trait FlankedBy {
    fn flanked_by(&self, left: &str, right: &str) -> String;
}

impl<T> FlankedBy for T
where
    T: AsRef<str>,
{
    fn flanked_by(&self, left: &str, right: &str) -> String {
        flanked_by(self.as_ref(), left, right)
    }
}

pub fn flanked_abbrev(s: &str) -> String {
    s.flanked_by("<", ">")
}

pub trait FlankedAbbrev {
    fn flanked_abbrev(&self) -> String;
}

impl<T> FlankedAbbrev for T
where
    T: AsRef<str>,
{
    fn flanked_abbrev(&self) -> String {
        flanked_abbrev(self.as_ref())
    }
}

pub fn flanked_function(s: &str) -> String {
    s.flanked_by("«", "»")
}

pub trait FlankedFunction {
    fn flanked_function(&self) -> String;
}

impl<T> FlankedFunction for T
where
    T: AsRef<str>,
{
    fn flanked_function(&self) -> String {
        flanked_function(self.as_ref())
    }
}

pub trait Abbrev {
    fn abbrev(&self) -> String;
}

fn abbreviate_string(s: impl AsRef<str>) -> String {
    s.as_ref().prefix(8).flanked_abbrev()
}

fn abbreviate_opt_string(s: Option<impl AsRef<str>>) -> String {
    if let Some(s) = s {
        abbreviate_string(s)
    } else {
        "<None>".to_string()
    }
}

impl Abbrev for str {
    fn abbrev(&self) -> String {
        abbreviate_string(self)
    }
}

impl Abbrev for String {
    fn abbrev(&self) -> String {
        abbreviate_string(self)
    }
}

impl Abbrev for Option<String> {
    fn abbrev(&self) -> String {
        abbreviate_opt_string(self.as_deref())
    }
}

impl Abbrev for Option<&String> {
    fn abbrev(&self) -> String {
        abbreviate_opt_string(self.as_deref())
    }
}

impl Abbrev for Option<&str> {
    fn abbrev(&self) -> String {
        abbreviate_opt_string(self.as_deref())
    }
}

impl Abbrev for ARID {
    fn abbrev(&self) -> String {
        self.ur_string().suffix(8).flanked_abbrev()
    }
}

impl Abbrev for PublicKeyBase {
    fn abbrev(&self) -> String {
        self.ur_string().suffix(8).flanked_abbrev()
    }
}

impl Abbrev for PrivateKeyBase {
    fn abbrev(&self) -> String {
        self.ur_string().suffix(8).flanked_abbrev()
    }
}

impl Abbrev for Envelope {
    fn abbrev(&self) -> String {
        self.ur_string().suffix(8).flanked_abbrev()
    }
}

impl Abbrev for Receipt {
    fn abbrev(&self) -> String {
        let envelope: Envelope = self.clone().into();
        envelope.ur_string().suffix(8).flanked_abbrev()
    }
}

impl Abbrev for Bytes {
    fn abbrev(&self) -> String {
        format!("{} bytes", self.len()).flanked_abbrev()
    }
}

impl<T> Abbrev for Vec<T>
where
    T: Abbrev,
{
    fn abbrev(&self) -> String {
        let mut items = self.iter().map(|i| i.abbrev()).collect::<Vec<_>>();
        items.sort();
        items.join(", ").flanked_by("[", "]")
    }
}

impl<T> Abbrev for HashSet<T>
where
    T: Abbrev,
{
    fn abbrev(&self) -> String {
        let mut items = self.iter().map(|i| i.abbrev()).collect::<Vec<_>>();
        items.sort();
        items.join(", ").flanked_by("[", "]")
    }
}

impl<K, V> Abbrev for HashMap<K, V>
where
    K: Abbrev,
    V: Abbrev,
{
    fn abbrev(&self) -> String {
        let mut items = self
            .iter()
            .map(|(k, v)| format!("{}: {}", k.abbrev(), v.abbrev()))
            .collect::<Vec<_>>();
        items.sort();
        items.join(", ").flanked_by("{", "}")
    }
}