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
//! Format wrappers for Zebra

use std::{fmt, ops};

#[cfg(any(test, feature = "proptest-impl"))]
use proptest::prelude::*;
#[cfg(any(test, feature = "proptest-impl"))]
use proptest_derive::Arbitrary;

pub mod time;

pub use time::{duration_short, humantime_milliseconds, humantime_seconds};

/// Wrapper to override `Debug`, redirecting it to only output the type's name.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(any(test, feature = "proptest-impl"), derive(Arbitrary))]
pub struct TypeNameToDebug<T>(pub T);

impl<T> fmt::Debug for TypeNameToDebug<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(std::any::type_name::<T>())
    }
}

impl<T> ops::Deref for TypeNameToDebug<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<T> ops::DerefMut for TypeNameToDebug<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl<T> From<T> for TypeNameToDebug<T> {
    fn from(t: T) -> Self {
        Self(t)
    }
}

/// Wrapper to override `Debug`, redirecting it to the `Display` impl.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(any(test, feature = "proptest-impl"), derive(Arbitrary))]
pub struct DisplayToDebug<T: fmt::Display>(pub T);

impl<T: fmt::Display> fmt::Debug for DisplayToDebug<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

impl<T: fmt::Display> ops::Deref for DisplayToDebug<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<T: fmt::Display> ops::DerefMut for DisplayToDebug<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl<T: fmt::Display> From<T> for DisplayToDebug<T> {
    fn from(t: T) -> Self {
        Self(t)
    }
}

/// Wrapper to override `Debug` to display a shorter summary of the type.
///
/// For collections and exact size iterators, it only displays the
/// collection/iterator type, the item type, and the length.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SummaryDebug<CollectionOrIter>(pub CollectionOrIter)
where
    CollectionOrIter: IntoIterator + Clone,
    <CollectionOrIter as IntoIterator>::IntoIter: ExactSizeIterator;

impl<CollectionOrIter> fmt::Debug for SummaryDebug<CollectionOrIter>
where
    CollectionOrIter: IntoIterator + Clone,
    <CollectionOrIter as IntoIterator>::IntoIter: ExactSizeIterator,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}<{}>, len={}",
            std::any::type_name::<CollectionOrIter>(),
            std::any::type_name::<<CollectionOrIter as IntoIterator>::Item>(),
            self.0.clone().into_iter().len()
        )
    }
}

impl<CollectionOrIter> ops::Deref for SummaryDebug<CollectionOrIter>
where
    CollectionOrIter: IntoIterator + Clone,
    <CollectionOrIter as IntoIterator>::IntoIter: ExactSizeIterator,
{
    type Target = CollectionOrIter;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<CollectionOrIter> ops::DerefMut for SummaryDebug<CollectionOrIter>
where
    CollectionOrIter: IntoIterator + Clone,
    <CollectionOrIter as IntoIterator>::IntoIter: ExactSizeIterator,
{
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl<CollectionOrIter> From<CollectionOrIter> for SummaryDebug<CollectionOrIter>
where
    CollectionOrIter: IntoIterator + Clone,
    <CollectionOrIter as IntoIterator>::IntoIter: ExactSizeIterator,
{
    fn from(collection: CollectionOrIter) -> Self {
        Self(collection)
    }
}

impl<CollectionOrIter> IntoIterator for SummaryDebug<CollectionOrIter>
where
    CollectionOrIter: IntoIterator + Clone,
    <CollectionOrIter as IntoIterator>::IntoIter: ExactSizeIterator,
{
    type Item = <CollectionOrIter as IntoIterator>::Item;
    type IntoIter = <CollectionOrIter as IntoIterator>::IntoIter;

    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}

#[cfg(any(test, feature = "proptest-impl"))]
impl<CollectionOrIter> Arbitrary for SummaryDebug<CollectionOrIter>
where
    CollectionOrIter: Arbitrary + IntoIterator + Clone + 'static,
    <CollectionOrIter as IntoIterator>::IntoIter: ExactSizeIterator,
{
    type Parameters = <CollectionOrIter as Arbitrary>::Parameters;

    fn arbitrary_with(args: Self::Parameters) -> Self::Strategy {
        CollectionOrIter::arbitrary_with(args)
            .prop_map_into()
            .boxed()
    }

    type Strategy = BoxedStrategy<Self>;
}

/// Wrapper to override `Debug`, redirecting it to hex-encode the type.
/// The type must implement `AsRef<[u8]>`.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, Default)]
#[cfg_attr(any(test, feature = "proptest-impl"), derive(Arbitrary))]
#[serde(transparent)]
pub struct HexDebug<T: AsRef<[u8]>>(pub T);

impl<T: AsRef<[u8]>> fmt::Debug for HexDebug<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple(std::any::type_name::<T>())
            .field(&hex::encode(self.as_ref()))
            .finish()
    }
}

impl<T: AsRef<[u8]>> ops::Deref for HexDebug<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<T: AsRef<[u8]>> ops::DerefMut for HexDebug<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl<T: AsRef<[u8]>> From<T> for HexDebug<T> {
    fn from(t: T) -> Self {
        Self(t)
    }
}