|
| 1 | +// SPDX-FileCopyrightText: Copyright © 2025 Serpent OS Developers |
| 2 | +// SPDX-FileCopyrightText: Copyright © 2025 AerynOS Developers |
| 3 | +// |
| 4 | +// SPDX-License-Identifier: MPL-2.0 |
| 5 | + |
| 6 | +use std::{fmt, str::FromStr}; |
| 7 | + |
| 8 | +pub use gpt::partition_types::Type as GptPartitionType; |
| 9 | +use partitioning::gpt; |
| 10 | +pub use uuid::Uuid; |
| 11 | + |
| 12 | +use crate::{get_kdl_entry, kdl_value_to_string, UnsupportedValue}; |
| 13 | + |
| 14 | +use super::FromKdlType; |
| 15 | + |
| 16 | +pub enum PartitionTypeKDL { |
| 17 | + GUID, |
| 18 | +} |
| 19 | + |
| 20 | +impl FromStr for PartitionTypeKDL { |
| 21 | + type Err = crate::Error; |
| 22 | + |
| 23 | + fn from_str(value: &str) -> Result<Self, Self::Err> { |
| 24 | + match value { |
| 25 | + "guid" => Ok(Self::GUID), |
| 26 | + _ => Err(crate::Error::UnknownVariant), |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +impl fmt::Display for PartitionTypeKDL { |
| 32 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 33 | + match self { |
| 34 | + Self::GUID => f.write_str("guid"), |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +impl<'a> FromKdlType<'a> for PartitionTypeKDL { |
| 40 | + fn from_kdl_type(id: &'a kdl::KdlEntry) -> Result<Self, crate::Error> { |
| 41 | + let ty_id = if let Some(ty) = id.ty() { |
| 42 | + ty.value().to_lowercase() |
| 43 | + } else { |
| 44 | + "".into() |
| 45 | + }; |
| 46 | + |
| 47 | + let v = ty_id.parse().map_err(|_| UnsupportedValue { |
| 48 | + at: id.span(), |
| 49 | + advice: Some("only '(GUID)' type is supported".into()), |
| 50 | + })?; |
| 51 | + Ok(v) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +/// Represents GPT partition type GUIDs |
| 56 | +#[derive(Debug, PartialEq)] |
| 57 | +pub enum PartitionTypeGuid { |
| 58 | + EfiSystemPartition, |
| 59 | + ExtendedBootLoader, |
| 60 | + LinuxSwap, |
| 61 | +} |
| 62 | + |
| 63 | +impl fmt::Display for PartitionTypeGuid { |
| 64 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 65 | + match self { |
| 66 | + Self::EfiSystemPartition => f.write_str("efi-system-partition"), |
| 67 | + Self::ExtendedBootLoader => f.write_str("linux-extended-boot"), |
| 68 | + Self::LinuxSwap => f.write_str("linux-swap"), |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +impl FromStr for PartitionTypeGuid { |
| 74 | + type Err = crate::Error; |
| 75 | + |
| 76 | + fn from_str(value: &str) -> Result<Self, Self::Err> { |
| 77 | + match value { |
| 78 | + "efi-system-partition" => Ok(Self::EfiSystemPartition), |
| 79 | + "linux-extended-boot" => Ok(Self::ExtendedBootLoader), |
| 80 | + "linux-swap" => Ok(Self::LinuxSwap), |
| 81 | + _ => Err(crate::Error::UnknownVariant), |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +impl PartitionTypeGuid { |
| 87 | + /// Returns the GUID value for this partition type |
| 88 | + pub fn as_guid(&self) -> GptPartitionType { |
| 89 | + match self { |
| 90 | + Self::EfiSystemPartition => gpt::partition_types::EFI, |
| 91 | + Self::ExtendedBootLoader => gpt::partition_types::FREEDESK_BOOT, |
| 92 | + Self::LinuxSwap => gpt::partition_types::LINUX_SWAP, |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + pub fn from_kdl_node(node: &kdl::KdlNode) -> Result<Self, crate::Error> { |
| 97 | + let value = kdl_value_to_string(get_kdl_entry(node, &0)?)?; |
| 98 | + let v = value.parse().map_err(|_| crate::UnsupportedValue { |
| 99 | + at: node.span(), |
| 100 | + advice: Some("'efi-system-partition', 'linux-swap', and 'linux-extended-boot' are supported".into()), |
| 101 | + })?; |
| 102 | + Ok(v) |
| 103 | + } |
| 104 | +} |
0 commit comments