diff --git a/crates/prelude/CHANGELOG.md b/crates/prelude/CHANGELOG.md index 6f54f922..087d7dc3 100644 --- a/crates/prelude/CHANGELOG.md +++ b/crates/prelude/CHANGELOG.md @@ -4,6 +4,7 @@ ### Minor +- Change `led::{get,set}()` to never trap and return an error instead - Document all public API - Remove deprecated `println!` macro - Add `Sha384` and `HmacSha384` to the `rust-crypto` feature diff --git a/crates/prelude/src/led.rs b/crates/prelude/src/led.rs index e5fe7fd3..aaba54d6 100644 --- a/crates/prelude/src/led.rs +++ b/crates/prelude/src/led.rs @@ -32,6 +32,7 @@ pub fn count() -> usize { /// Returns the status of a LED. /// /// The `led` argument is the index of the LED. It must be less than [count()]. +#[track_caller] pub fn get(led: usize) -> api::Status { let status = convert_bool(unsafe { api::get(api::get::Params { led }) }).unwrap(); match status { @@ -44,6 +45,7 @@ pub fn get(led: usize) -> api::Status { /// /// The `led` argument is the index of the LED. It must be less than [count()]. The `status` /// argument is the new status. +#[track_caller] pub fn set(led: usize, status: api::Status) { let params = api::set::Params { led, status: status as usize }; convert_unit(unsafe { api::set(params) }).unwrap(); diff --git a/crates/scheduler/CHANGELOG.md b/crates/scheduler/CHANGELOG.md index 35a14db4..bf2b8445 100644 --- a/crates/scheduler/CHANGELOG.md +++ b/crates/scheduler/CHANGELOG.md @@ -2,6 +2,10 @@ ## 0.3.1-git +### Minor + +- Change `led::{get,set}()` to never trap and return an error instead + ### Patch - Simplify `#[cfg(all)]` attributes between board and applet features diff --git a/crates/scheduler/src/call/led.rs b/crates/scheduler/src/call/led.rs index f578ce4d..099191d6 100644 --- a/crates/scheduler/src/call/led.rs +++ b/crates/scheduler/src/call/led.rs @@ -18,9 +18,9 @@ use wasefire_board_api::led::Api as _; use wasefire_board_api::Api as Board; #[cfg(feature = "board-api-led")] use wasefire_board_api::{self as board, Id, Support}; - #[cfg(feature = "board-api-led")] -use crate::Trap; +use wasefire_error::{Code, Error}; + use crate::{DispatchSchedulerCall, SchedulerCall}; pub fn process(call: Api>) { @@ -44,8 +44,10 @@ fn count(call: SchedulerCall) { fn get(call: SchedulerCall) { let api::get::Params { led } = call.read(); let result = try { - let id = Id::new(*led as usize).ok_or(Trap)?; - board::Led::::get(id) + match Id::new(*led as usize) { + Some(id) => board::Led::::get(id), + None => Err(Error::user(Code::InvalidArgument)), + } }; call.reply(result); } @@ -54,9 +56,13 @@ fn get(call: SchedulerCall) { fn set(call: SchedulerCall) { let api::set::Params { led, status } = call.read(); let result = try { - let id = Id::new(*led as usize).ok_or(Trap)?; - let on = matches!(api::Status::try_from(*status)?, api::Status::On); - board::Led::::set(id, on) + match Id::new(*led as usize) { + Some(id) => { + let on = matches!(api::Status::try_from(*status)?, api::Status::On); + board::Led::::set(id, on) + } + None => Err(Error::user(Code::InvalidArgument)), + } }; call.reply(result); }