|
| 1 | +// SPDX-FileCopyrightText: Copyright © 2025 Serpent OS Developers |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: MPL-2.0 |
| 4 | + |
| 5 | +use std::{fmt, str::FromStr}; |
| 6 | + |
| 7 | +use crate::kdl_value_to_string; |
| 8 | + |
| 9 | +use super::FromKdlProperty; |
| 10 | + |
| 11 | +/// The type of partition table to create |
| 12 | +#[derive(Debug, PartialEq)] |
| 13 | +pub enum PartitionTableType { |
| 14 | + /// GUID Partition Table |
| 15 | + Gpt, |
| 16 | + |
| 17 | + /// Master Boot Record |
| 18 | + Msdos, |
| 19 | +} |
| 20 | + |
| 21 | +impl fmt::Display for PartitionTableType { |
| 22 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 23 | + match self { |
| 24 | + Self::Gpt => f.write_str("gpt"), |
| 25 | + Self::Msdos => f.write_str("msdos"), |
| 26 | + } |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +impl FromStr for PartitionTableType { |
| 31 | + type Err = crate::Error; |
| 32 | + |
| 33 | + /// Attempt to convert a string to a partition table type |
| 34 | + fn from_str(value: &str) -> Result<Self, Self::Err> { |
| 35 | + match value { |
| 36 | + "gpt" => Ok(Self::Gpt), |
| 37 | + "msdos" => Ok(Self::Msdos), |
| 38 | + _ => Err(crate::Error::UnknownVariant), |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +impl FromKdlProperty<'_> for PartitionTableType { |
| 44 | + fn from_kdl_property(entry: &kdl::KdlEntry) -> Result<Self, crate::Error> { |
| 45 | + let value = kdl_value_to_string(entry)?; |
| 46 | + let v = value.parse().map_err(|_| crate::UnsupportedValue { |
| 47 | + at: entry.span(), |
| 48 | + advice: Some("'gpt' and 'mbr' are supported".into()), |
| 49 | + })?; |
| 50 | + Ok(v) |
| 51 | + } |
| 52 | +} |
0 commit comments