aes_externalobj 0.1.2

ExtendScript external object library implementation in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
use std::ffi::{c_char,  c_void, CString};
use crate::bindings::*;
use aes_types::{TaggedData, ES_ERR_OK};
/// A safe wrapper around Adobe's ExtendScript server interface.
/// 
/// This struct provides a safe and idiomatic Rust interface to the underlying C API,
/// handling memory management, type conversions, and error handling automatically.
#[derive(Clone, Copy)]
pub struct ServerInterface<'a> {
    interface: &'a SoServerInterface,
    server: SoHServer,
}

impl<'a> ServerInterface<'a> {
    /// Creates a new ServerInterface instance.
    /// 
    /// # Arguments
    /// * `interface` - Reference to the SoServerInterface function table
    /// * `server` - Server handle for object management
    pub fn new(interface: &'a SoServerInterface, server: SoHServer) -> Self {
        Self { interface, server }
    }

    /// Get the server handle
    pub fn server(&self) -> SoHServer {
        self.server
    }

    /// Get the raw interface pointer
    pub fn raw_interface(&self) -> *const SoServerInterface {
        self.interface as *const _
    }

    /// Dumps the current state of an object for debugging purposes.
    /// 
    /// # Arguments
    /// * `h_object` - Handle to the object to dump
    /// 
    /// # Returns
    /// * `ESerror_t` - Error code indicating success or failure
    pub fn dump_object(&self, h_object: SoHObject) -> ESerror_t {
        unsafe { (self.interface.dumpObject)(h_object) }
    }

    /// Dumps the current state of the server for debugging purposes.
    /// 
    /// # Returns
    /// * `ESerror_t` - Error code indicating success or failure
    pub fn dump_server(&self) -> ESerror_t {
        unsafe { (self.interface.dumpServer)(self.server) }
    }

    /// Gets a property value from a live object.
    /// 
    /// # Arguments
    /// * `h_object` - Handle to the object
    /// * `name` - Name of the property to get
    /// 
    /// # Returns
    /// * `Result<TaggedData, ESerror_t>` - The property value or error code
    pub fn get_live_object(&self, h_object: SoHObject, name: &str) -> Result<TaggedData, ESerror_t> {
        unsafe {
            let c_name = CString::new(name).unwrap();
            let mut value = TaggedData::new_undefined();
            
            let result = (self.interface.getLiveObject)(
                self.server,
                h_object,
                c_name.as_ptr() as *mut c_char,
                &mut value
            );

            if result == ES_ERR_OK {
                Ok(value)
            } else {
                Err(result)
            }
        }
    }

    pub fn get_live_object_from_raw_address(&self, h_object: SoHObject, address: *mut c_void) -> Result<TaggedData, ESerror_t> {
        unsafe {
            let mut value = TaggedData::new_undefined();

            let result = (self.interface.getLiveObject)(
                self.server,
                h_object,
                address as *mut c_char,
                &mut value
            );

            if result == ES_ERR_OK {
                Ok(value)
            } else {
                Err(result)
            }
        }
    }


    /// Sets a property value on a live object.
    /// 
    /// # Arguments
    /// * `h_object` - Handle to the object
    /// * `name` - Name of the property to set
    /// * `value` - Value to set
    /// 
    /// # Returns
    /// * `ESerror_t` - Error code indicating success or failure
    pub fn put_live_object(&self, h_object: SoHObject, name: &str, value: &TaggedData) -> ESerror_t {
        unsafe {
            let c_name = CString::new(name).unwrap();
            (self.interface.putLiveObject)(
                self.server,
                h_object,
                c_name.as_ptr() as *mut c_char,
                value as *const _ as *mut _
            )
        }
    }

    /// Calls a method on a live object.
    /// 
    /// # Arguments
    /// * `h_object` - Handle to the object
    /// * `method` - Name of the method to call
    /// * `args` - Array of arguments to pass to the method
    /// 
    /// # Returns
    /// * `Result<TaggedData, ESerror_t>` - The return value or error code
    pub fn call_live_object(&self, h_object: SoHObject, method: &str, args: &[TaggedData]) -> Result<TaggedData, ESerror_t> {
        unsafe {
            let c_method = CString::new(method).unwrap();
            let mut result = TaggedData::new_undefined();
            
            let error = (self.interface.callLiveObject)(
                self.server,
                h_object,
                c_method.as_ptr() as *mut c_char,
                args.len() as i32,
                args.as_ptr() as *mut _,
                &mut result
            );

            if error == ES_ERR_OK {
                Ok(result)
            } else {
                Err(error)
            }
        }
    }

    /// Evaluates a JavaScript script.
    /// 
    /// # Arguments
    /// * `script` - JavaScript code to evaluate
    /// 
    /// # Returns
    /// * `Result<TaggedData, ESerror_t>` - The result of evaluation or error code
    pub fn eval(&self, script: &str) -> Result<TaggedData, ESerror_t> {
        unsafe {
            let c_script = CString::new(script).unwrap();
            let mut result = TaggedData::new_undefined();
            
            let error = (self.interface.eval)(
                self.server,
                c_script.into_raw(),
                &mut result
            );

            if error == ES_ERR_OK {
                Ok(result)
            } else {
                Err(error)
            }
        }
    }

    /// Initializes a new TaggedData instance.
    /// 
    /// # Returns
    /// * `Result<TaggedData, ESerror_t>` - The initialized TaggedData or error code
    pub fn tagged_data_init(&self) -> Result<TaggedData, ESerror_t> {
        unsafe {
            let mut data = TaggedData::new_undefined();
            let result = (self.interface.taggedDataInit)(self.server, &mut data);
            
            if result == ES_ERR_OK {
                Ok(data)
            } else {
                Err(result)
            }
        }
    }

    /// Frees a TaggedData instance.
    /// 
    /// # Arguments
    /// * `data` - TaggedData instance to free
    pub fn tagged_data_free(&self, data: &mut TaggedData) {
        unsafe {
            let _ = (self.interface.taggedDataFree)(self.server, data);
        }
    }

    /// Adds a new JavaScript class.
    /// 
    /// # Arguments
    /// * `name` - Name of the class
    /// * `object_interface` - Interface implementation for the class
    /// 
    /// # Returns
    /// * `ESerror_t` - Error code indicating success or failure
    pub fn add_class(&self, name: &str, object_interface: &mut SoObjectInterface) -> ESerror_t {
        unsafe {
            let c_name = CString::new(name).unwrap();
            (self.interface.addClass)(
                self.server,
                c_name.as_ptr() as *mut c_char,
                object_interface as *mut _
            )
        }
    }

    /// Adds a method to an object.
    /// 
    /// # Arguments
    /// * `h_object` - Handle to the object
    /// * `name` - Name of the method
    /// * `id` - Method ID
    /// * `desc` - Method description
    /// 
    /// # Returns
    /// * `ESerror_t` - Error code indicating success or failure
    pub fn add_method(&self, h_object: SoHObject, name: &str, id: i32, desc: &str) -> ESerror_t {
        unsafe {
            let c_name = CString::new(name).unwrap();
            let c_desc = CString::new(desc).unwrap();
            (self.interface.addMethod)(
                h_object,
                c_name.as_ptr(),
                id,
                c_desc.as_ptr() as *mut c_char
            )
        }
    }

    /// Adds multiple methods to an object.
    /// 
    /// # Arguments
    /// * `h_object` - Handle to the object
    /// * `names` - Array of method definitions
    /// 
    /// # Returns
    /// * `ESerror_t` - Error code indicating success or failure
    pub fn add_methods(&self, h_object: SoHObject, names: &mut [SoCClientName]) -> ESerror_t {
        unsafe {
            (self.interface.addMethods)(
                h_object,
                names.as_mut_ptr()
            )
        }
    }

    /// Adds a property to an object.
    /// 
    /// # Arguments
    /// * `h_object` - Handle to the object
    /// * `name` - Name of the property
    /// * `id` - Property ID
    /// * `desc` - Property description
    /// 
    /// # Returns
    /// * `ESerror_t` - Error code indicating success or failure
    pub fn add_property(&self, h_object: SoHObject, name: &str, id: i32, desc: &str) -> ESerror_t {
        unsafe {
            let c_name = CString::new(name).unwrap();
            let c_desc = CString::new(desc).unwrap();
            (self.interface.addProperty)(
                h_object,
                c_name.as_ptr(),
                id,
                c_desc.as_ptr() as *mut c_char
            )
        }
    }

    /// Adds multiple properties to an object.
    /// 
    /// # Arguments
    /// * `h_object` - Handle to the object
    /// * `names` - Array of property definitions
    /// 
    /// # Returns
    /// * `ESerror_t` - Error code indicating success or failure
    pub fn add_properties(&self, h_object: SoHObject, names: &mut [SoCClientName]) -> ESerror_t {
        unsafe {
            (self.interface.addProperties)(
                h_object,
                names.as_mut_ptr()
            )
        }
    }

    /// Gets the class name of an object.
    /// 
    /// # Arguments
    /// * `h_object` - Handle to the object
    /// * `max_name_len` - Maximum length of the class name
    /// 
    /// # Returns
    /// * `Result<String, ESerror_t>` - The class name or error code
    pub fn get_class(&self, h_object: SoHObject, max_name_len: i32) -> Result<String, ESerror_t> {
        unsafe {
            let mut buffer = vec![0u8; max_name_len as usize];
            let result = (self.interface.getClass)(
                h_object,
                buffer.as_mut_ptr() as *mut c_char,
                max_name_len
            );

            if result == ES_ERR_OK {
                Ok(String::from_utf8_lossy(&buffer).trim_matches(char::from(0)).to_string())
            } else {
                Err(result)
            }
        }
    }

    /// Gets the server interface for an object.
    /// 
    /// # Arguments
    /// * `h_object` - Handle to the object
    /// 
    /// # Returns
    /// * `Result<(SoHServer, &'a SoServerInterface), ESerror_t>` - Server handle and interface or error code
    pub fn get_server(&self, h_object: SoHObject) -> Result<(SoHServer, &'a SoServerInterface), ESerror_t> {
        unsafe {
            let mut server = std::ptr::null_mut();
            let mut interface = std::ptr::null_mut();
            
            let result = (self.interface.getServer)(
                h_object,
                &mut server,
                &mut interface
            );

            if result == ES_ERR_OK {
                Ok((server, &*interface))
            } else {
                Err(result)
            }
        }
    }

    /// Sets client data for an object.
    /// 
    /// # Arguments
    /// * `h_object` - Handle to the object
    /// * `client_data` - Pointer to client data
    /// 
    /// # Returns
    /// * `ESerror_t` - Error code indicating success or failure
    pub fn set_client_data(&self, h_object: SoHObject, client_data: *mut c_void) -> ESerror_t {
        unsafe {
            (self.interface.setClientData)(h_object, client_data)
        }
    }

    /// Gets client data from an object.
    /// 
    /// # Type Parameters
    /// * `T` - Type of the client data
    /// 
    /// # Arguments
    /// * `h_object` - Handle to the object
    /// 
    /// # Returns
    /// * `Result<*mut T, ESerror_t>` - Pointer to client data or error code
    pub fn get_client_data<T>(&self, h_object: SoHObject) -> Result<*mut T, ESerror_t> {
        unsafe {
            let mut client_data: *mut c_void = std::ptr::null_mut();
            let result = (self.interface.getClientData)(h_object, &mut client_data);
            
            if result == ES_ERR_OK {
                Ok(client_data as *mut T)
            } else {
                Err(result)
            }
        }
    }
}

/// Macro for safely accessing the server interface.
/// 
/// # Arguments
/// * `$retval` - Return value pointer for error handling
/// * `$interface` - Name for the server interface instance
/// * `$body` - Code block to execute with the server interface
#[macro_export]
macro_rules! with_server_interface {
    ($retval:expr, |$interface:ident| $body:expr) => {
        match { CLIENT_DATA.as_ref().and_then(|data| data.server_interface.as_ref()) } {
            Some(raw_interface) => {
                let $interface = ServerInterface::new(raw_interface, get_server!($retval));
                $body
            },
            None => return make_error_result(ES_ERR_NO_MEMORY, $retval)
        }
    };
}