aes_types 0.1.0

Adobe ExtendScript external object library implementation in Rust
Documentation
mod tagged_data_imp;
mod js_script;
mod live_object;

pub use tagged_data_imp::*;
pub use js_script::*;
pub use live_object::*;
use libc::{c_char, c_long, c_void};

// SoSharedLibDefs.h error codes
pub const ES_ERR_OK: i32 = 0;
pub const ES_ERR_NO_LVALUE: i32 = 3;
pub const ES_ERR_OPEN_STRING: i32 = 4;
pub const ES_ERR_BAD_DIGIT: i32 = 6;
pub const ES_ERR_SYNTAX: i32 = 8;
pub const ES_ERR_BAD_ARGUMENT_LIST: i32 = 20;
pub const ES_ERR_NO_MEMORY: i32 = -28;
pub const ES_ERR_EXCEPTION: i32 = -29;
pub const ES_ERR_BAD_URI: i32 = 31;
pub const ES_ERR_BAD_ACTION: i32 = 32;
pub const ES_ERR_INTERNAL: i32 = -33;
pub const ES_ERR_NOT_IMPLEMENTED: i32 = -36;
pub const ES_ERR_RANGE: i32 = 41;
pub const ES_ERR_EVAL: i32 = 43;
pub const ES_ERR_CONVERSION: i32 = 44;
pub const ES_ERR_INVALID_OBJECT: i32 = 45;
pub const ES_ERR_TYPE_MISMATCH: i32 = 47;
pub const ES_ERR_NO_FILE: i32 = 48;
pub const ES_ERR_FILE_EXISTS: i32 = 49;
pub const ES_ERR_NOT_OPEN: i32 = 50;
pub const ES_ERR_EOF: i32 = 51;
pub const ES_ERR_IO: i32 = 52;
pub const ES_ERR_NO_PERMISSION: i32 = 53;
pub const ES_ERR_CANNOT_RESOLVE: i32 = 57;
pub const ES_ERR_IO_TIMEOUT: i32 = 58;
pub const ES_ERR_NO_RESPONSE: i32 = 59;

// TaggedData types
pub const TYPE_UNDEFINED: i32 = 0;
pub const TYPE_BOOL: i32 = 2;
pub const TYPE_DOUBLE: i32 = 3;
pub const TYPE_STRING: i32 = 4;
pub const TYPE_LIVE_OBJECT: i32 = 6;
pub const TYPE_LIVE_OBJECT_RELEASE: i32 = 7;
pub const TYPE_INTEGER: i32 = 123;
pub const TYPE_UINTEGER: i32 = 124;
pub const TYPE_SCRIPT: i32 = 125;




// Basic structures
#[repr(C)]
#[repr(align(8))]
#[derive(Copy, Clone)]
pub union TaggedDataUnion {
    pub intval: i32,
    pub fltval: f64,
    pub string: *mut c_char,
    pub hObject: *mut c_long,
}

#[repr(C)]
#[repr(align(8))]
#[derive(Copy, Clone)]
pub struct TaggedData {
    pub data: TaggedDataUnion,
    pub type_: c_long,
    pub filler: c_long,
}


impl std::fmt::Debug for TaggedData {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        unsafe {
            match self.type_ as i32 {
                TYPE_UNDEFINED => write!(f, "TaggedData(Undefined)"),
                TYPE_BOOL => write!(f, "TaggedData(Bool: {})", self.data.intval != 0),
                TYPE_DOUBLE => write!(f, "TaggedData(Double: {})", self.data.fltval),
                TYPE_STRING => {
                    if self.data.string.is_null() {
                        write!(f, "TaggedData(String: null)")
                    } else {
                        let c_str = std::ffi::CStr::from_ptr(self.data.string);
                        write!(f, "TaggedData(String: {:?})", c_str)
                    }
                },
                TYPE_LIVE_OBJECT | TYPE_LIVE_OBJECT_RELEASE => {
                    write!(f, "TaggedData(LiveObject: {:?})", self.data.hObject)
                },
                TYPE_INTEGER => write!(f, "TaggedData(Integer: {})", self.data.intval),
                TYPE_UINTEGER => write!(f, "TaggedData(UInteger: {})", self.data.intval),
                TYPE_SCRIPT => {
                    if self.data.string.is_null() {
                        write!(f, "TaggedData(Script: null)")
                    } else {
                        let c_str = std::ffi::CStr::from_ptr(self.data.string);
                        write!(f, "TaggedData(Script: {:?})", c_str)
                    }
                },
                _ => write!(f, "TaggedData(Unknown type: {})", self.type_)
            }
        }
    }
}

#[derive(Debug)]
pub enum Error {
    InitializationFailed,
    InvalidArguments,
    ExecutionFailed,
}

impl From<Error> for i32 {
    fn from(error: Error) -> Self {
        match error {
            Error::InitializationFailed => ES_ERR_EXCEPTION,
            Error::InvalidArguments => ES_ERR_BAD_ARGUMENT_LIST,
            Error::ExecutionFailed => ES_ERR_EVAL,
        }
    }
}

pub type Result<T> = std::result::Result<T, Error>;