ui-automation 0.1.1

Control the User Interface, cross-platform.
Documentation
// Copyright (C) 2024 Tristan Gerritsen <tristan@thewoosh.org>
// All Rights Reserved.

use crate::{UIError, UIErrorKind, UIFeature};

pub type AXErrorI32 = i32;

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
#[repr(i32)]
pub enum AXError {
    #[default]
    Success,
    Failure,
    IllegalArgument,
    InvalidUIElement,
    InvalidUIElementObserver,
    CannotComplete,
    AttributeUnsupported,
    ActionUnsupported,
    NotificationUnsupported,
    NotImplemented,
    NotificationAlreadyRegistered,
    NotificationNotRegistered,
    APIDisabled,
    NoValue,
    ParameterizedAttributeUnsupported,
    NotEnoughPrecision,

    Unknown(i32),
}

impl From<AXError> for UIError {
    fn from(value: AXError) -> Self {
        match value {
            AXError::Success => panic!("todo: bad api"),
            AXError::Failure => UIErrorKind::PlatformIssue { issue: "failure (generic error)" }.into(),
            AXError::IllegalArgument => UIErrorKind::PlatformIssue { issue: "illegal argument" }.into(),
            AXError::InvalidUIElement => UIErrorKind::PlatformIssue { issue: "invalid UIElement" }.into(),
            AXError::InvalidUIElementObserver => UIErrorKind::PlatformIssue { issue: "invalid UIElementObserver" }.into(),
            AXError::CannotComplete => UIErrorKind::PlatformIssue { issue: "kAXErrorCannotComplete" }.into(),
            AXError::AttributeUnsupported => UIErrorKind::PlatformIssue { issue: "kAXErrorAttributeUnsupported" }.into(),
            AXError::ActionUnsupported => UIErrorKind::PlatformIssue { issue: "kAXErrorActionUnsupported" }.into(),
            AXError::NotificationUnsupported => UIErrorKind::PlatformIssue { issue: "kAXErrorNotificationUnsupported" }.into(),
            AXError::NotImplemented => UIErrorKind::PlatformIssue { issue: "kAXErrorNotImplemented" }.into(),
            AXError::NotificationAlreadyRegistered => UIErrorKind::PlatformIssue { issue: "kAXErrorNotificationAlreadyRegistered" }.into(),
            AXError::NotificationNotRegistered => UIErrorKind::PlatformIssue { issue: "kAXErrorNotificationNotRegistered" }.into(),
            AXError::APIDisabled => UIErrorKind::FeatureDisabled{ feature: UIFeature::Accessibility }.into(),
            AXError::NoValue => UIErrorKind::PlatformIssue { issue: "kAXErrorNoValue" }.into(),
            AXError::ParameterizedAttributeUnsupported => UIErrorKind::PlatformIssue { issue: "kAXErrorParameterizedAttributeUnsupported" }.into(),
            AXError::NotEnoughPrecision => UIErrorKind::PlatformIssue { issue: "kAXErrorNotEnoughPrecision" }.into(),
            AXError::Unknown(_) => UIErrorKind::PlatformIssue { issue: "unknown AXError" }.into(),
        }
    }
}

impl From<i32> for AXError {
    fn from(value: i32) -> Self {
        match value {
            0 => Self::Success,
            -25200 => Self::Failure,
            -25201 => Self::IllegalArgument,
            -25202 => Self::InvalidUIElement,
            -25203 => Self::InvalidUIElementObserver,
            -25204 => Self::CannotComplete,
            -25205 => Self::AttributeUnsupported,
            -25206 => Self::ActionUnsupported,
            -25207 => Self::NotificationUnsupported,
            -25208 => Self::NotImplemented,
            -25209 => Self::NotificationAlreadyRegistered,
            -25210 => Self::NotificationNotRegistered,
            -25211 => Self::APIDisabled,
            -25212 => Self::NoValue,
            -25213 => Self::ParameterizedAttributeUnsupported,
            -25214 => Self::NotEnoughPrecision,
            _ => Self::Unknown(value),
        }
    }
}