Skip to content

Commit 094061a

Browse files
committed
provisioning: Implement find_disk
Signed-off-by: Ikey Doherty <[email protected]>
1 parent 7d7e11f commit 094061a

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

crates/provisioning/src/commands.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ mod find_disk;
1313
pub enum Command {
1414
CreatePartition,
1515
CreatePartitionTable(Box<create_partition_table::Command>),
16-
FindDisk,
16+
FindDisk(Box<find_disk::Command>),
1717
}
1818

1919
/// Command execution function
2020
type CommandExec = for<'a> fn(Context<'a>) -> Result<Command, crate::Error>;
2121

2222
/// Map of command names to functions
2323
static COMMANDS: phf::Map<&'static str, CommandExec> = phf::phf_map! {
24-
//"find-disk" => find_disk::parse,
24+
"find-disk" => find_disk::parse,
2525
//"create-partition" => create_partition::parse,
2626
"create-partition-table" => create_partition_table::parse,
2727
};

crates/provisioning/src/commands/find_disk.rs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,43 @@
22
//
33
// SPDX-License-Identifier: MPL-2.0
44

5-
use super::Command;
5+
use itertools::Itertools;
6+
67
use crate::Context;
78

9+
#[derive(Debug)]
10+
pub struct Command {
11+
pub name: String,
12+
}
13+
814
/// Generate a command to find a disk
9-
pub(crate) fn parse(_context: Context<'_>) -> Result<Command, crate::Error> {
10-
unimplemented!("Command not implemented");
15+
pub(crate) fn parse(context: Context<'_>) -> Result<super::Command, crate::Error> {
16+
let arguments = context
17+
.node
18+
.entries()
19+
.iter()
20+
.filter(|e| e.is_empty() || e.name().is_none())
21+
.collect_vec();
22+
23+
let name = match arguments.len() {
24+
0 => {
25+
return Err(crate::InvalidArguments {
26+
at: context.node.span(),
27+
advice: Some("find-disk <name> - provide a name for the storage device".into()),
28+
}
29+
.into())
30+
}
31+
1 => arguments[0].value().as_string().ok_or(crate::InvalidType {
32+
at: arguments[0].span(),
33+
})?,
34+
_ => {
35+
return Err(crate::InvalidArguments {
36+
at: context.node.span(),
37+
advice: Some("find-disk <name> - only one positional argument supported".into()),
38+
}
39+
.into())
40+
}
41+
};
42+
43+
Ok(super::Command::FindDisk(Box::new(Command { name: name.to_owned() })))
1144
}

crates/provisioning/src/errors.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ pub enum Error {
2323
#[error("unknown variant")]
2424
UnknownVariant,
2525

26+
#[diagnostic(transparent)]
27+
#[error(transparent)]
28+
InvalidArguments(#[from] InvalidArguments),
29+
2630
#[diagnostic(transparent)]
2731
#[error(transparent)]
2832
InvalidType(#[from] InvalidType),
@@ -97,3 +101,15 @@ pub struct UnsupportedValue {
97101
#[help]
98102
pub advice: Option<String>,
99103
}
104+
105+
/// Error for invalid arguments
106+
#[derive(Debug, Diagnostic, Error)]
107+
#[error("invalid arguments")]
108+
#[diagnostic(severity(error))]
109+
pub struct InvalidArguments {
110+
#[label]
111+
pub at: SourceSpan,
112+
113+
#[help]
114+
pub advice: Option<String>,
115+
}

0 commit comments

Comments
 (0)