1212//! - Track and undo changes
1313//! - Validate that changes won't conflict with existing partitions
1414
15- use disks:: Disk ;
15+ use disks:: BlockDevice ;
1616use log:: { debug, warn} ;
1717use std:: collections:: VecDeque ;
1818use thiserror:: Error ;
@@ -46,8 +46,8 @@ pub enum Change {
4646
4747/// A disk partitioning planner.
4848pub struct Planner {
49- /// The original disk state that we're planning changes for
50- disk : Disk ,
49+ /// The original block device state that we're planning changes for
50+ device : BlockDevice ,
5151 /// Stack of changes that can be undone
5252 changes : VecDeque < Change > ,
5353 /// Original partition layout for reference
@@ -190,12 +190,16 @@ impl Change {
190190
191191impl Planner {
192192 /// Creates a new partitioning planner for the given disk.
193- pub fn new ( disk : Disk ) -> Self {
194- debug ! ( "Creating new partition planner for disk of size {}" , disk. size( ) ) ;
195- // Extract original regions from disk
196- let original_regions = disk. partitions ( ) . iter ( ) . map ( |p| Region :: new ( p. start , p. end ) ) . collect ( ) ;
193+ pub fn new ( device : BlockDevice ) -> Self {
194+ debug ! ( "Creating new partition planner for device of size {}" , device. size( ) ) ;
195+ // Extract original regions from device
196+ let original_regions = device
197+ . partitions ( )
198+ . iter ( )
199+ . map ( |p| Region :: new ( p. start , p. end ) )
200+ . collect ( ) ;
197201 Self {
198- disk ,
202+ device ,
199203 changes : VecDeque :: new ( ) ,
200204 original_regions,
201205 }
@@ -207,7 +211,7 @@ impl Planner {
207211 return "No pending changes" . to_string ( ) ;
208212 }
209213
210- let disk_size = self . disk . size ( ) ;
214+ let disk_size = self . device . size ( ) ;
211215 let mut description = "Pending changes:\n " . to_string ( ) ;
212216
213217 for ( i, change) in self . changes . iter ( ) . enumerate ( ) {
@@ -269,7 +273,7 @@ impl Planner {
269273 debug ! ( "Aligned positions: {}..{}" , aligned_start, aligned_end) ;
270274
271275 // Validate bounds
272- if aligned_end > self . disk . size ( ) {
276+ if aligned_end > self . device . size ( ) {
273277 warn ! ( "Partition would exceed disk bounds" ) ;
274278 return Err ( PlanError :: RegionOutOfBounds {
275279 start : aligned_start,
@@ -318,7 +322,7 @@ impl Planner {
318322 warn ! ( "Invalid partition index {}" , index) ;
319323 return Err ( PlanError :: RegionOutOfBounds {
320324 start : 0 ,
321- end : self . disk . size ( ) ,
325+ end : self . device . size ( ) ,
322326 } ) ;
323327 }
324328
@@ -354,9 +358,9 @@ impl Planner {
354358 & self . changes
355359 }
356360
357- /// Get the original disk state
358- pub fn original_disk ( & self ) -> & Disk {
359- & self . disk
361+ /// Get the original block device state
362+ pub fn original_device ( & self ) -> & BlockDevice {
363+ & self . device
360364 }
361365 /// Plan to initialize a clean partition layout
362366 pub fn plan_initialize_disk ( & mut self ) -> Result < ( ) , PlanError > {
@@ -399,7 +403,7 @@ mod tests {
399403 #[ test]
400404 fn test_fresh_installation ( ) {
401405 let disk = create_mock_disk ( ) ;
402- let mut planner = Planner :: new ( Disk :: Mock ( disk) ) ;
406+ let mut planner = Planner :: new ( BlockDevice :: mock_device ( disk) ) ;
403407
404408 // Create typical Linux partition layout with absolute positions
405409 // - 0 -> 512MB: EFI System Partition
@@ -421,7 +425,7 @@ mod tests {
421425 #[ test]
422426 fn test_dual_boot_with_windows ( ) {
423427 let disk = create_windows_disk ( ) ;
424- let mut planner = Planner :: new ( Disk :: Mock ( disk) ) ;
428+ let mut planner = Planner :: new ( BlockDevice :: mock_device ( disk) ) ;
425429
426430 // Available space starts after Windows partitions (~ 200.6GB)
427431 let start = 200 * GB + 616 * MB ;
@@ -448,7 +452,7 @@ mod tests {
448452 disk. add_partition ( 512 * MB , 4 * GB + 512 * MB ) ; // Swap: 512MB -> 4.5GB
449453 disk. add_partition ( 4 * GB + 512 * MB , 500 * GB ) ; // Root: 4.5GB -> 500GB
450454
451- let mut planner = Planner :: new ( Disk :: Mock ( disk) ) ;
455+ let mut planner = Planner :: new ( BlockDevice :: mock_device ( disk) ) ;
452456
453457 // Delete old Linux partitions
454458 assert ! ( planner. plan_delete_partition( 1 ) . is_ok( ) ) ; // Delete swap
@@ -471,7 +475,7 @@ mod tests {
471475 #[ test]
472476 fn test_region_validation ( ) {
473477 let disk = create_mock_disk ( ) ;
474- let mut planner = Planner :: new ( Disk :: Mock ( disk) ) ;
478+ let mut planner = Planner :: new ( BlockDevice :: mock_device ( disk) ) ;
475479
476480 // Test out of bounds
477481 assert ! ( matches!(
@@ -490,7 +494,7 @@ mod tests {
490494 #[ test]
491495 fn test_undo_operations ( ) {
492496 let disk = create_mock_disk ( ) ;
493- let mut planner = Planner :: new ( Disk :: Mock ( disk) ) ;
497+ let mut planner = Planner :: new ( BlockDevice :: mock_device ( disk) ) ;
494498
495499 // Add some partitions
496500 assert ! ( planner. plan_add_partition( 0 , 100 * GB ) . is_ok( ) ) ;
@@ -512,7 +516,7 @@ mod tests {
512516 #[ test]
513517 fn test_partition_boundaries ( ) {
514518 let disk = create_mock_disk ( ) ;
515- let mut planner = Planner :: new ( Disk :: Mock ( disk) ) ;
519+ let mut planner = Planner :: new ( BlockDevice :: mock_device ( disk) ) ;
516520
517521 // Add first partition from 0 to 100GB
518522 assert ! ( planner. plan_add_partition( 0 , 100 * GB ) . is_ok( ) ) ;
0 commit comments