Skip to content

Commit 01fcadd

Browse files
committed
Prefer using try_lookup_task for better errors
1 parent 53afeda commit 01fcadd

8 files changed

Lines changed: 24 additions & 38 deletions

File tree

cmd/dump/src/lib.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -427,14 +427,12 @@ fn dump_via_agent(
427427
if !subargs.simulate_dumper {
428428
bail!("--simulate-task-dump requires --simulate-dumper");
429429
}
430-
let ndx = match hubris.lookup_task(task) {
431-
Some(HubrisTask::Task(ndx)) => ndx,
432-
_ => {
433-
bail!("invalid task \"{task}\"");
434-
}
430+
let ndx = match hubris.try_lookup_task(task)? {
431+
HubrisTask::Task(ndx) => ndx,
432+
HubrisTask::Kernel => bail!("invalid task \"{task}\""),
435433
};
436434

437-
Some(DumpTask::new(*ndx as u16, hubris.ticks(core)?))
435+
Some(DumpTask::new(ndx as u16, hubris.ticks(core)?))
438436
}
439437
None => None,
440438
};
@@ -709,11 +707,9 @@ fn dump_task_via_agent(
709707
let mut agent = get_dump_agent(hubris, core, subargs)?;
710708

711709
let task = subargs.task.as_ref().unwrap();
712-
let ndx = match hubris.lookup_task(task) {
713-
Some(HubrisTask::Task(ndx)) => *ndx,
714-
_ => {
715-
bail!("invalid task \"{task}\"");
716-
}
710+
let ndx = match hubris.try_lookup_task(task)? {
711+
HubrisTask::Task(ndx) => ndx,
712+
HubrisTask::Kernel => bail!("invalid task \"{task}\""),
717713
};
718714
if ndx == 0 {
719715
bail!("cannot dump supervisor");

cmd/hiffy/src/lib.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
//!
4747
4848
use ::idol::syntax::{Operation, Reply};
49-
use anyhow::{Context, Result, anyhow, bail};
49+
use anyhow::{Context, Result, bail};
5050
use clap::{CommandFactory, Parser};
5151
use humility::hubris::*;
5252
use humility::warn;
@@ -259,11 +259,7 @@ fn hiffy(context: &mut ExecutionContext) -> Result<()> {
259259
}
260260

261261
let task = match subargs.task {
262-
Some(task) => Some(
263-
hubris
264-
.lookup_task(&task)
265-
.ok_or_else(|| anyhow!("unknown task \"{}\"", task))?,
266-
),
262+
Some(task) => Some(hubris.try_lookup_task(&task)?),
267263
None => None,
268264
};
269265

cmd/jefe/src/lib.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
//! As with tasks held by `--hold`, use `--release`/`-r` to set the task back to
9595
//! normal, or `--start`/`-s` to run it once but catch the next fault.
9696
97-
use anyhow::{Result, anyhow, bail};
97+
use anyhow::{Result, bail};
9898
use clap::{CommandFactory, Parser};
9999
use humility::hubris::*;
100100
use humility_cli::{ExecutionContext, Subcommand};
@@ -150,16 +150,14 @@ fn jefe(context: &mut ExecutionContext) -> Result<()> {
150150
bail!("one of fault, start, hold, or release must be specified");
151151
};
152152

153-
let task = hubris
154-
.lookup_task(&subargs.task)
155-
.ok_or_else(|| anyhow!("couldn't find task {}", subargs.task))?;
153+
let task = hubris.try_lookup_task(&subargs.task)?;
156154

157155
let id = match task {
158156
HubrisTask::Kernel => {
159157
bail!("cannot change disposition of kernel");
160158
}
161159
HubrisTask::Task(id) => {
162-
if let Some(id) = NonZeroU32::new(*id) {
160+
if let Some(id) = NonZeroU32::new(id) {
163161
id
164162
} else {
165163
bail!("cannot change disposition of supervisor task");

cmd/powershelf/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ fn lookup_operation_enum(hubris: &HubrisArchive) -> Result<&HubrisEnum> {
5454
anyhow!("missing `power` task - is this a PSC archive?")
5555
})?;
5656

57-
let module = hubris.lookup_module(*power_task)?;
57+
let module = hubris.lookup_module(power_task)?;
5858

5959
// `Operation` matches multiple enums, and `task_power_api::Operation`
6060
// doesn't show up alone, but `Option<task_power_api::Operation>` does! Find

cmd/rpc/src/lib.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ impl<'a> RpcClient<'a> {
389389
)
390390
})?;
391391
let rpc_reply_type = hubris
392-
.lookup_module(*rpc_task)?
392+
.lookup_module(rpc_task)?
393393
.lookup_enum_byname(hubris, "RpcReply")?
394394
.ok_or_else(|| anyhow!("can't find RpcReply"))?;
395395

@@ -550,11 +550,7 @@ fn rpc_run(context: &mut ExecutionContext) -> Result<()> {
550550
}
551551

552552
let task = match &subargs.task {
553-
Some(task) => Some(
554-
hubris
555-
.lookup_task(task)
556-
.ok_or_else(|| anyhow!("unknown task \"{}\"", task))?,
557-
),
553+
Some(task) => Some(hubris.try_lookup_task(task)?),
558554
None => None,
559555
};
560556

humility-core/src/hubris.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1976,15 +1976,15 @@ impl HubrisArchive {
19761976
self.modules.values()
19771977
}
19781978

1979-
pub fn lookup_task(&self, name: &str) -> Option<&HubrisTask> {
1980-
self.tasks.get(name)
1979+
pub fn lookup_task(&self, name: &str) -> Option<HubrisTask> {
1980+
self.tasks.get(name).copied()
19811981
}
19821982

19831983
/// Tries to look up a task by name, returning an error with similar names
19841984
pub fn try_lookup_task(&self, name: &str) -> Result<HubrisTask> {
19851985
self.tasks
19861986
.get(name)
1987-
.cloned()
1987+
.copied()
19881988
.ok_or_else(|| self.task_name_suggestion(name))
19891989
}
19901990

@@ -6684,7 +6684,7 @@ impl ExternRegions {
66846684

66856685
for (name, task) in &config.tasks {
66866686
if task.extern_regions.is_some() {
6687-
set.insert(*hubris.lookup_task(name).unwrap());
6687+
set.insert(hubris.lookup_task(name).unwrap());
66886688
}
66896689
}
66906690

humility-hiffy/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ impl<'a> HiffyContext<'a> {
368368

369369
Some(
370370
hubris
371-
.lookup_module(*rpc_task)?
371+
.lookup_module(rpc_task)?
372372
.lookup_enum_byname(hubris, "RpcReply")?
373373
.ok_or_else(|| anyhow!("failed to find RpcReply"))?,
374374
)
@@ -984,7 +984,7 @@ impl<'a> HiffyContext<'a> {
984984
None
985985
}
986986

987-
HubrisTask::Task(i) => Some(*i),
987+
HubrisTask::Task(i) => Some(i),
988988
}
989989
});
990990

humility-idol/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl<'a> IdolOperation<'a> {
6262
hubris: &'a HubrisArchive,
6363
interface: &str,
6464
operation: &str,
65-
task: Option<&HubrisTask>,
65+
task: Option<HubrisTask>,
6666
) -> Result<Self> {
6767
let (task, op, code) = lookup(hubris, interface, operation, task)?;
6868
let name = (interface.to_string(), operation.to_string());
@@ -309,15 +309,15 @@ fn lookup<'a>(
309309
hubris: &'a HubrisArchive,
310310
interface: &str,
311311
operation: &str,
312-
target: Option<&HubrisTask>,
312+
target: Option<HubrisTask>,
313313
) -> Result<(HubrisTask, &'a Operation, u16)> {
314314
//
315315
// Find this interface and its operation.
316316
//
317317
let mut rval = None;
318318

319319
let tasks = match target {
320-
Some(task) => vec![*task],
320+
Some(task) => vec![task],
321321
None => {
322322
(0..hubris.ntasks()).map(|t| HubrisTask::Task(t as u32)).collect()
323323
}

0 commit comments

Comments
 (0)