1use std::fmt::{self, Display};
28
29pub trait Showable {
32 fn format_show(&self, f: &mut fmt::Formatter) -> fmt::Result;
33}
34
35impl<T: Showable + ?Sized> Showable for &T {
37 #[inline(always)]
38 fn format_show(&self, f: &mut fmt::Formatter) -> fmt::Result {
39 (**self).format_show(f)
40 }
41}
42
43impl Showable for i32 {
45 #[inline(always)]
46 fn format_show(&self, f: &mut fmt::Formatter) -> fmt::Result {
47 Display::fmt(self, f)
48 }
49}
50
51impl Showable for i64 {
52 #[inline(always)]
53 fn format_show(&self, f: &mut fmt::Formatter) -> fmt::Result {
54 Display::fmt(self, f)
55 }
56}
57
58impl Showable for u64 {
59 #[inline(always)]
60 fn format_show(&self, f: &mut fmt::Formatter) -> fmt::Result {
61 Display::fmt(self, f)
62 }
63}
64
65impl Showable for usize {
66 #[inline(always)]
67 fn format_show(&self, f: &mut fmt::Formatter) -> fmt::Result {
68 Display::fmt(self, f)
69 }
70}
71
72impl Showable for f64 {
73 #[inline(always)]
74 fn format_show(&self, f: &mut fmt::Formatter) -> fmt::Result {
75 Display::fmt(self, f)
76 }
77}
78
79impl Showable for logicaffeine_data::LogosInt {
84 #[inline(always)]
85 fn format_show(&self, f: &mut fmt::Formatter) -> fmt::Result {
86 Display::fmt(self, f)
87 }
88}
89
90impl Showable for logicaffeine_data::LogosRational {
91 #[inline(always)]
92 fn format_show(&self, f: &mut fmt::Formatter) -> fmt::Result {
93 Display::fmt(self, f)
94 }
95}
96
97impl Showable for logicaffeine_data::LogosDecimal {
100 #[inline(always)]
101 fn format_show(&self, f: &mut fmt::Formatter) -> fmt::Result {
102 Display::fmt(self, f)
103 }
104}
105
106impl Showable for logicaffeine_data::LogosComplex {
108 #[inline(always)]
109 fn format_show(&self, f: &mut fmt::Formatter) -> fmt::Result {
110 Display::fmt(self, f)
111 }
112}
113
114impl Showable for logicaffeine_data::LogosModular {
116 #[inline(always)]
117 fn format_show(&self, f: &mut fmt::Formatter) -> fmt::Result {
118 Display::fmt(self, f)
119 }
120}
121
122impl Showable for logicaffeine_data::LogosQuantity {
124 #[inline(always)]
125 fn format_show(&self, f: &mut fmt::Formatter) -> fmt::Result {
126 Display::fmt(self, f)
127 }
128}
129
130impl Showable for logicaffeine_data::LogosMoney {
131 #[inline(always)]
132 fn format_show(&self, f: &mut fmt::Formatter) -> fmt::Result {
133 Display::fmt(self, f)
134 }
135}
136
137impl Showable for logicaffeine_data::LogosUuid {
138 #[inline(always)]
139 fn format_show(&self, f: &mut fmt::Formatter) -> fmt::Result {
140 Display::fmt(self, f)
141 }
142}
143
144impl Showable for bool {
145 #[inline(always)]
146 fn format_show(&self, f: &mut fmt::Formatter) -> fmt::Result {
147 Display::fmt(self, f)
148 }
149}
150
151impl Showable for u8 {
152 #[inline(always)]
153 fn format_show(&self, f: &mut fmt::Formatter) -> fmt::Result {
154 Display::fmt(self, f)
155 }
156}
157
158impl Showable for char {
159 #[inline(always)]
160 fn format_show(&self, f: &mut fmt::Formatter) -> fmt::Result {
161 Display::fmt(self, f)
162 }
163}
164
165impl Showable for String {
166 #[inline(always)]
167 fn format_show(&self, f: &mut fmt::Formatter) -> fmt::Result {
168 Display::fmt(self, f)
169 }
170}
171
172impl Showable for &str {
173 #[inline(always)]
174 fn format_show(&self, f: &mut fmt::Formatter) -> fmt::Result {
175 Display::fmt(self, f)
176 }
177}
178
179impl<T: Showable> Showable for Vec<T> {
181 #[inline(always)]
182 fn format_show(&self, f: &mut fmt::Formatter) -> fmt::Result {
183 write!(f, "[")?;
184 for (i, item) in self.iter().enumerate() {
185 if i > 0 {
186 write!(f, ", ")?;
187 }
188 item.format_show(f)?;
189 }
190 write!(f, "]")
191 }
192}
193
194impl<T: Showable> Showable for logicaffeine_data::LogosSeq<T> {
195 #[inline(always)]
196 fn format_show(&self, f: &mut fmt::Formatter) -> fmt::Result {
197 let inner = self.borrow();
198 write!(f, "[")?;
199 for (i, item) in inner.iter().enumerate() {
200 if i > 0 {
201 write!(f, ", ")?;
202 }
203 item.format_show(f)?;
204 }
205 write!(f, "]")
206 }
207}
208
209impl<K: Showable + Eq + std::hash::Hash, V: Showable> Showable for logicaffeine_data::LogosMap<K, V> {
210 #[inline(always)]
211 fn format_show(&self, f: &mut fmt::Formatter) -> fmt::Result {
212 let inner = self.borrow();
213 write!(f, "{{")?;
214 for (i, (k, v)) in inner.iter().enumerate() {
215 if i > 0 {
216 write!(f, ", ")?;
217 }
218 k.format_show(f)?;
219 write!(f, ": ")?;
220 v.format_show(f)?;
221 }
222 write!(f, "}}")
223 }
224}
225
226impl<T: Showable, S: std::hash::BuildHasher> Showable for indexmap::IndexSet<T, S> {
230 #[inline(always)]
231 fn format_show(&self, f: &mut fmt::Formatter) -> fmt::Result {
232 write!(f, "{{")?;
233 for (i, v) in self.iter().enumerate() {
234 if i > 0 {
235 write!(f, ", ")?;
236 }
237 v.format_show(f)?;
238 }
239 write!(f, "}}")
240 }
241}
242
243impl<T: Showable> Showable for [T] {
245 #[inline(always)]
246 fn format_show(&self, f: &mut fmt::Formatter) -> fmt::Result {
247 write!(f, "[")?;
248 for (i, item) in self.iter().enumerate() {
249 if i > 0 {
250 write!(f, ", ")?;
251 }
252 item.format_show(f)?;
253 }
254 write!(f, "]")
255 }
256}
257
258impl<T: Showable> Showable for Option<T> {
263 #[inline(always)]
264 fn format_show(&self, f: &mut fmt::Formatter) -> fmt::Result {
265 match self {
266 Some(v) => v.format_show(f),
267 None => write!(f, "nothing"),
268 }
269 }
270}
271
272impl Showable for logicaffeine_data::crdt::GCounter {
274 #[inline(always)]
275 fn format_show(&self, f: &mut fmt::Formatter) -> fmt::Result {
276 write!(f, "{}", self.value())
277 }
278}
279
280impl Showable for logicaffeine_data::crdt::PNCounter {
281 #[inline(always)]
282 fn format_show(&self, f: &mut fmt::Formatter) -> fmt::Result {
283 write!(f, "{}", self.value())
284 }
285}
286
287impl<T: Showable> Showable for logicaffeine_data::crdt::LWWRegister<T> {
289 #[inline(always)]
290 fn format_show(&self, f: &mut fmt::Formatter) -> fmt::Result {
291 self.get().format_show(f)
292 }
293}
294
295impl<T: Showable + Clone + PartialEq> Showable for logicaffeine_data::crdt::MVRegister<T> {
297 #[inline(always)]
298 fn format_show(&self, f: &mut fmt::Formatter) -> fmt::Result {
299 let values = self.values();
300 if values.len() == 1 {
301 values[0].format_show(f)
302 } else if values.is_empty() {
303 write!(f, "nothing")
304 } else {
305 write!(f, "conflict[")?;
307 for (i, val) in values.iter().enumerate() {
308 if i > 0 {
309 write!(f, ", ")?;
310 }
311 val.format_show(f)?;
312 }
313 write!(f, "]")
314 }
315 }
316}
317
318impl Showable for logicaffeine_data::Value {
320 #[inline(always)]
321 fn format_show(&self, f: &mut fmt::Formatter) -> fmt::Result {
322 Display::fmt(self, f)
323 }
324}
325
326impl Showable for std::time::Duration {
328 #[inline(always)]
329 fn format_show(&self, f: &mut fmt::Formatter) -> fmt::Result {
330 let nanos = self.as_nanos();
331 if nanos >= 3_600_000_000_000 {
332 write!(f, "{}h", nanos / 3_600_000_000_000)
334 } else if nanos >= 60_000_000_000 {
335 write!(f, "{}min", nanos / 60_000_000_000)
337 } else if nanos >= 1_000_000_000 {
338 write!(f, "{}s", nanos / 1_000_000_000)
340 } else if nanos >= 1_000_000 {
341 write!(f, "{}ms", nanos / 1_000_000)
343 } else if nanos >= 1_000 {
344 write!(f, "{}μs", nanos / 1_000)
346 } else {
347 write!(f, "{}ns", nanos)
349 }
350 }
351}
352
353#[inline(always)]
356pub fn show<T: Showable>(value: &T) {
357 struct Wrapper<'a, T>(&'a T);
358 impl<T: Showable> Display for Wrapper<'_, T> {
359 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
360 self.0.format_show(f)
361 }
362 }
363 println!("{}", Wrapper(value));
364}
365
366pub fn read_line() -> String {
367 let mut buffer = String::new();
368 std::io::stdin().read_line(&mut buffer).unwrap_or(0);
369 buffer.trim().to_string()
370}
371
372#[inline(always)]
373pub fn print<T: Display>(x: T) {
374 print!("{}", x);
375}
376
377#[inline(always)]
378pub fn eprintln<T: Display>(x: T) {
379 eprintln!("{}", x);
380}
381
382#[inline(always)]
383pub fn println<T: Display>(x: T) {
384 println!("{}", x);
385}