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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
use std::fmt;
use std::str::FromStr;

pub use human_size::ParsingError as ByteSizeParseError;
use human_size::{Any, Size, SpecificSize};
use serde::ser::Serializer;
use serde::{de, Serialize};

/// Represents a size in bytes.
///
/// `ByteSize` can be parsed from strings or with Serde, and remembers the original unit that was
/// used to describe it for stable serialization. Use `ByteSize::infer` to infer the most
/// appropriate unit for a number of bytes.
///
/// Units based on 1000 and 1024 are both supported:
///  - To refer to the 1000-based versions, use "kB" and "MB".
///  - To refer to the 1024-based versions, use "KiB" and "MiB".
///
/// # Examples
///
/// Infer the best unit:
///
/// ```
/// use relay_config::ByteSize;
///
/// let size = ByteSize::infer(42 * 1000 * 1000);
/// assert_eq!("42MB", size.to_string());
/// ```
///
/// Format a 1024-based size to string:
///
/// ```
/// use relay_config::ByteSize;
///
/// let size = ByteSize::kibibytes(42);
/// assert_eq!("42KiB", size.to_string());
/// ```
pub struct ByteSize(Size);

impl ByteSize {
    fn multiple(value: u32, multiple: Any) -> Self {
        // Can be unwrapped because f64::from<u32> always returns a "normal" number.
        // See https://doc.rust-lang.org/nightly/std/primitive.f64.html#method.is_normal
        Self(SpecificSize::new(value, multiple).unwrap())
    }

    fn try_multiple(value: u32, multiple: Any) -> Option<Self> {
        let factor = match multiple {
            Any::Mebibyte => 1024 * 1024,
            Any::Megabyte => 1000 * 1000,
            Any::Kibibyte => 1024,
            Any::Kilobyte => 1000,
            _ => 1,
        };

        match value % factor {
            0 => Some(Self::multiple(value / factor, multiple)),
            _ => None,
        }
    }

    /// Create a byte size from bytes, inferring the most appropriate unit.
    pub fn infer(value: u32) -> Self {
        Self::try_multiple(value, Any::Mebibyte)
            .or_else(|| Self::try_multiple(value, Any::Megabyte))
            .or_else(|| Self::try_multiple(value, Any::Kibibyte))
            .or_else(|| Self::try_multiple(value, Any::Kilobyte))
            .unwrap_or_else(|| Self::bytes(value))
    }

    /// Create a byte size from bytes.
    pub fn bytes(value: u32) -> Self {
        Self::multiple(value, Any::Byte)
    }

    /// Create a byte size from 1024-based kibibytes.
    pub fn kibibytes(value: u32) -> Self {
        Self::multiple(value, Any::Kibibyte)
    }

    /// Create a byte size from 1024-based mebibytes.
    pub fn mebibytes(value: u32) -> Self {
        Self::multiple(value, Any::Mebibyte)
    }

    /// Return the value in bytes.
    pub fn as_bytes(&self) -> usize {
        let byte_size = self.0.into::<human_size::Byte>();
        byte_size.value() as usize
    }
}

impl From<u32> for ByteSize {
    fn from(value: u32) -> ByteSize {
        ByteSize::infer(value)
    }
}

impl FromStr for ByteSize {
    type Err = ByteSizeParseError;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        match value.parse::<u32>() {
            Ok(bytes) => Ok(Self::bytes(bytes)),
            Err(_) => value.parse().map(ByteSize),
        }
    }
}

impl fmt::Display for ByteSize {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}{}", self.0.value(), self.0.multiple())
    }
}

impl fmt::Debug for ByteSize {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("ByteSize")
            .field(&format_args!("{}", self.0))
            .finish()
    }
}

impl Serialize for ByteSize {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.collect_str(self)
    }
}

impl<'de> de::Deserialize<'de> for ByteSize {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        struct V;

        impl<'de> de::Visitor<'de> for V {
            type Value = ByteSize;

            fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
                formatter.write_str("data size")
            }

            fn visit_u64<E>(self, value: u64) -> Result<ByteSize, E>
            where
                E: de::Error,
            {
                match value.try_into() {
                    Ok(value32) => Ok(ByteSize::infer(value32)),
                    Err(_) => Err(de::Error::invalid_value(
                        de::Unexpected::Unsigned(value),
                        &self,
                    )),
                }
            }

            fn visit_str<E>(self, value: &str) -> Result<ByteSize, E>
            where
                E: de::Error,
            {
                value
                    .parse()
                    .map_err(|_| de::Error::invalid_value(de::Unexpected::Str(value), &self))
            }
        }

        deserializer.deserialize_any(V)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_infer() {
        let size = ByteSize::infer(42);
        assert_eq!(42, size.as_bytes());
        assert_eq!("42B", size.to_string());

        let size = ByteSize::infer(1000);
        assert_eq!(1000, size.as_bytes());
        assert_eq!("1kB", size.to_string());

        let size = ByteSize::infer(1024);
        assert_eq!(1024, size.as_bytes());
        assert_eq!("1KiB", size.to_string());

        let size = ByteSize::infer(1000 * 1000);
        assert_eq!(1000 * 1000, size.as_bytes());
        assert_eq!("1MB", size.to_string());

        let size = ByteSize::infer(1024 * 1024);
        assert_eq!(1024 * 1024, size.as_bytes());
        assert_eq!("1MiB", size.to_string());
    }

    #[test]
    fn test_parse() {
        let size = ByteSize::from_str("4242").unwrap();
        assert_eq!(4242, size.as_bytes());
        assert_eq!("4242B", size.to_string());

        let size = ByteSize::from_str("42B").unwrap();
        assert_eq!(42, size.as_bytes());
        assert_eq!("42B", size.to_string());

        // NOTE: Lowercase k is kilo
        let size = ByteSize::from_str("1kB").unwrap();
        assert_eq!(1000, size.as_bytes());
        assert_eq!("1kB", size.to_string());

        // NOTE: Uppercase K is kibi
        let size = ByteSize::from_str("1KB").unwrap();
        assert_eq!(1024, size.as_bytes());
        assert_eq!("1KiB", size.to_string());

        let size = ByteSize::from_str("1KiB").unwrap();
        assert_eq!(1024, size.as_bytes());
        assert_eq!("1KiB", size.to_string());

        let size = ByteSize::from_str("1MB").unwrap();
        assert_eq!(1000 * 1000, size.as_bytes());
        assert_eq!("1MB", size.to_string());

        let size = ByteSize::from_str("1MiB").unwrap();
        assert_eq!(1024 * 1024, size.as_bytes());
        assert_eq!("1MiB", size.to_string());
    }

    #[test]
    fn test_as_bytes() {
        let size = ByteSize::bytes(42);
        assert_eq!(42, size.as_bytes());

        let size = ByteSize::kibibytes(42);
        assert_eq!(42 * 1024, size.as_bytes());

        let size = ByteSize::mebibytes(42);
        assert_eq!(42 * 1024 * 1024, size.as_bytes());
    }

    #[test]
    fn test_serde_number() {
        let size = serde_json::from_str::<ByteSize>("1024").unwrap();
        let json = serde_json::to_string(&size).unwrap();
        assert_eq!(json, "\"1KiB\"");
    }

    #[test]
    fn test_serde_string() {
        let size = serde_json::from_str::<ByteSize>("\"1KiB\"").unwrap();
        let json = serde_json::to_string(&size).unwrap();
        assert_eq!(json, "\"1KiB\"");
    }
}