gel_protocol/model/
memory.rs

1use std::fmt::{Debug, Display};
2
3/// A type for cfg::memory received from the database
4#[derive(Copy, Debug, Clone, PartialEq)]
5#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
6pub struct ConfigMemory(pub i64);
7
8impl ConfigMemory {}
9
10static KIB: i64 = 1024;
11static MIB: i64 = 1024 * KIB;
12static GIB: i64 = 1024 * MIB;
13static TIB: i64 = 1024 * GIB;
14
15impl Display for ConfigMemory {
16    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
17        // Use the same rendering logic we have in Gel server
18        // to cast cfg::memory to std::str.
19        let v = self.0;
20        if v >= TIB && v % TIB == 0 {
21            write!(f, "{}TiB", v / TIB)
22        } else if v >= GIB && v % GIB == 0 {
23            write!(f, "{}GiB", v / GIB)
24        } else if v >= MIB && v % MIB == 0 {
25            write!(f, "{}MiB", v / MIB)
26        } else if v >= KIB && v % KIB == 0 {
27            write!(f, "{}KiB", v / KIB)
28        } else {
29            write!(f, "{v}B")
30        }
31    }
32}