Parse a Kubernetes-style memory quantity to bytes. Supports: `Ki`, `Mi`, `Gi`, `Ti` (binary) and `k`, `M`, `G`, `T` (decimal), as well as plain byte values.
(quantity: &str)
| 1092 | /// Supports: `Ki`, `Mi`, `Gi`, `Ti` (binary) and `k`, `M`, `G`, `T` |
| 1093 | /// (decimal), as well as plain byte values. |
| 1094 | fn parse_memory_to_bytes(quantity: &str) -> Option<u64> { |
| 1095 | let suffixes: &[(&str, u64)] = &[ |
| 1096 | ("Ei", 1024 * 1024 * 1024 * 1024 * 1024 * 1024), |
| 1097 | ("Pi", 1024 * 1024 * 1024 * 1024 * 1024), |
| 1098 | ("Ti", 1024 * 1024 * 1024 * 1024), |
| 1099 | ("Gi", 1024 * 1024 * 1024), |
| 1100 | ("Mi", 1024 * 1024), |
| 1101 | ("Ki", 1024), |
| 1102 | ("E", 1_000_000_000_000_000_000), |
| 1103 | ("P", 1_000_000_000_000_000), |
| 1104 | ("T", 1_000_000_000_000), |
| 1105 | ("G", 1_000_000_000), |
| 1106 | ("M", 1_000_000), |
| 1107 | ("K", 1_000), |
| 1108 | ("k", 1_000), |
| 1109 | ]; |
| 1110 | |
| 1111 | for (suffix, multiplier) in suffixes { |
| 1112 | if let Some(num_str) = quantity.strip_suffix(suffix) { |
| 1113 | let num: u64 = num_str.parse().ok()?; |
| 1114 | return num.checked_mul(*multiplier); |
| 1115 | } |
| 1116 | } |
| 1117 | |
| 1118 | // Plain bytes. |
| 1119 | quantity.parse().ok() |
| 1120 | } |
| 1121 | |
| 1122 | #[cfg(test)] |
| 1123 | mod tests { |
no test coverage detected