blockdev 0.3.1

A Rust library for parsing and working with lsblk JSON output, providing type-safe block device representation and utilities for Linux
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
use serde::de::Error as DeError;
use serde::{Deserialize, Deserializer, Serialize};
use serde_json::Value;
use std::process::Command;
use std::slice::Iter;
use std::string::FromUtf8Error;
use std::vec::IntoIter;
use thiserror::Error;

/// Represents the major and minor device numbers.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MajMin {
    /// The major device number.
    pub major: u32,
    /// The minor device number.
    pub minor: u32,
}

impl Serialize for MajMin {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(&format!("{}:{}", self.major, self.minor))
    }
}

impl<'de> Deserialize<'de> for MajMin {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        let parts: Vec<&str> = s.split(':').collect();
        if parts.len() != 2 {
            return Err(DeError::custom(format!(
                "invalid maj:min format: expected 'major:minor', got '{s}'"
            )));
        }
        let major = parts[0]
            .parse()
            .map_err(|_| DeError::custom(format!("invalid major number: {}", parts[0])))?;
        let minor = parts[1]
            .parse()
            .map_err(|_| DeError::custom(format!("invalid minor number: {}", parts[1])))?;
        Ok(MajMin { major, minor })
    }
}

impl std::fmt::Display for MajMin {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}:{}", self.major, self.minor)
    }
}

/// Represents the type of a block device.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash)]
#[serde(rename_all = "lowercase")]
pub enum DeviceType {
    /// A physical disk device.
    Disk,
    /// A partition on a disk.
    Part,
    /// A loop device.
    Loop,
    /// A RAID1 (mirroring) device.
    Raid1,
    /// A RAID5 device.
    Raid5,
    /// A RAID6 device.
    Raid6,
    /// A RAID0 (striping) device.
    Raid0,
    /// A RAID10 device.
    Raid10,
    /// An LVM logical volume.
    Lvm,
    /// A device mapper crypt device.
    Crypt,
    /// A ROM device (e.g., CD/DVD drive).
    Rom,
    /// An unknown or unsupported device type.
    #[serde(other)]
    Other,
}

/// Error type for blockdev operations.
#[derive(Debug, Error)]
pub enum BlockDevError {
    /// The lsblk command failed to execute.
    #[error("failed to execute lsblk: {0}")]
    CommandFailed(#[from] std::io::Error),

    /// The lsblk command returned a non-zero exit status.
    #[error("lsblk returned error: {0}")]
    LsblkError(String),

    /// The output from lsblk was not valid UTF-8.
    #[error("invalid UTF-8 in lsblk output: {0}")]
    InvalidUtf8(#[from] FromUtf8Error),

    /// Failed to parse the JSON output from lsblk.
    #[error("failed to parse lsblk JSON: {0}")]
    JsonParse(#[from] serde_json::Error),
}

/// Represents the entire JSON output produced by `lsblk --json`.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Default)]
pub struct BlockDevices {
    /// A vector of block devices.
    pub blockdevices: Vec<BlockDevice>,
}

/// Parses a human-readable size string (e.g., "500G", "3.5T") into bytes.
fn parse_size_string(s: &str) -> Option<u64> {
    let s = s.trim();
    if s.is_empty() {
        return None;
    }

    // Find where the numeric part ends and the suffix begins
    let (num_part, suffix) = {
        let idx = s
            .find(|c: char| !c.is_ascii_digit() && c != '.')
            .unwrap_or(s.len());
        (&s[..idx], s[idx..].trim())
    };

    let num: f64 = num_part.parse().ok()?;
    let multiplier: u64 = match suffix.to_uppercase().as_str() {
        "" | "B" => 1,
        "K" | "KB" | "KIB" => 1024,
        "M" | "MB" | "MIB" => 1024 * 1024,
        "G" | "GB" | "GIB" => 1024 * 1024 * 1024,
        "T" | "TB" | "TIB" => 1024 * 1024 * 1024 * 1024,
        "P" | "PB" | "PIB" => 1024 * 1024 * 1024 * 1024 * 1024,
        _ => return None,
    };

    Some((num * multiplier as f64) as u64)
}

/// Custom deserializer that handles both numeric byte values and human-readable size strings.
fn deserialize_size<'de, D>(deserializer: D) -> Result<u64, D::Error>
where
    D: Deserializer<'de>,
{
    let value = Value::deserialize(deserializer)?;
    match &value {
        Value::Number(n) => n
            .as_u64()
            .or_else(|| n.as_f64().map(|f| f as u64))
            .ok_or_else(|| DeError::custom("invalid numeric size")),
        Value::String(s) => {
            parse_size_string(s).ok_or_else(|| DeError::custom(format!("invalid size string: {s}")))
        }
        _ => Err(DeError::custom("size must be a number or string")),
    }
}

/// Custom deserializer that supports both a single mountpoint (which may be null)
/// and an array of mountpoints.
///
/// # Arguments
///
/// * `deserializer` - The deserializer instance.
///
/// # Returns
///
/// A vector of optional strings representing mountpoints.
///
/// # Errors
///
/// Returns an error if the value cannot be deserialized either as a single value or as an array.
///
/// This function is used internally by Serde when deserializing block devices.
/// For example, if the JSON value is `null`, it will be converted to `vec![None]`.
fn deserialize_mountpoints<'de, D>(deserializer: D) -> Result<Vec<Option<String>>, D::Error>
where
    D: Deserializer<'de>,
{
    let value = Value::deserialize(deserializer)?;
    if value.is_array() {
        // Deserialize as an array of optional strings.
        serde_json::from_value(value).map_err(DeError::custom)
    } else {
        // Otherwise, deserialize as a single Option<String> and wrap it in a vector.
        let single: Option<String> = serde_json::from_value(value).map_err(DeError::custom)?;
        Ok(vec![single])
    }
}

/// Represents a block device as output by `lsblk`.
///
/// Note that the `children` field is optional, as some devices might not have any nested children.
///
/// # Field Details
///
/// - `name`: The device name.
/// - `maj_min`: The device's major and minor numbers. (Renamed from the JSON field "maj:min")
/// - `rm`: Whether the device is removable.
/// - `size`: The device size.
/// - `ro`: Whether the device is read-only.
/// - `device_type`: The device type (renamed from the reserved keyword "type").
/// - `mountpoints`: A vector of mountpoints for the device. Uses a custom deserializer to support both single and multiple mountpoints.
/// - `children`: Optional nested block devices.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct BlockDevice {
    /// The name of the block device.
    pub name: String,
    /// The major and minor numbers of the block device.
    ///
    /// This field corresponds to the JSON field `"maj:min"`.
    #[serde(rename = "maj:min")]
    pub maj_min: MajMin,
    /// Indicates if the device is removable.
    pub rm: bool,
    /// The size of the block device in bytes.
    #[serde(deserialize_with = "deserialize_size")]
    pub size: u64,
    /// Indicates if the device is read-only.
    pub ro: bool,
    /// The type of the block device.
    ///
    /// The JSON field is `"type"`, which is a reserved keyword in Rust. It is renamed to `device_type`.
    #[serde(rename = "type")]
    pub device_type: DeviceType,
    /// The mountpoints of the device.
    ///
    /// Uses a custom deserializer to handle both a single mountpoint (possibly null) and an array of mountpoints.
    #[serde(
        default,
        alias = "mountpoint",
        deserialize_with = "deserialize_mountpoints"
    )]
    pub mountpoints: Vec<Option<String>>,
    /// Optional nested children block devices.
    #[serde(default)]
    pub children: Option<Vec<BlockDevice>>,
}

impl BlockDevice {
    /// Returns `true` if this device has any children.
    #[must_use]
    pub fn has_children(&self) -> bool {
        self.children.as_ref().is_some_and(|c| !c.is_empty())
    }

    /// Returns an iterator over the children of this device.
    ///
    /// Returns an empty iterator if the device has no children.
    pub fn children_iter(&self) -> impl Iterator<Item = &BlockDevice> {
        self.children.iter().flat_map(|c| c.iter())
    }

    /// Finds a direct child device by name.
    ///
    /// Returns `None` if no child with the given name exists.
    #[must_use]
    pub fn find_child(&self, name: &str) -> Option<&BlockDevice> {
        self.children.as_ref()?.iter().find(|c| c.name == name)
    }

    /// Returns all non-null mountpoints for this device.
    #[must_use]
    pub fn active_mountpoints(&self) -> Vec<&str> {
        self.mountpoints
            .iter()
            .filter_map(|m| m.as_deref())
            .collect()
    }

    /// Returns `true` if this device has at least one mountpoint.
    #[must_use]
    pub fn is_mounted(&self) -> bool {
        self.mountpoints.iter().any(|m| m.is_some())
    }

    /// Determines if this block device or any of its recursive children has a mountpoint of `/`,
    /// indicating a system mount.
    #[must_use]
    pub fn is_system(&self) -> bool {
        if self.mountpoints.iter().any(|m| m.as_deref() == Some("/")) {
            return true;
        }
        if let Some(children) = &self.children {
            for child in children {
                if child.is_system() {
                    return true;
                }
            }
        }
        false
    }

    /// Returns `true` if this device is a disk.
    #[must_use]
    pub fn is_disk(&self) -> bool {
        self.device_type == DeviceType::Disk
    }

    /// Returns `true` if this device is a partition.
    #[must_use]
    pub fn is_partition(&self) -> bool {
        self.device_type == DeviceType::Part
    }
}

impl BlockDevices {
    /// Returns the number of top-level block devices.
    #[must_use]
    pub fn len(&self) -> usize {
        self.blockdevices.len()
    }

    /// Returns `true` if there are no block devices.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.blockdevices.is_empty()
    }

    /// Returns an iterator over references to the block devices.
    pub fn iter(&self) -> Iter<'_, BlockDevice> {
        self.blockdevices.iter()
    }

    /// Returns a vector of references to `BlockDevice` entries that have a mountpoint
    /// of `/` on them or on any of their recursive children.
    #[must_use]
    pub fn system(&self) -> Vec<&BlockDevice> {
        self.blockdevices
            .iter()
            .filter(|device| device.is_system())
            .collect()
    }

    /// Returns a vector of references to `BlockDevice` entries that do not have a mountpoint
    /// of `/` on them or on any of their recursive children.
    #[must_use]
    pub fn non_system(&self) -> Vec<&BlockDevice> {
        self.blockdevices
            .iter()
            .filter(|device| !device.is_system())
            .collect()
    }

    /// Finds a top-level block device by name.
    ///
    /// Returns `None` if no device with the given name exists.
    #[must_use]
    pub fn find_by_name(&self, name: &str) -> Option<&BlockDevice> {
        self.blockdevices.iter().find(|d| d.name == name)
    }
}

impl IntoIterator for BlockDevices {
    type Item = BlockDevice;
    type IntoIter = IntoIter<BlockDevice>;

    fn into_iter(self) -> Self::IntoIter {
        self.blockdevices.into_iter()
    }
}

impl<'a> IntoIterator for &'a BlockDevices {
    type Item = &'a BlockDevice;
    type IntoIter = Iter<'a, BlockDevice>;

    fn into_iter(self) -> Self::IntoIter {
        self.blockdevices.iter()
    }
}

/// Parses a JSON string (produced by `lsblk --json`)
/// into a `BlockDevices` struct.
///
/// This function is useful when you already have JSON data from `lsblk`
/// and want to parse it without running the command again.
///
/// # Arguments
///
/// * `json_data` - A string slice containing the JSON data.
///
/// # Errors
///
/// Returns a `serde_json::Error` if the JSON cannot be parsed.
///
/// # Examples
///
/// ```
/// use blockdev::parse_lsblk;
///
/// let json = r#"{"blockdevices": [{"name": "sda", "maj:min": "8:0", "rm": false, "size": "500G", "ro": false, "type": "disk", "mountpoints": [null]}]}"#;
/// let devices = parse_lsblk(json).expect("Failed to parse JSON");
/// assert_eq!(devices.len(), 1);
/// ```
pub fn parse_lsblk(json_data: &str) -> Result<BlockDevices, serde_json::Error> {
    serde_json::from_str(json_data)
}

/// Runs the `lsblk --json` command, captures its output, and parses it
/// into a `BlockDevices` struct. If the command fails or the output cannot be parsed,
/// an error is returned.
///
/// # Errors
///
/// Returns an error if the `lsblk` command fails or if the output cannot be parsed as valid JSON.
///
/// # Examples
///
/// ```no_run
/// # use blockdev::get_devices;
/// let devices = get_devices().expect("Failed to get block devices");
/// ```
pub fn get_devices() -> Result<BlockDevices, BlockDevError> {
    let output = Command::new("lsblk")
        .arg("--json")
        .arg("--bytes")
        .output()?;

    if !output.status.success() {
        return Err(BlockDevError::LsblkError(
            String::from_utf8_lossy(&output.stderr).into_owned(),
        ));
    }

    let json_output = String::from_utf8(output.stdout)?;
    let lsblk = parse_lsblk(&json_output)?;
    Ok(lsblk)
}

#[cfg(test)]
mod tests {
    use super::*;

    const SAMPLE_JSON: &str = r#"
    {
        "blockdevices": [
            {"name":"nvme1n1", "maj:min":"259:0", "rm":false, "size":"3.5T", "ro":false, "type":"disk", "mountpoint":null,
                "children": [
                    {"name":"nvme1n1p1", "maj:min":"259:1", "rm":false, "size":"3.5T", "ro":false, "type":"part", "mountpoint":null},
                    {"name":"nvme1n1p9", "maj:min":"259:2", "rm":false, "size":"8M", "ro":false, "type":"part", "mountpoint":null}
                ]
            },
            {"name":"nvme7n1", "maj:min":"259:3", "rm":false, "size":"3.5T", "ro":false, "type":"disk", "mountpoint":null,
                "children": [
                    {"name":"nvme7n1p1", "maj:min":"259:7", "rm":false, "size":"3.5T", "ro":false, "type":"part", "mountpoint":null},
                    {"name":"nvme7n1p9", "maj:min":"259:8", "rm":false, "size":"8M", "ro":false, "type":"part", "mountpoint":null}
                ]
            },
            {"name":"nvme5n1", "maj:min":"259:4", "rm":false, "size":"3.5T", "ro":false, "type":"disk", "mountpoint":null,
                "children": [
                    {"name":"nvme5n1p1", "maj:min":"259:5", "rm":false, "size":"3.5T", "ro":false, "type":"part", "mountpoint":null},
                    {"name":"nvme5n1p9", "maj:min":"259:6", "rm":false, "size":"8M", "ro":false, "type":"part", "mountpoint":null}
                ]
            },
            {"name":"nvme9n1", "maj:min":"259:9", "rm":false, "size":"3.5T", "ro":false, "type":"disk", "mountpoint":null,
                "children": [
                    {"name":"nvme9n1p1", "maj:min":"259:13", "rm":false, "size":"3.5T", "ro":false, "type":"part", "mountpoint":null},
                    {"name":"nvme9n1p9", "maj:min":"259:14", "rm":false, "size":"8M", "ro":false, "type":"part", "mountpoint":null}
                ]
            },
            {"name":"nvme4n1", "maj:min":"259:10", "rm":false, "size":"3.5T", "ro":false, "type":"disk", "mountpoint":null,
                "children": [
                    {"name":"nvme4n1p1", "maj:min":"259:11", "rm":false, "size":"3.5T", "ro":false, "type":"part", "mountpoint":null},
                    {"name":"nvme4n1p9", "maj:min":"259:12", "rm":false, "size":"8M", "ro":false, "type":"part", "mountpoint":null}
                ]
            },
            {"name":"nvme8n1", "maj:min":"259:15", "rm":false, "size":"3.5T", "ro":false, "type":"disk", "mountpoint":null,
                "children": [
                    {"name":"nvme8n1p1", "maj:min":"259:20", "rm":false, "size":"3.5T", "ro":false, "type":"part", "mountpoint":null},
                    {"name":"nvme8n1p9", "maj:min":"259:21", "rm":false, "size":"8M", "ro":false, "type":"part", "mountpoint":null}
                ]
            },
            {"name":"nvme6n1", "maj:min":"259:16", "rm":false, "size":"3.5T", "ro":false, "type":"disk", "mountpoint":null,
                "children": [
                    {"name":"nvme6n1p1", "maj:min":"259:17", "rm":false, "size":"3.5T", "ro":false, "type":"part", "mountpoint":null},
                    {"name":"nvme6n1p9", "maj:min":"259:18", "rm":false, "size":"8M", "ro":false, "type":"part", "mountpoint":null}
                ]
            },
            {"name":"nvme3n1", "maj:min":"259:19", "rm":false, "size":"894.3G", "ro":false, "type":"disk", "mountpoint":null,
                "children": [
                    {"name":"nvme3n1p1", "maj:min":"259:23", "rm":false, "size":"1M", "ro":false, "type":"part", "mountpoint":null},
                    {"name":"nvme3n1p2", "maj:min":"259:24", "rm":false, "size":"244M", "ro":false, "type":"part", "mountpoint":"/boot/efi"},
                    {"name":"nvme3n1p3", "maj:min":"259:25", "rm":false, "size":"488M", "ro":false, "type":"part", "mountpoint":null,
                    "children": [
                        {"name":"md0", "maj:min":"9:0", "rm":false, "size":"487M", "ro":false, "type":"raid1", "mountpoint":"/boot"}
                    ]
                    },
                    {"name":"nvme3n1p4", "maj:min":"259:26", "rm":false, "size":"7.6G", "ro":false, "type":"part", "mountpoint":null,
                    "children": [
                        {"name":"md1", "maj:min":"9:1", "rm":false, "size":"7.6G", "ro":false, "type":"raid1", "mountpoint":"[SWAP]"}
                    ]
                    },
                    {"name":"nvme3n1p5", "maj:min":"259:27", "rm":false, "size":"19.1G", "ro":false, "type":"part", "mountpoint":null,
                    "children": [
                        {"name":"md2", "maj:min":"9:2", "rm":false, "size":"19.1G", "ro":false, "type":"raid1", "mountpoint":"/"}
                    ]
                    },
                    {"name":"nvme3n1p6", "maj:min":"259:28", "rm":false, "size":"866.8G", "ro":false, "type":"part", "mountpoint":null}
                ]
            },
            {"name":"nvme0n1", "maj:min":"259:22", "rm":false, "size":"3.5T", "ro":false, "type":"disk", "mountpoint":null,
                "children": [
                    {"name":"nvme0n1p1", "maj:min":"259:29", "rm":false, "size":"3.5T", "ro":false, "type":"part", "mountpoint":null},
                    {"name":"nvme0n1p9", "maj:min":"259:30", "rm":false, "size":"8M", "ro":false, "type":"part", "mountpoint":null}
                ]
            },
            {"name":"nvme2n1", "maj:min":"259:31", "rm":false, "size":"894.3G", "ro":false, "type":"disk", "mountpoint":null,
                "children": [
                    {"name":"nvme2n1p1", "maj:min":"259:32", "rm":false, "size":"1M", "ro":false, "type":"part", "mountpoint":null},
                    {"name":"nvme2n1p2", "maj:min":"259:33", "rm":false, "size":"244M", "ro":false, "type":"part", "mountpoint":null},
                    {"name":"nvme2n1p3", "maj:min":"259:34", "rm":false, "size":"488M", "ro":false, "type":"part", "mountpoint":null,
                    "children": [
                        {"name":"md0", "maj:min":"9:0", "rm":false, "size":"487M", "ro":false, "type":"raid1", "mountpoint":"/boot"}
                    ]
                    },
                    {"name":"nvme2n1p4", "maj:min":"259:35", "rm":false, "size":"7.6G", "ro":false, "type":"part", "mountpoint":null,
                    "children": [
                        {"name":"md1", "maj:min":"9:1", "rm":false, "size":"7.6G", "ro":false, "type":"raid1", "mountpoint":"[SWAP]"}
                    ]
                    },
                    {"name":"nvme2n1p5", "maj:min":"259:36", "rm":false, "size":"19.1G", "ro":false, "type":"part", "mountpoint":null,
                    "children": [
                        {"name":"md2", "maj:min":"9:2", "rm":false, "size":"19.1G", "ro":false, "type":"raid1", "mountpoint":"/"}
                    ]
                    },
                    {"name":"nvme2n1p6", "maj:min":"259:37", "rm":false, "size":"866.8G", "ro":false, "type":"part", "mountpoint":null}
                ]
            }
        ]
    }
    "#;

    #[test]
    fn test_parse_lsblk() {
        let lsblk = parse_lsblk(SAMPLE_JSON).expect("Failed to parse JSON");

        // Assert the expected number of top-level block devices.
        assert_eq!(
            lsblk.blockdevices.len(),
            10,
            "Expected 10 top-level block devices"
        );

        // Verify that required fields are non-empty.
        for device in &lsblk.blockdevices {
            assert!(!device.name.is_empty(), "Device name should not be empty");
        }

        // Pick a device with nested children and validate details.
        let nvme3n1 = lsblk
            .blockdevices
            .iter()
            .find(|d| d.name == "nvme3n1")
            .expect("Expected to find device nvme3n1");

        // Its first mountpoint should be None.
        assert!(
            nvme3n1
                .mountpoints
                .first()
                .and_then(|opt| opt.as_deref())
                .is_none(),
            "nvme3n1 effective mountpoint should be None"
        );

        // Verify that nvme3n1 has exactly 6 children.
        let children = nvme3n1
            .children
            .as_ref()
            .expect("nvme3n1 should have children");
        assert_eq!(children.len(), 6, "nvme3n1 should have 6 children");

        // Validate that child nvme3n1p2 has first mountpoint of "/boot/efi".
        let nvme3n1p2 = children
            .iter()
            .find(|c| c.name == "nvme3n1p2")
            .expect("Expected to find nvme3n1p2");
        assert_eq!(
            nvme3n1p2.mountpoints.first().and_then(|opt| opt.as_deref()),
            Some("/boot/efi"),
            "nvme3n1p2 first mountpoint should be '/boot/efi'"
        );

        // In nvme3n1p3, verify that its nested child md0 has an effective mountpoint of "/boot".
        let nvme3n1p3 = children
            .iter()
            .find(|c| c.name == "nvme3n1p3")
            .expect("Expected to find nvme3n1p3");
        let nested_children = nvme3n1p3
            .children
            .as_ref()
            .expect("nvme3n1p3 should have children");
        let md0 = nested_children
            .iter()
            .find(|d| d.name == "md0")
            .expect("Expected to find md0 under nvme3n1p3");
        assert_eq!(
            md0.mountpoints.first().and_then(|opt| opt.as_deref()),
            Some("/boot"),
            "md0 effective mountpoint should be '/boot'"
        );

        // Test the non_system method.
        // Since nvme3n1 has a descendant (md2) with effective mountpoint "/" it should be excluded.
        let non_system = lsblk.non_system();
        assert_eq!(
            non_system.len(),
            8,
            "Expected 8 non-system top-level devices, since nvme3n1/nvme2n1 is system"
        );
        assert!(
            !non_system.iter().any(|d| d.name == "nvme3n1"),
            "nvme3n1 should be excluded from non-system devices"
        );
    }

    #[test]
    fn test_non_system() {
        // Create a JSON where one device is system (has "/" mountpoint in a child)
        // and one is non-system.
        let test_json = r#"
        {
            "blockdevices": [
                {
                    "name": "sda",
                    "maj:min": "8:0",
                    "rm": false,
                    "size": "447.1G",
                    "ro": false,
                    "type": "disk",
                    "mountpoints": [
                        null
                    ],
                    "children": [
                        {
                        "name": "sda1",
                        "maj:min": "8:1",
                        "rm": false,
                        "size": "512M",
                        "ro": false,
                        "type": "part",
                        "mountpoints": [
                            null
                        ]
                        },{
                        "name": "sda2",
                        "maj:min": "8:2",
                        "rm": false,
                        "size": "446.6G",
                        "ro": false,
                        "type": "part",
                        "mountpoints": [
                            null
                        ],
                        "children": [
                            {
                                "name": "md0",
                                "maj:min": "9:0",
                                "rm": false,
                                "size": "446.6G",
                                "ro": false,
                                "type": "raid1",
                                "mountpoints": [
                                    "/"
                                ]
                            }
                        ]
                        }
                    ]
                },{
                    "name": "sdb",
                    "maj:min": "8:16",
                    "rm": false,
                    "size": "447.1G",
                    "ro": false,
                    "type": "disk",
                    "mountpoints": [
                        null
                    ],
                    "children": [
                        {
                        "name": "sdb1",
                        "maj:min": "8:17",
                        "rm": false,
                        "size": "512M",
                        "ro": false,
                        "type": "part",
                        "mountpoints": [
                            "/boot/efi"
                        ]
                        },{
                        "name": "sdb2",
                        "maj:min": "8:18",
                        "rm": false,
                        "size": "446.6G",
                        "ro": false,
                        "type": "part",
                        "mountpoints": [
                            null
                        ],
                        "children": [
                            {
                                "name": "md0",
                                "maj:min": "9:0",
                                "rm": false,
                                "size": "446.6G",
                                "ro": false,
                                "type": "raid1",
                                "mountpoints": [
                                    "/"
                                ]
                            }
                        ]
                        }
                    ]
                },{
                    "name": "nvme0n1",
                    "maj:min": "259:2",
                    "rm": false,
                    "size": "1.7T",
                    "ro": false,
                    "type": "disk",
                    "mountpoints": [
                        null
                    ]
                },{
                    "name": "nvme1n1",
                    "maj:min": "259:3",
                    "rm": false,
                    "size": "1.7T",
                    "ro": false,
                    "type": "disk",
                    "mountpoints": [
                        null
                    ]
                }
            ]
        }
        "#;
        let disks = parse_lsblk(test_json).unwrap();
        let non_system = disks.non_system();
        assert_eq!(non_system.len(), 2);
        let names: Vec<&str> = non_system.iter().map(|d| d.name.as_str()).collect();
        assert_eq!(names, vec!["nvme0n1", "nvme1n1"]);
    }

    /// Warning: This test will attempt to run the `lsblk` command on your system.
    /// It may fail if `lsblk` is not available or if the test environment does not permit running commands.
    #[test]
    #[ignore = "requires lsblk command to be available on the system"]
    fn test_get_devices() {
        let dev = get_devices().expect("Failed to get block devices");
        // This assertion is simplistic; adjust according to your environment's expected output.
        assert!(!dev.blockdevices.is_empty());
    }
    #[test]
    fn test_into_iterator() {
        // Create dummy BlockDevice instances.
        let device1 = BlockDevice {
            name: "sda".to_string(),
            maj_min: MajMin { major: 8, minor: 0 },
            rm: false,
            size: 536_870_912_000, // 500G in bytes
            ro: false,
            device_type: DeviceType::Disk,
            mountpoints: vec![None],
            children: None,
        };

        let device2 = BlockDevice {
            name: "sdb".to_string(),
            maj_min: MajMin { major: 8, minor: 16 },
            rm: false,
            size: 536_870_912_000, // 500G in bytes
            ro: false,
            device_type: DeviceType::Disk,
            mountpoints: vec![None],
            children: None,
        };

        // Create a BlockDevices instance containing the two devices.
        let devices = BlockDevices {
            blockdevices: vec![device1, device2],
        };

        // Use the IntoIterator implementation to iterate over the devices.
        let names: Vec<String> = devices.into_iter().map(|dev| dev.name).collect();
        assert_eq!(names, vec!["sda".to_string(), "sdb".to_string()]);
    }

    #[test]
    fn test_empty_blockdevices() {
        let json = r#"{"blockdevices": []}"#;
        let devices = parse_lsblk(json).expect("Failed to parse empty JSON");
        assert!(devices.is_empty());
        assert_eq!(devices.len(), 0);
        assert!(devices.non_system().is_empty());
        assert!(devices.system().is_empty());
        assert!(devices.find_by_name("sda").is_none());
    }

    #[test]
    fn test_default_trait() {
        let devices = BlockDevices::default();
        assert!(devices.is_empty());
        assert_eq!(devices.len(), 0);
    }

    #[test]
    fn test_clone_trait() {
        let json = r#"{"blockdevices": [{"name": "sda", "maj:min": "8:0", "rm": false, "size": "500G", "ro": false, "type": "disk", "mountpoints": [null]}]}"#;
        let devices = parse_lsblk(json).expect("Failed to parse JSON");
        let cloned = devices.clone();
        assert_eq!(devices, cloned);
        assert_eq!(cloned.len(), 1);
    }

    #[test]
    fn test_serialization_roundtrip() {
        let json = r#"{"blockdevices":[{"name":"sda","maj:min":"8:0","rm":false,"size":"500G","ro":false,"type":"disk","mountpoints":[null],"children":null}]}"#;
        let devices = parse_lsblk(json).expect("Failed to parse JSON");
        let serialized = serde_json::to_string(&devices).expect("Failed to serialize");
        let deserialized: BlockDevices =
            serde_json::from_str(&serialized).expect("Failed to deserialize");
        assert_eq!(devices, deserialized);
    }

    #[test]
    fn test_device_with_direct_root_mount() {
        let json = r#"{
            "blockdevices": [{
                "name": "sda",
                "maj:min": "8:0",
                "rm": false,
                "size": "500G",
                "ro": false,
                "type": "disk",
                "mountpoints": ["/"]
            }]
        }"#;
        let devices = parse_lsblk(json).expect("Failed to parse JSON");
        let device = devices.find_by_name("sda").unwrap();
        assert!(device.is_system());
        assert!(device.is_mounted());
        assert_eq!(device.active_mountpoints(), vec!["/"]);
        assert_eq!(devices.system().len(), 1);
        assert!(devices.non_system().is_empty());
    }

    #[test]
    fn test_block_device_methods() {
        let device = BlockDevice {
            name: "sda".to_string(),
            maj_min: MajMin { major: 8, minor: 0 },
            rm: false,
            size: 536_870_912_000, // 500G in bytes
            ro: false,
            device_type: DeviceType::Disk,
            mountpoints: vec![Some("/mnt/data".to_string()), None],
            children: Some(vec![BlockDevice {
                name: "sda1".to_string(),
                maj_min: MajMin { major: 8, minor: 1 },
                rm: false,
                size: 268_435_456_000, // 250G in bytes
                ro: false,
                device_type: DeviceType::Part,
                mountpoints: vec![Some("/home".to_string())],
                children: None,
            }]),
        };

        assert!(device.is_disk());
        assert!(!device.is_partition());
        assert!(device.has_children());
        assert!(device.is_mounted());
        assert_eq!(device.active_mountpoints(), vec!["/mnt/data"]);

        let child = device.find_child("sda1").unwrap();
        assert!(!child.is_disk());
        assert!(child.is_partition());
        assert!(!child.has_children());

        assert!(device.find_child("nonexistent").is_none());
    }

    #[test]
    fn test_children_iter() {
        let device = BlockDevice {
            name: "sda".to_string(),
            maj_min: MajMin { major: 8, minor: 0 },
            rm: false,
            size: 536_870_912_000, // 500G in bytes
            ro: false,
            device_type: DeviceType::Disk,
            mountpoints: vec![None],
            children: Some(vec![
                BlockDevice {
                    name: "sda1".to_string(),
                    maj_min: MajMin { major: 8, minor: 1 },
                    rm: false,
                    size: 268_435_456_000, // 250G in bytes
                    ro: false,
                    device_type: DeviceType::Part,
                    mountpoints: vec![None],
                    children: None,
                },
                BlockDevice {
                    name: "sda2".to_string(),
                    maj_min: MajMin { major: 8, minor: 2 },
                    rm: false,
                    size: 268_435_456_000, // 250G in bytes
                    ro: false,
                    device_type: DeviceType::Part,
                    mountpoints: vec![None],
                    children: None,
                },
            ]),
        };

        let names: Vec<&str> = device.children_iter().map(|c| c.name.as_str()).collect();
        assert_eq!(names, vec!["sda1", "sda2"]);

        // Test empty children iterator
        let device_no_children = BlockDevice {
            name: "sdb".to_string(),
            maj_min: MajMin { major: 8, minor: 16 },
            rm: false,
            size: 536_870_912_000, // 500G in bytes
            ro: false,
            device_type: DeviceType::Disk,
            mountpoints: vec![None],
            children: None,
        };
        assert_eq!(device_no_children.children_iter().count(), 0);
    }

    #[test]
    fn test_borrowing_iterator() {
        let devices = BlockDevices {
            blockdevices: vec![
                BlockDevice {
                    name: "sda".to_string(),
                    maj_min: MajMin { major: 8, minor: 0 },
                    rm: false,
                    size: 536_870_912_000, // 500G in bytes
                    ro: false,
                    device_type: DeviceType::Disk,
                    mountpoints: vec![None],
                    children: None,
                },
                BlockDevice {
                    name: "sdb".to_string(),
                    maj_min: MajMin { major: 8, minor: 16 },
                    rm: false,
                    size: 536_870_912_000, // 500G in bytes
                    ro: false,
                    device_type: DeviceType::Disk,
                    mountpoints: vec![None],
                    children: None,
                },
            ],
        };

        // Test borrowing iterator (doesn't consume)
        let names: Vec<&str> = (&devices).into_iter().map(|d| d.name.as_str()).collect();
        assert_eq!(names, vec!["sda", "sdb"]);

        // devices is still available
        assert_eq!(devices.len(), 2);

        // Test iter() method
        let names2: Vec<&str> = devices.iter().map(|d| d.name.as_str()).collect();
        assert_eq!(names2, vec!["sda", "sdb"]);
    }

    #[test]
    fn test_find_by_name() {
        let devices = BlockDevices {
            blockdevices: vec![
                BlockDevice {
                    name: "sda".to_string(),
                    maj_min: MajMin { major: 8, minor: 0 },
                    rm: false,
                    size: 536_870_912_000, // 500G in bytes
                    ro: false,
                    device_type: DeviceType::Disk,
                    mountpoints: vec![None],
                    children: None,
                },
                BlockDevice {
                    name: "nvme0n1".to_string(),
                    maj_min: MajMin { major: 259, minor: 0 },
                    rm: false,
                    size: 1_099_511_627_776, // 1T in bytes
                    ro: false,
                    device_type: DeviceType::Disk,
                    mountpoints: vec![None],
                    children: None,
                },
            ],
        };

        assert!(devices.find_by_name("sda").is_some());
        assert_eq!(devices.find_by_name("sda").unwrap().size, 536_870_912_000);
        assert!(devices.find_by_name("nvme0n1").is_some());
        assert!(devices.find_by_name("nonexistent").is_none());
    }

    #[test]
    fn test_system_method() {
        let json = r#"{
            "blockdevices": [
                {"name": "sda", "maj:min": "8:0", "rm": false, "size": "500G", "ro": false, "type": "disk", "mountpoints": ["/"]},
                {"name": "sdb", "maj:min": "8:16", "rm": false, "size": "500G", "ro": false, "type": "disk", "mountpoints": [null]},
                {"name": "sdc", "maj:min": "8:32", "rm": false, "size": "500G", "ro": false, "type": "disk", "mountpoints": ["/home"]}
            ]
        }"#;
        let devices = parse_lsblk(json).expect("Failed to parse JSON");
        let system = devices.system();
        assert_eq!(system.len(), 1);
        assert_eq!(system[0].name, "sda");
    }

    #[test]
    fn test_multiple_mountpoints() {
        let json = r#"{
            "blockdevices": [{
                "name": "sda",
                "maj:min": "8:0",
                "rm": false,
                "size": "500G",
                "ro": false,
                "type": "disk",
                "mountpoints": ["/mnt/data", "/mnt/backup", null]
            }]
        }"#;
        let devices = parse_lsblk(json).expect("Failed to parse JSON");
        let device = devices.find_by_name("sda").unwrap();
        assert!(device.is_mounted());
        assert_eq!(
            device.active_mountpoints(),
            vec!["/mnt/data", "/mnt/backup"]
        );
    }

    #[test]
    fn test_removable_and_readonly() {
        let json = r#"{
            "blockdevices": [{
                "name": "sr0",
                "maj:min": "11:0",
                "rm": true,
                "size": "4.7G",
                "ro": true,
                "type": "rom",
                "mountpoints": [null]
            }]
        }"#;
        let devices = parse_lsblk(json).expect("Failed to parse JSON");
        let device = devices.find_by_name("sr0").unwrap();
        assert!(device.rm);
        assert!(device.ro);
        assert_eq!(device.device_type, DeviceType::Rom);
    }
}