|
| 1 | +// SPDX-FileCopyrightText: Copyright © 2025 Serpent OS Developers |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: MPL-2.0 |
| 4 | + |
| 5 | +use std::{fmt, str::FromStr}; |
| 6 | + |
| 7 | +use crate::UnsupportedValue; |
| 8 | + |
| 9 | +use super::FromKdlType; |
| 10 | + |
| 11 | +/// Storage unit |
| 12 | +#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)] |
| 13 | +#[repr(u64)] |
| 14 | +pub enum StorageUnit { |
| 15 | + /// Bytes |
| 16 | + #[default] |
| 17 | + Bytes = 1, |
| 18 | + |
| 19 | + // as 1000s, |
| 20 | + /// Kilobytes |
| 21 | + Kilobytes = 1000, |
| 22 | + /// Megabytes |
| 23 | + Megabytes = 1_000_000, |
| 24 | + /// Gigabytes |
| 25 | + Gigabytes = 1_000_000_000, |
| 26 | + /// Terabytes |
| 27 | + Terabytes = 1_000_000_000_000, |
| 28 | + |
| 29 | + // as 1024s, |
| 30 | + /// Kibibytes |
| 31 | + Kibibytes = 1024, |
| 32 | + /// Mebibytes |
| 33 | + Mebibytes = 1024 * 1024, |
| 34 | + /// Gibibytes |
| 35 | + Gibibytes = 1024 * 1024 * 1024, |
| 36 | + /// Tebibytes |
| 37 | + Tebibytes = 1024 * 1024 * 1024 * 1024, |
| 38 | +} |
| 39 | + |
| 40 | +impl fmt::Display for StorageUnit { |
| 41 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 42 | + match self { |
| 43 | + StorageUnit::Bytes => f.write_str("bytes"), |
| 44 | + StorageUnit::Kilobytes => f.write_str("kilobytes"), |
| 45 | + StorageUnit::Megabytes => f.write_str("megabytes"), |
| 46 | + StorageUnit::Gigabytes => f.write_str("gigabytes"), |
| 47 | + StorageUnit::Terabytes => f.write_str("terabytes"), |
| 48 | + StorageUnit::Kibibytes => f.write_str("kibibytes"), |
| 49 | + StorageUnit::Mebibytes => f.write_str("mebibytes"), |
| 50 | + StorageUnit::Gibibytes => f.write_str("gibibytes"), |
| 51 | + StorageUnit::Tebibytes => f.write_str("tebibytes"), |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +impl FromStr for StorageUnit { |
| 57 | + type Err = crate::Error; |
| 58 | + |
| 59 | + /// Attempt to convert a string to a storage unit |
| 60 | + fn from_str(value: &str) -> Result<Self, Self::Err> { |
| 61 | + match value { |
| 62 | + "b" => Ok(Self::Bytes), |
| 63 | + "kb" => Ok(Self::Kilobytes), |
| 64 | + "mb" => Ok(Self::Megabytes), |
| 65 | + "gb" => Ok(Self::Gigabytes), |
| 66 | + "tb" => Ok(Self::Terabytes), |
| 67 | + "kib" => Ok(Self::Kibibytes), |
| 68 | + "mib" => Ok(Self::Mebibytes), |
| 69 | + "gib" => Ok(Self::Gibibytes), |
| 70 | + "tib" => Ok(Self::Tebibytes), |
| 71 | + _ => Err(crate::Error::UnknownVariant), |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +impl FromKdlType<'_> for StorageUnit { |
| 77 | + fn from_kdl_type(id: &kdl::KdlEntry) -> Result<Self, crate::Error> { |
| 78 | + let ty_id = if let Some(ty) = id.ty() { |
| 79 | + ty.value().to_lowercase() |
| 80 | + } else { |
| 81 | + "b".into() |
| 82 | + }; |
| 83 | + |
| 84 | + let v = ty_id.parse().map_err(|_| UnsupportedValue { |
| 85 | + at: id.span(), |
| 86 | + advice: Some("'b', 'kb', 'mb', 'gb', 'tb', 'kib', 'mib', 'gib', 'tib' are supported".into()), |
| 87 | + })?; |
| 88 | + Ok(v) |
| 89 | + } |
| 90 | +} |
0 commit comments