ui-automation 0.1.1

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

use std::{ffi::c_void, fmt::Display};

use crate::{UIErrorKind, UIResult};

use super::{CFString, CFStringRef, CFType};

pub type CFURLRef = *const c_void;

#[derive(Debug, Clone)]
pub struct CFURL {
    inner: CFType,
}

impl CFURL {
    #[must_use]
    pub fn from_get(str_ref: CFType) -> Option<Self> {
        Some(Self {
            inner: str_ref,
        })
    }

    pub fn get_string(&self) -> Option<CFString> {
        let str = unsafe { CFURLGetString(self.inner.ptr()) };
        CFString::from_get(CFType::from_get(str)?)
    }

    pub fn to_string(&self) -> UIResult<String> {
        let Some(str) = self.get_string() else {
            return Err(UIErrorKind::ConversionError { description: "Could not convert CFURL to CFString" }.into());
        };

        str.to_string()
    }
}

impl Display for CFURL {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.to_string().unwrap().fmt(f)
    }
}

#[allow(unused)]
extern "C-unwind" {
    pub(super) fn CFURLGetString(url: CFURLRef) -> CFStringRef;
}