|
12 | 12 | //! - Track and undo changes |
13 | 13 | //! - Validate that changes won't conflict with existing partitions |
14 | 14 |
|
15 | | -use disks::BlockDevice; |
| 15 | +use disks::{align_down, align_up, format_position, format_size, is_aligned, BlockDevice}; |
16 | 16 | use log::{debug, warn}; |
17 | 17 | use std::collections::VecDeque; |
18 | 18 | use thiserror::Error; |
@@ -136,75 +136,6 @@ impl Region { |
136 | 136 | } |
137 | 137 | } |
138 | 138 |
|
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 | | - |
208 | 139 | impl Change { |
209 | 140 | /// Get a human readable description of this change |
210 | 141 | pub fn describe(&self, disk_size: u64) -> String { |
|
0 commit comments