Skip to content

Commit caee64c

Browse files
committed
Refactor to use shared unit conversion helpers
Our unit stuff was messy, this cleans it up a lot. Maybe at some point we should look for a crate that handles explicit units. Assisted-by: Claude Code (Sonnet 4.5) Signed-off-by: Colin Walters <[email protected]>
1 parent 5b1b1e1 commit caee64c

File tree

2 files changed

+25
-35
lines changed

2 files changed

+25
-35
lines changed

crates/kit/src/libvirt/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ pub(crate) fn convert_memory_to_mb(value: u32, unit: &str) -> Option<u32> {
103103
/// and binary (KiB, MiB, GiB - powers of 1024) units per libvirt specification
104104
/// Returns None if the unit is unknown or if the result overflows u64
105105
#[allow(dead_code)]
106-
pub(crate) fn convert_to_mb_u64(value: u64, unit: &str) -> Option<u64> {
106+
pub(crate) fn convert_to_mb(value: u64, unit: &str) -> Option<u64> {
107107
let value_u128 = value as u128;
108108
let mib_u128 = 1024 * 1024;
109109

crates/kit/src/utils.rs

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -138,38 +138,28 @@ pub(crate) fn parse_memory_to_mb(memory_str: &str) -> Result<u32> {
138138
}
139139

140140
// Check if it ends with a unit suffix
141-
if let Some(last_char) = memory_str.chars().last() {
142-
match last_char.to_ascii_uppercase() {
143-
'G' => {
144-
let number_part = &memory_str[..memory_str.len() - 1];
145-
let gb: f64 = number_part
146-
.parse()
147-
.context("Invalid number in memory specification")?;
148-
Ok((gb * 1024.0) as u32)
149-
}
150-
'M' => {
151-
let number_part = &memory_str[..memory_str.len() - 1];
152-
let mb: u32 = number_part
153-
.parse()
154-
.context("Invalid number in memory specification")?;
155-
Ok(mb)
156-
}
157-
'K' => {
158-
let number_part = &memory_str[..memory_str.len() - 1];
159-
let kb: u32 = number_part
160-
.parse()
161-
.context("Invalid number in memory specification")?;
162-
Ok(kb / 1024)
163-
}
164-
_ => {
165-
// No suffix, assume megabytes
166-
let mb: u32 = memory_str
167-
.parse()
168-
.context("Invalid number in memory specification")?;
169-
Ok(mb)
170-
}
171-
}
172-
} else {
173-
Err(eyre!("Memory specification cannot be empty - please provide a value like '2G', '1024M', or '512'"))
174-
}
141+
let last_char = memory_str
142+
.chars()
143+
.last()
144+
.ok_or_else(|| eyre!("Memory specification cannot be empty"))?;
145+
146+
let (number_str, unit) = match last_char.to_ascii_uppercase() {
147+
'G' => (&memory_str[..memory_str.len() - 1], "GiB"),
148+
'M' => (&memory_str[..memory_str.len() - 1], "MiB"),
149+
'K' => (&memory_str[..memory_str.len() - 1], "KiB"),
150+
_ => (memory_str, "MiB"), // No suffix, assume megabytes
151+
};
152+
153+
let number: f64 = number_str
154+
.parse()
155+
.context("Invalid number in memory specification")?;
156+
157+
// Use libvirt helper to get bytes per unit
158+
let bytes_per_unit =
159+
crate::libvirt::unit_to_bytes(unit).ok_or_else(|| eyre!("Unknown unit: {}", unit))? as f64;
160+
161+
let mib = 1024.0 * 1024.0;
162+
let total_mb = (number * bytes_per_unit) / mib;
163+
164+
Ok(total_mb as u32)
175165
}

0 commit comments

Comments
 (0)