Skip to content

Commit

Permalink
provisioning: More types
Browse files Browse the repository at this point in the history
Signed-off-by: Ikey Doherty <[email protected]>
  • Loading branch information
ikeycode committed Feb 3, 2025
1 parent 094061a commit e86c768
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
12 changes: 12 additions & 0 deletions crates/provisioning/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,15 @@ pub struct InvalidArguments {
#[help]
pub advice: Option<String>,
}

/// Error for missing types
#[derive(Debug, Diagnostic, Error)]
#[error("missing type")]
#[diagnostic(severity(error))]
pub struct MissingType {
#[label]
pub at: SourceSpan,

#[help]
pub advice: Option<String>,
}
7 changes: 7 additions & 0 deletions crates/provisioning/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ use crate::Error;
mod partition_table;
pub use partition_table::*;

mod units;
pub use units::*;

/// The type of a KDL value
#[derive(Debug)]
pub enum KdlType {
Expand Down Expand Up @@ -55,3 +58,7 @@ impl fmt::Display for KdlType {
pub trait FromKdlProperty<'a>: Sized {
fn from_kdl_property(entry: &'a KdlEntry) -> Result<Self, Error>;
}

pub trait FromKdlType<'a>: Sized {
fn from_kdl_type(id: &'a KdlEntry) -> Result<Self, Error>;
}
90 changes: 90 additions & 0 deletions crates/provisioning/src/types/units.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// SPDX-FileCopyrightText: Copyright © 2025 Serpent OS Developers
//
// SPDX-License-Identifier: MPL-2.0

use std::{fmt, str::FromStr};

use crate::UnsupportedValue;

use super::FromKdlType;

/// Storage unit
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u64)]
pub enum StorageUnit {
/// Bytes
#[default]
Bytes = 1,

// as 1000s,
/// Kilobytes
Kilobytes = 1000,
/// Megabytes
Megabytes = 1_000_000,
/// Gigabytes
Gigabytes = 1_000_000_000,
/// Terabytes
Terabytes = 1_000_000_000_000,

// as 1024s,
/// Kibibytes
Kibibytes = 1024,
/// Mebibytes
Mebibytes = 1024 * 1024,
/// Gibibytes
Gibibytes = 1024 * 1024 * 1024,
/// Tebibytes
Tebibytes = 1024 * 1024 * 1024 * 1024,
}

impl fmt::Display for StorageUnit {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
StorageUnit::Bytes => f.write_str("bytes"),
StorageUnit::Kilobytes => f.write_str("kilobytes"),
StorageUnit::Megabytes => f.write_str("megabytes"),
StorageUnit::Gigabytes => f.write_str("gigabytes"),
StorageUnit::Terabytes => f.write_str("terabytes"),
StorageUnit::Kibibytes => f.write_str("kibibytes"),
StorageUnit::Mebibytes => f.write_str("mebibytes"),
StorageUnit::Gibibytes => f.write_str("gibibytes"),
StorageUnit::Tebibytes => f.write_str("tebibytes"),
}
}
}

impl FromStr for StorageUnit {
type Err = crate::Error;

/// Attempt to convert a string to a storage unit
fn from_str(value: &str) -> Result<Self, Self::Err> {
match value {
"b" => Ok(Self::Bytes),
"kb" => Ok(Self::Kilobytes),
"mb" => Ok(Self::Megabytes),
"gb" => Ok(Self::Gigabytes),
"tb" => Ok(Self::Terabytes),
"kib" => Ok(Self::Kibibytes),
"mib" => Ok(Self::Mebibytes),
"gib" => Ok(Self::Gibibytes),
"tib" => Ok(Self::Tebibytes),
_ => Err(crate::Error::UnknownVariant),
}
}
}

impl FromKdlType<'_> for StorageUnit {
fn from_kdl_type(id: &kdl::KdlEntry) -> Result<Self, crate::Error> {
let ty_id = if let Some(ty) = id.ty() {
ty.value().to_lowercase()
} else {
"b".into()
};

let v = ty_id.parse().map_err(|_| UnsupportedValue {
at: id.span(),
advice: Some("'b', 'kb', 'mb', 'gb', 'tb', 'kib', 'mib', 'gib', 'tib' are supported".into()),
})?;
Ok(v)
}
}

0 comments on commit e86c768

Please sign in to comment.