xref: /DADK/dadk-config/src/rootfs/partition.rs (revision 7a97f354ff649be3f7ed5e2b7f96d45e87c19500)
1 use serde::Deserialize;
2 
3 #[derive(Debug, Clone, PartialEq, Eq, Deserialize, Default)]
4 pub struct PartitionConfig {
5     #[serde(rename = "type")]
6     pub partition_type: PartitionType,
7 }
8 
9 #[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Default)]
10 pub enum PartitionType {
11     /// Disk image is not partitioned
12     #[default]
13     #[serde(rename = "none")]
14     None,
15     /// Use MBR partition table
16     #[serde(rename = "mbr")]
17     Mbr,
18     /// Use GPT partition table
19     #[serde(rename = "gpt")]
20     Gpt,
21 }
22 
23 impl PartitionConfig {
24     /// Determines whether the disk image should be partitioned
25     ///
26     /// Returns `true` if the partition type is not `None`, otherwise returns `false`.
image_should_be_partitioned(&self) -> bool27     pub fn image_should_be_partitioned(&self) -> bool {
28         self.partition_type != PartitionType::None
29     }
30 }
31 
32 #[cfg(test)]
33 mod tests {
34     use super::*;
35     #[test]
test_parse_partition_type()36     fn test_parse_partition_type() {
37         let test_cases = vec![
38             (r#"type = "none""#, PartitionType::None),
39             (r#"type = "mbr""#, PartitionType::Mbr),
40             (r#"type = "gpt""#, PartitionType::Gpt),
41         ];
42 
43         for (config_content, expected_type) in test_cases {
44             let partition_config: PartitionConfig = toml::from_str(config_content).unwrap();
45             assert_eq!(partition_config.partition_type, expected_type);
46         }
47     }
48 }
49