Skip to content

Commit a2d3ec5

Browse files
committed
disks: Absorb size formatting/manip APIs
Signed-off-by: Ikey Doherty <ikey@aerynos.com>
1 parent 5a7a7d1 commit a2d3ec5

File tree

5 files changed

+80
-73
lines changed

5 files changed

+80
-73
lines changed

crates/disks/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
// SPDX-License-Identifier: MPL-2.0
44

55
mod disk;
6+
mod sizing;
7+
pub use sizing::*;
8+
69
use std::{
710
fs, io,
811
path::{Path, PathBuf},

crates/disks/src/sizing.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// SPDX-FileCopyrightText: Copyright © 2025 Serpent OS Developers
2+
// SPDX-FileCopyrightText: Copyright © 2025 AerynOS Developers
3+
//
4+
// SPDX-License-Identifier: MPL-2.0
5+
6+
/// Format a size in bytes into a human readable string
7+
/// Format a byte size into a human-readable string with appropriate units
8+
///
9+
/// # Examples
10+
///
11+
/// ```
12+
/// use disks::format_size;
13+
/// assert_eq!(format_size(1500), "1.5KiB");
14+
/// assert_eq!(format_size(1500000), "1.4MiB");
15+
/// ```
16+
pub fn format_size(size: u64) -> String {
17+
const KB: f64 = 1024.0;
18+
const MB: f64 = KB * 1024.0;
19+
const GB: f64 = MB * 1024.0;
20+
const TB: f64 = GB * 1024.0;
21+
22+
let size = size as f64;
23+
if size >= TB {
24+
format!("{:.1}TiB", size / TB)
25+
} else if size >= GB {
26+
format!("{:.1}GiB", size / GB)
27+
} else if size >= MB {
28+
format!("{:.1}MiB", size / MB)
29+
} else if size >= KB {
30+
format!("{:.1}KiB", size / KB)
31+
} else {
32+
format!("{}B", size)
33+
}
34+
}
35+
36+
/// Format a disk position as a percentage and absolute size
37+
/// Format a disk position as both a percentage and absolute size
38+
///
39+
/// This is useful for displaying partition locations in a user-friendly way.
40+
///
41+
/// # Examples
42+
///
43+
/// ```
44+
/// use disks::format_position;
45+
/// let total = 1000;
46+
/// assert_eq!(format_position(500, total), "50% (500B)");
47+
/// ```
48+
pub fn format_position(pos: u64, total: u64) -> String {
49+
format!("{}% ({})", (pos as f64 / total as f64 * 100.0) as u64, format_size(pos))
50+
}
51+
52+
/// Check if a value is already aligned to the given boundary
53+
pub fn is_aligned(value: u64, alignment: u64) -> bool {
54+
value % alignment == 0
55+
}
56+
57+
/// Align up to the nearest multiple of alignment, unless already aligned
58+
pub fn align_up(value: u64, alignment: u64) -> u64 {
59+
match value % alignment {
60+
0 => value,
61+
remainder if remainder > (alignment / 2) => value + (alignment - remainder),
62+
remainder => value - remainder,
63+
}
64+
}
65+
66+
/// Align down to the nearest multiple of alignment, unless already aligned
67+
pub fn align_down(value: u64, alignment: u64) -> u64 {
68+
match value % alignment {
69+
0 => value,
70+
remainder if remainder < (alignment / 2) => value - remainder,
71+
remainder => value + (alignment - remainder),
72+
}
73+
}

crates/partitioning/src/planner.rs

Lines changed: 1 addition & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//! - Track and undo changes
1313
//! - Validate that changes won't conflict with existing partitions
1414
15-
use disks::BlockDevice;
15+
use disks::{align_down, align_up, format_position, format_size, is_aligned, BlockDevice};
1616
use log::{debug, warn};
1717
use std::collections::VecDeque;
1818
use thiserror::Error;
@@ -136,75 +136,6 @@ impl Region {
136136
}
137137
}
138138

139-
/// Format a size in bytes into a human readable string
140-
/// Format a byte size into a human-readable string with appropriate units
141-
///
142-
/// # Examples
143-
///
144-
/// ```
145-
/// use partitioning::planner::format_size;
146-
/// assert_eq!(format_size(1500), "1.5KiB");
147-
/// assert_eq!(format_size(1500000), "1.4MiB");
148-
/// ```
149-
pub fn format_size(size: u64) -> String {
150-
const KB: f64 = 1024.0;
151-
const MB: f64 = KB * 1024.0;
152-
const GB: f64 = MB * 1024.0;
153-
const TB: f64 = GB * 1024.0;
154-
155-
let size = size as f64;
156-
if size >= TB {
157-
format!("{:.1}TiB", size / TB)
158-
} else if size >= GB {
159-
format!("{:.1}GiB", size / GB)
160-
} else if size >= MB {
161-
format!("{:.1}MiB", size / MB)
162-
} else if size >= KB {
163-
format!("{:.1}KiB", size / KB)
164-
} else {
165-
format!("{}B", size)
166-
}
167-
}
168-
169-
/// Format a disk position as a percentage and absolute size
170-
/// Format a disk position as both a percentage and absolute size
171-
///
172-
/// This is useful for displaying partition locations in a user-friendly way.
173-
///
174-
/// # Examples
175-
///
176-
/// ```
177-
/// use partitioning::planner::format_position;
178-
/// let total = 1000;
179-
/// assert_eq!(format_position(500, total), "50% (500B)");
180-
/// ```
181-
pub fn format_position(pos: u64, total: u64) -> String {
182-
format!("{}% ({})", (pos as f64 / total as f64 * 100.0) as u64, format_size(pos))
183-
}
184-
185-
/// Check if a value is already aligned to the given boundary
186-
fn is_aligned(value: u64, alignment: u64) -> bool {
187-
value % alignment == 0
188-
}
189-
190-
/// Align up to the nearest multiple of alignment, unless already aligned
191-
fn align_up(value: u64, alignment: u64) -> u64 {
192-
match value % alignment {
193-
0 => value,
194-
remainder if remainder > (alignment / 2) => value + (alignment - remainder),
195-
remainder => value - remainder,
196-
}
197-
}
198-
199-
/// Align down to the nearest multiple of alignment, unless already aligned
200-
fn align_down(value: u64, alignment: u64) -> u64 {
201-
match value % alignment {
202-
0 => value,
203-
remainder if remainder < (alignment / 2) => value - remainder,
204-
remainder => value + (alignment - remainder),
205-
}
206-
}
207-
208139
impl Change {
209140
/// Get a human readable description of this change
210141
pub fn describe(&self, disk_size: u64) -> String {

crates/partitioning/src/strategy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ impl Strategy {
115115

116116
/// Get a human readable description of this strategy
117117
pub fn describe(&self) -> String {
118-
use crate::planner::format_size;
118+
use disks::format_size;
119119

120120
let mut desc = match &self.allocation {
121121
AllocationStrategy::InitializeWholeDisk => "Initialize new partition layout on entire disk".to_string(),

crates/provisioning/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ impl Parser {
5656
src: NamedSource::new(&name, Arc::new("".to_string())),
5757
diagnostics: vec![e.into()],
5858
})?;
59-
Self::new(name.to_string(), txt)
59+
Self::new(&name, &txt)
6060
}
6161

6262
/// Create a new parser from a string
63-
pub fn new(name: String, contents: String) -> Result<Self, ParseError> {
63+
pub fn new(name: &str, contents: &str) -> Result<Self, ParseError> {
6464
let source = Arc::new(contents.to_string());
6565
let ns = NamedSource::new(name, source).with_language("KDL");
6666
let mut errors = vec![];

0 commit comments

Comments
 (0)