Skip to content

Commit e86c768

Browse files
committed
provisioning: More types
Signed-off-by: Ikey Doherty <[email protected]>
1 parent 094061a commit e86c768

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

crates/provisioning/src/errors.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,15 @@ pub struct InvalidArguments {
113113
#[help]
114114
pub advice: Option<String>,
115115
}
116+
117+
/// Error for missing types
118+
#[derive(Debug, Diagnostic, Error)]
119+
#[error("missing type")]
120+
#[diagnostic(severity(error))]
121+
pub struct MissingType {
122+
#[label]
123+
pub at: SourceSpan,
124+
125+
#[help]
126+
pub advice: Option<String>,
127+
}

crates/provisioning/src/types.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ use crate::Error;
1111
mod partition_table;
1212
pub use partition_table::*;
1313

14+
mod units;
15+
pub use units::*;
16+
1417
/// The type of a KDL value
1518
#[derive(Debug)]
1619
pub enum KdlType {
@@ -55,3 +58,7 @@ impl fmt::Display for KdlType {
5558
pub trait FromKdlProperty<'a>: Sized {
5659
fn from_kdl_property(entry: &'a KdlEntry) -> Result<Self, Error>;
5760
}
61+
62+
pub trait FromKdlType<'a>: Sized {
63+
fn from_kdl_type(id: &'a KdlEntry) -> Result<Self, Error>;
64+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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

Comments
 (0)