Skip to content

Commit a663208

Browse files
committed
provisioning: Ergonomic processing, don't permit invalid nodes
Signed-off-by: Ikey Doherty <[email protected]>
1 parent bbdb76a commit a663208

File tree

4 files changed

+38
-17
lines changed

4 files changed

+38
-17
lines changed

crates/provisioning/src/commands/create_partition.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub struct Command {
2323
/// The GUID of the partition type
2424
pub partition_type: Option<PartitionTypeGuid>,
2525

26+
/// Constraints for the partition
2627
pub constraints: Constraints,
2728
}
2829

@@ -36,23 +37,27 @@ pub(crate) fn parse(context: Context<'_>) -> Result<super::Command, crate::Error
3637
None
3738
};
3839

39-
let constraints =
40-
if let Some(constraints) = context.node.iter_children().find(|n| n.name().value() == "constraints") {
41-
Constraints::from_kdl_node(constraints)?
42-
} else {
43-
return Err(crate::Error::MissingNode("constraints"));
44-
};
45-
46-
let partition_type = if let Some(partition_type) = context.node.iter_children().find(|n| n.name().value() == "type")
47-
{
48-
match PartitionTypeKDL::from_kdl_type(get_kdl_entry(partition_type, &0)?)? {
49-
PartitionTypeKDL::GUID => Some(PartitionTypeGuid::from_kdl_node(partition_type)?),
40+
let mut constraints = Constraints::default();
41+
let mut partition_type = None;
42+
43+
for child in context.node.iter_children() {
44+
match child.name().value() {
45+
"constraints" => constraints = Constraints::from_kdl_node(child)?,
46+
"type" => {
47+
partition_type = match PartitionTypeKDL::from_kdl_type(get_kdl_entry(child, &0)?)? {
48+
PartitionTypeKDL::GUID => Some(PartitionTypeGuid::from_kdl_node(child)?),
49+
}
50+
}
51+
_ => {
52+
return Err(crate::UnsupportedNode {
53+
at: child.span(),
54+
name: child.name().value().into(),
55+
}
56+
.into())
57+
}
5058
}
51-
} else {
52-
None
53-
};
59+
}
5460

55-
// TODO: Load constraints etc
5661
Ok(super::Command::CreatePartition(Box::new(Command {
5762
disk,
5863
id,

crates/provisioning/src/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ pub struct MissingEntry {
108108
/// Error for unsupported node types
109109
#[derive(Debug, Diagnostic, Error)]
110110
#[error("unsupported node: {name}")]
111-
#[diagnostic(severity(warning))]
111+
#[diagnostic(severity(error))]
112112
pub struct UnsupportedNode {
113113
#[label]
114114
pub at: SourceSpan,

crates/provisioning/src/types/constraints.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{get_kdl_entry, kdl_value_to_storage_size};
66

77
/// Constraints for partition size, 1:1 mapping to SizeRequirements in
88
/// partitioning strategy internals.
9-
#[derive(Debug)]
9+
#[derive(Debug, Default, Clone, Copy)]
1010
pub enum Constraints {
1111
/// Exact size in bytes
1212
Exact(u64),
@@ -16,6 +16,10 @@ pub enum Constraints {
1616
Range { min: u64, max: u64 },
1717
/// Use all remaining space
1818
Remaining,
19+
20+
/// Default constraints
21+
#[default]
22+
Invalid,
1923
}
2024

2125
impl Constraints {

crates/provisioning/tests/use_whole_disk.kdl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ strategy name="whole_disk" summary="Wipe and use an entire disk" {
1818
max (GiB)2
1919
}
2020
type (GUID)"efi-system-partition"
21+
filesystem {
22+
type "fat32"
23+
label "ESP"
24+
}
2125
}
2226

2327
// Create xbootldr
@@ -27,6 +31,10 @@ strategy name="whole_disk" summary="Wipe and use an entire disk" {
2731
max (GiB)4
2832
}
2933
type (GUID) "linux-extended-boot"
34+
filesystem {
35+
type "fat32"
36+
label "XBOOTLDR"
37+
}
3038
}
3139

3240
// Create a partition for rootfs
@@ -35,6 +43,10 @@ strategy name="whole_disk" summary="Wipe and use an entire disk" {
3543
min (GiB)30
3644
max (GiB)120
3745
}
46+
filesystem {
47+
type "any"
48+
label "ROOT"
49+
}
3850
}
3951

4052
// find a partition (bound to root_disk here)

0 commit comments

Comments
 (0)