Skip to content

Commit

Permalink
Simplify new_for_driver
Browse files Browse the repository at this point in the history
  • Loading branch information
RReverser committed May 27, 2023
1 parent 93b175f commit 3773e02
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ const MAX: u16 = 0xFFF;
impl ASCOMErrorCode {
/// Generate ASCOM error code from a zero-based driver error code.
///
/// Will panic if the driver error code is larger than the maximum allowed (2815).
///
/// You'll typically want to define an enum for your driver errors and use this in a single
/// place - in the [`From`] conversion from your driver error type to the [`ASCOMError`].
///
Expand Down Expand Up @@ -64,12 +66,12 @@ impl ASCOMErrorCode {
/// }
/// }
/// ```
pub fn new_for_driver(driver_code: u16) -> Self {
let raw = match driver_code.checked_add(DRIVER_BASE) {
Some(raw) if raw <= MAX => raw,
_ => panic!("Driver error code is too large"),
};
Self(raw)
pub const fn new_for_driver(driver_code: u16) -> Self {
const DRIVER_MAX: u16 = MAX - DRIVER_BASE;

assert!(driver_code <= DRIVER_MAX, "Driver error code is too large");

Self(driver_code + DRIVER_BASE)
}

/// Get the driver-specific error code.
Expand Down

0 comments on commit 3773e02

Please sign in to comment.