@@ -16,7 +16,7 @@ pub(crate) const GRUB_BIN: &str = "usr/sbin/grub2-install";
1616#[ derive( Serialize , Deserialize , Debug ) ]
1717struct BlockDevice {
1818 path : String ,
19- pttype : String ,
19+ pttype : Option < String > ,
2020 parttypename : Option < String > ,
2121}
2222
@@ -113,12 +113,14 @@ impl Bios {
113113
114114 let output = String :: from_utf8 ( output. stdout ) ?;
115115 // Parse the JSON string into the `Devices` struct
116- let devices: Devices = serde_json:: from_str ( & output) . expect ( "JSON was not well-formatted" ) ;
116+ let Ok ( devices) = serde_json:: from_str :: < Devices > ( & output) else {
117+ bail ! ( "Could not deserialize JSON output from lsblk" ) ;
118+ } ;
117119
118120 // Find the device with the parttypename "BIOS boot"
119121 for device in devices. blockdevices {
120122 if let Some ( parttypename) = & device. parttypename {
121- if parttypename == "BIOS boot" && device. pttype == "gpt" {
123+ if parttypename == "BIOS boot" && device. pttype . as_deref ( ) == Some ( "gpt" ) {
122124 return Ok ( Some ( device. path ) ) ;
123125 }
124126 }
@@ -213,3 +215,18 @@ impl Component for Bios {
213215 Ok ( None )
214216 }
215217}
218+
219+ #[ cfg( test) ]
220+ mod tests {
221+ use super :: * ;
222+
223+ #[ test]
224+ fn test_deserialize_lsblk_output ( ) {
225+ let data = include_str ! ( "../tests/fixtures/example-lsblk-output.json" ) ;
226+ let devices: Devices = serde_json:: from_str ( & data) . expect ( "JSON was not well-formatted" ) ;
227+ assert_eq ! ( devices. blockdevices. len( ) , 7 ) ;
228+ assert_eq ! ( devices. blockdevices[ 0 ] . path, "/dev/sr0" ) ;
229+ assert ! ( devices. blockdevices[ 0 ] . pttype. is_none( ) ) ;
230+ assert ! ( devices. blockdevices[ 0 ] . parttypename. is_none( ) ) ;
231+ }
232+ }
0 commit comments