|
| 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 | +use crate::kdl_value_to_string; |
| 9 | + |
| 10 | +use super::FromKdlProperty; |
| 11 | + |
| 12 | +/// The role assigned to a partition |
| 13 | +#[derive(Debug, PartialEq)] |
| 14 | +pub enum PartitionRole { |
| 15 | + /// Boot partition (usually ESP) |
| 16 | + Boot, |
| 17 | + |
| 18 | + /// Extended boot partition (e.g. XBOOTLDR) |
| 19 | + ExtendedBoot, |
| 20 | + |
| 21 | + /// Root filesystem |
| 22 | + Root, |
| 23 | + |
| 24 | + /// Home directory mount |
| 25 | + Home, |
| 26 | + |
| 27 | + /// Swap partition |
| 28 | + Swap, |
| 29 | +} |
| 30 | + |
| 31 | +impl fmt::Display for PartitionRole { |
| 32 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 33 | + match self { |
| 34 | + Self::Boot => f.write_str("boot"), |
| 35 | + Self::ExtendedBoot => f.write_str("extended-boot"), |
| 36 | + Self::Root => f.write_str("root"), |
| 37 | + Self::Home => f.write_str("home"), |
| 38 | + Self::Swap => f.write_str("swap"), |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +impl FromStr for PartitionRole { |
| 44 | + type Err = crate::Error; |
| 45 | + |
| 46 | + /// Attempt to convert a string to a partition role |
| 47 | + fn from_str(value: &str) -> Result<Self, Self::Err> { |
| 48 | + match value { |
| 49 | + "boot" => Ok(Self::Boot), |
| 50 | + "extended-boot" => Ok(Self::ExtendedBoot), |
| 51 | + "root" => Ok(Self::Root), |
| 52 | + "home" => Ok(Self::Home), |
| 53 | + "swap" => Ok(Self::Swap), |
| 54 | + _ => Err(crate::Error::UnknownVariant), |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +impl FromKdlProperty<'_> for PartitionRole { |
| 60 | + fn from_kdl_property(entry: &kdl::KdlEntry) -> Result<Self, crate::Error> { |
| 61 | + let value = kdl_value_to_string(entry)?; |
| 62 | + let v = value.parse().map_err(|_| crate::UnsupportedValue { |
| 63 | + at: entry.span(), |
| 64 | + advice: Some("'boot', 'extended-boot', 'root', 'home' and 'swap' are supported".into()), |
| 65 | + })?; |
| 66 | + Ok(v) |
| 67 | + } |
| 68 | +} |
0 commit comments