Skip to content

Commit

Permalink
Rearrange error codes
Browse files Browse the repository at this point in the history
  • Loading branch information
RReverser committed Sep 14, 2024
1 parent 3becb0b commit fd14f86
Showing 1 changed file with 34 additions and 31 deletions.
65 changes: 34 additions & 31 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -12,24 +20,16 @@ impl TryFrom<u16> for ASCOMErrorCode {

/// Convert a raw error code into an `ASCOMErrorCode` if it's in the valid range.
fn try_from(raw: u16) -> eyre::Result<Self> {
let range = BASE..=MAX;
const RANGE: RangeInclusive<u16> = 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.
///
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit fd14f86

Please sign in to comment.