From fd14f8605c0103515ef7130be9befb4db11ac818 Mon Sep 17 00:00:00 2001 From: Ingvar Stepanyan Date: Sat, 14 Sep 2024 23:10:18 +0100 Subject: [PATCH] Rearrange error codes --- src/errors.rs | 65 +++++++++++++++++++++++++++------------------------ 1 file changed, 34 insertions(+), 31 deletions(-) diff --git a/src/errors.rs b/src/errors.rs index f8de0a7..cebe063 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -1,7 +1,15 @@ use serde::{Deserialize, Serialize}; use std::borrow::Cow; +use std::ops::RangeInclusive; use thiserror::Error; +/// The starting value for error numbers. +const BASE: u16 = 0x400; +/// The starting value for driver-specific error numbers. +const DRIVER_BASE: u16 = 0x500; +/// The maximum value for error numbers. +const MAX: u16 = 0xFFF; + /// Alpaca representation of an ASCOM error code. #[derive(Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] #[serde(transparent)] @@ -12,24 +20,16 @@ impl TryFrom for ASCOMErrorCode { /// Convert a raw error code into an `ASCOMErrorCode` if it's in the valid range. fn try_from(raw: u16) -> eyre::Result { - let range = BASE..=MAX; + const RANGE: RangeInclusive = BASE..=MAX; + eyre::ensure!( - range.contains(&raw), - "Error code {raw:#X} is out of valid range ({range:#X?})", - raw = raw, - range = range, + RANGE.contains(&raw), + "Error code {raw:#X} is out of valid range ({RANGE:#X?})" ); Ok(Self(raw)) } } -/// The starting value for error numbers. -const BASE: u16 = 0x400; -/// The starting value for driver-specific error numbers. -const DRIVER_BASE: u16 = 0x500; -/// The maximum value for error numbers. -const MAX: u16 = 0xFFF; - impl ASCOMErrorCode { /// Generate ASCOM error code from a zero-based driver error code. /// @@ -159,35 +159,38 @@ macro_rules! ascom_error_codes { }; (@msg OK $doc:literal) => (""); - (@msg $name:ident $doc:literal) => ($doc); + (@msg $name:ident $doc:literal) => ($doc.trim_ascii()); } ascom_error_codes! { - #[doc = "Success"] + /// Success. pub OK = 0, // Well-known Alpaca error codes as per the specification. - #[doc = "The requested action is not implemented in this driver"] - pub ACTION_NOT_IMPLEMENTED = 0x40C, - #[doc = "The requested operation can not be undertaken at this time"] - pub INVALID_OPERATION = 0x40B, - #[doc = "Invalid value"] + + /// Property or method not implemented. + pub NOT_IMPLEMENTED = 0x400, + /// Invalid value. pub INVALID_VALUE = 0x401, - #[doc = "The attempted operation is invalid because the mount is currently in a Parked state"] + /// A value has not been set. + pub VALUE_NOT_SET = 0x402, + /// The communications channel is not connected. + pub NOT_CONNECTED = 0x407, + /// The attempted operation is invalid because the mount is currently in a Parked state. pub INVALID_WHILE_PARKED = 0x408, - #[doc = "The attempted operation is invalid because the mount is currently in a Slaved state"] + /// The attempted operation is invalid because the mount is currently in a Slaved state. pub INVALID_WHILE_SLAVED = 0x409, - #[doc = "The communications channel is not connected"] - pub NOT_CONNECTED = 0x407, - #[doc = "Property or method not implemented"] - pub NOT_IMPLEMENTED = 0x400, - #[doc = "A value has not been set"] - pub VALUE_NOT_SET = 0x402, + /// The requested operation can not be undertaken at this time. + pub INVALID_OPERATION = 0x40B, + /// The requested action is not implemented in this driver. + pub ACTION_NOT_IMPLEMENTED = 0x40C, + /// In-progress asynchronous operation has been cancelled. + pub OPERATION_CANCELLED = 0x40D, + + // Extra codes for internal use only. - // Exists to map internal client errors to the Alpaca error structure. - // Internal use only. - #[doc = "Unspecified error"] - pub(crate) UNSPECIFIED = 0x4FF, + /// Reserved 'catch-all' error code (0x4FF) used when nothing else was specified. + UNSPECIFIED = 0x4FF, } impl ASCOMError {