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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
//! TestStand `PropertyObject` (`IPropertyObject`) wrapper.
use crate::Error;
use crate::dispids::property_object;
use crate::dispids::property_object_introspect as introspect;
use crate::enums::PropValType;
use rs_teststand_sys::{Dispatch, Value};
/// Safe wrapper for TestStand™ `PropertyObject` (`IPropertyObject`).
#[derive(Debug)]
pub struct PropertyObject {
dispatch: Box<dyn Dispatch>,
}
impl PropertyObject {
/// Creates a new `PropertyObject` wrapper around a COM dispatch seam.
pub(crate) fn new(dispatch: Box<dyn Dispatch>) -> Self {
Self { dispatch }
}
/// Returns the underlying `Dispatch` reference for internal COM calls.
pub(crate) fn as_dispatch(&self) -> &dyn Dispatch {
&*self.dispatch
}
/// Checks if a property exists by lookup path (`PropertyObject.Exists`).
///
/// # Errors
/// [`Error`] if the COM call fails or returns an unexpected type.
pub fn exists(&self, lookup_string: &str, options: i32) -> Result<bool, Error> {
Ok(self
.dispatch
.call(
property_object::EXISTS,
&[Value::Str(lookup_string.to_string()), Value::I32(options)],
)?
.as_bool()?)
}
/// Reads a string property by lookup path (`PropertyObject.GetValString`).
///
/// # Errors
/// [`Error`] if the COM call fails or returns an unexpected type.
pub fn get_val_string(&self, lookup_string: &str, options: i32) -> Result<String, Error> {
Ok(self
.dispatch
.call(
property_object::GET_VAL_STRING,
&[Value::Str(lookup_string.to_string()), Value::I32(options)],
)?
.into_string()?)
}
/// Writes a string property by lookup path (`PropertyObject.SetValString`).
///
/// # Errors
/// [`Error`] if the COM call fails.
pub fn set_val_string(
&self,
lookup_string: &str,
options: i32,
value: &str,
) -> Result<(), Error> {
self.dispatch.call(
property_object::SET_VAL_STRING,
&[
Value::Str(lookup_string.to_string()),
Value::I32(options),
Value::Str(value.to_string()),
],
)?;
Ok(())
}
/// Reads a numeric property by lookup path (`PropertyObject.GetValNumber`).
///
/// # Errors
/// [`Error`] if the COM call fails or returns an unexpected type.
pub fn get_val_number(&self, lookup_string: &str, options: i32) -> Result<f64, Error> {
Ok(self
.dispatch
.call(
property_object::GET_VAL_NUMBER,
&[Value::Str(lookup_string.to_string()), Value::I32(options)],
)?
.as_f64()?)
}
/// Writes a numeric property by lookup path (`PropertyObject.SetValNumber`).
///
/// # Errors
/// [`Error`] if the COM call fails.
pub fn set_val_number(
&self,
lookup_string: &str,
options: i32,
value: f64,
) -> Result<(), Error> {
self.dispatch.call(
property_object::SET_VAL_NUMBER,
&[
Value::Str(lookup_string.to_string()),
Value::I32(options),
Value::F64(value),
],
)?;
Ok(())
}
/// Reads a boolean property by lookup path (`PropertyObject.GetValBoolean`).
///
/// # Errors
/// [`Error`] if the COM call fails or returns an unexpected type.
pub fn get_val_bool(&self, lookup_string: &str, options: i32) -> Result<bool, Error> {
Ok(self
.dispatch
.call(
property_object::GET_VAL_BOOLEAN,
&[Value::Str(lookup_string.to_string()), Value::I32(options)],
)?
.as_bool()?)
}
/// Writes a boolean property by lookup path (`PropertyObject.SetValBoolean`).
///
/// # Errors
/// [`Error`] if the COM call fails.
pub fn set_val_bool(
&self,
lookup_string: &str,
options: i32,
value: bool,
) -> Result<(), Error> {
self.dispatch.call(
property_object::SET_VAL_BOOLEAN,
&[
Value::Str(lookup_string.to_string()),
Value::I32(options),
Value::Bool(value),
],
)?;
Ok(())
}
/// How many sub-properties sit directly under `lookup_string`
/// (`GetNumSubProperties`).
///
/// Pass an empty string for this object itself.
///
/// # Errors
/// [`Error`] if the COM call fails or returns an unexpected type.
pub fn get_num_sub_properties(&self, lookup_string: &str) -> Result<i32, Error> {
Ok(self
.dispatch
.call(
introspect::GET_NUM_SUB_PROPERTIES,
&[Value::Str(lookup_string.to_owned())],
)?
.as_i32()?)
}
/// The name of the `index`-th sub-property (`GetNthSubPropertyName`).
///
/// # Errors
/// [`Error`] if the COM call fails or returns an unexpected type.
pub fn get_nth_sub_property_name(
&self,
lookup_string: &str,
index: i32,
options: i32,
) -> Result<String, Error> {
Ok(self
.dispatch
.call(
introspect::GET_NTH_SUB_PROPERTY_NAME,
&[
Value::Str(lookup_string.to_owned()),
Value::I32(index),
Value::I32(options),
],
)?
.into_string()?)
}
/// The `index`-th sub-property itself (`GetNthSubProperty`).
///
/// # Errors
/// [`Error`] if the COM call fails or returns an unexpected type.
pub fn get_nth_sub_property(
&self,
lookup_string: &str,
index: i32,
options: i32,
) -> Result<Self, Error> {
Ok(Self::new(
self.dispatch
.call(
introspect::GET_NTH_SUB_PROPERTY,
&[
Value::Str(lookup_string.to_owned()),
Value::I32(index),
Value::I32(options),
],
)?
.into_object()?,
))
}
/// Reads a signed 64-bit integer property (`GetValInteger64`).
///
/// The engine stores a number as one of three things — a double, a signed
/// 64-bit integer, or an unsigned one — and the accessor must match. Use
/// this when [`property_type`](Self::property_type) reports
/// [`PropertyRepresentation::Int64`](crate::PropertyRepresentation::Int64);
/// [`get_val_number`](Self::get_val_number) fails on such a property rather
/// than converting.
///
/// # Errors
/// [`Error`] if the COM call fails or the property is not stored as a
/// signed 64-bit integer.
pub fn get_val_integer64(&self, lookup_string: &str, options: i32) -> Result<i64, Error> {
Ok(self
.dispatch
.call(
introspect::GET_VAL_INTEGER64,
&[Value::Str(lookup_string.to_owned()), Value::I32(options)],
)?
.as_i64()?)
}
/// Writes a signed 64-bit integer property (`SetValInteger64`).
///
/// # Errors
/// [`Error`] if the COM call fails.
pub fn set_val_integer64(
&self,
lookup_string: &str,
options: i32,
value: i64,
) -> Result<(), Error> {
self.dispatch.call(
introspect::SET_VAL_INTEGER64,
&[
Value::Str(lookup_string.to_owned()),
Value::I32(options),
Value::I64(value),
],
)?;
Ok(())
}
/// Reads an unsigned 64-bit integer property (`GetValUnsignedInteger64`).
///
/// Use this when the representation is
/// [`UInt64`](crate::PropertyRepresentation::UInt64). The value crosses the
/// COM boundary as `VT_UI8` and is returned with its bits intact, so the
/// full unsigned range survives.
///
/// # Errors
/// [`Error`] if the COM call fails or the property is not stored as an
/// unsigned 64-bit integer.
pub fn get_val_unsigned_integer64(
&self,
lookup_string: &str,
options: i32,
) -> Result<u64, Error> {
let raw = self
.dispatch
.call(
introspect::GET_VAL_UNSIGNED_INTEGER64,
&[Value::Str(lookup_string.to_owned()), Value::I32(options)],
)?
.as_i64()?;
// The sys layer carries VT_UI8 as i64 to keep one integer variant;
// reinterpreting restores the unsigned reading of the same bits.
// `cast_unsigned` would be clearer but postdates this crate's MSRV.
#[allow(clippy::cast_sign_loss, reason = "bit-preserving reinterpretation")]
Ok(raw as u64)
}
/// Writes an unsigned 64-bit integer property (`SetValUnsignedInteger64`).
///
/// # Errors
/// [`Error`] if the COM call fails.
pub fn set_val_unsigned_integer64(
&self,
lookup_string: &str,
options: i32,
value: u64,
) -> Result<(), Error> {
self.dispatch.call(
introspect::SET_VAL_UNSIGNED_INTEGER64,
&[
Value::Str(lookup_string.to_owned()),
Value::I32(options),
Value::U64(value),
],
)?;
Ok(())
}
/// The per-property numeric format string (`PropertyObject.NumericFormat`).
///
/// A `printf`-style format that decides how
/// [`get_formatted_value`](Self::get_formatted_value) renders a number, so
/// the same stored value can display as decimal, hex, octal or binary. It
/// is presentation only — the underlying number is unchanged.
///
/// Two departures from C: `%b` formats in binary, and a `$` placed straight
/// after the `%` strips trailing zeros after the decimal point. An empty
/// string restores the default format.
///
/// # Errors
/// [`Error`] if the COM call fails or returns an unexpected type.
pub fn numeric_format(&self) -> Result<String, Error> {
Ok(self
.dispatch
.get(introspect::NUMERIC_FORMAT)?
.into_string()?)
}
/// Sets the numeric format string (`PropertyObject.NumericFormat`).
///
/// # Errors
/// [`Error`] if the COM call fails.
pub fn set_numeric_format(&self, format: &str) -> Result<(), Error> {
self.dispatch
.put(introspect::NUMERIC_FORMAT, Value::Str(format.to_owned()))?;
Ok(())
}
/// Renders a property's value as display text (`GetFormattedValue`).
///
/// `format` overrides the formatting for this call; pass an empty string to
/// use the default. Set `use_value_format_if_defined` to honour the
/// property's own [`numeric_format`](Self::numeric_format) instead.
/// `separator` joins array elements.
///
/// Containers render as `...` and an empty reference as `Nothing`, so the
/// result is always displayable text rather than an error.
///
/// # Errors
/// [`Error`] if the COM call fails or returns an unexpected type.
pub fn get_formatted_value(
&self,
lookup_string: &str,
options: i32,
format: &str,
use_value_format_if_defined: bool,
separator: &str,
) -> Result<String, Error> {
Ok(self
.dispatch
.call(
introspect::GET_FORMATTED_VALUE,
&[
Value::Str(lookup_string.to_owned()),
Value::I32(options),
Value::Str(format.to_owned()),
Value::Bool(use_value_format_if_defined),
Value::Str(separator.to_owned()),
],
)?
.into_string()?)
}
/// The object's name (`PropertyObject.Name`).
///
/// A type definition must be named before it can be registered.
///
/// # Errors
/// [`Error`] if the COM call fails or returns an unexpected type.
pub fn name(&self) -> Result<String, Error> {
Ok(self.dispatch.get(introspect::NAME)?.into_string()?)
}
/// Sets the object's name (`PropertyObject.Name`).
///
/// # Errors
/// [`Error`] if the COM call fails.
pub fn set_name(&self, name: &str) -> Result<(), Error> {
self.dispatch
.put(introspect::NAME, Value::Str(name.to_owned()))?;
Ok(())
}
/// A type's version, as `major.minor.revision.build`
/// (`PropertyObject.TypeVersion`).
///
/// Which field is bumped carries meaning: raising the lowest field signals
/// a change the engine can apply to existing instances silently, while
/// raising a higher one marks the change as deliberate.
///
/// # Errors
/// [`Error`] if the COM call fails or returns an unexpected type.
pub fn type_version(&self) -> Result<String, Error> {
Ok(self.dispatch.get(introspect::TYPE_VERSION)?.into_string()?)
}
/// Sets a type's version (`PropertyObject.TypeVersion`).
///
/// # Errors
/// [`Error`] if the COM call fails.
pub fn set_type_version(&self, version: &str) -> Result<(), Error> {
self.dispatch
.put(introspect::TYPE_VERSION, Value::Str(version.to_owned()))?;
Ok(())
}
/// The object's attributes (`PropertyObject.Attributes`).
///
/// A property tree of its own, used for metadata that is not part of the
/// value — an enumeration's strictness flag, for instance.
///
/// # Errors
/// [`Error`] if the COM call fails or returns an unexpected type.
pub fn attributes(&self) -> Result<Self, Error> {
Ok(Self::new(
self.dispatch.get(introspect::ATTRIBUTES)?.into_object()?,
))
}
/// An enumeration's enumerators (`PropertyObject.Enumerators`).
///
/// # Errors
/// [`Error`] if the COM call fails or returns an unexpected type.
pub fn enumerators(&self) -> Result<Self, Error> {
Ok(Self::new(
self.dispatch.get(introspect::ENUMERATORS)?.into_object()?,
))
}
/// Replaces an enumeration's enumerators (`UpdateEnumerators`).
///
/// Expects an array of containers, each holding `EnumeratorName` and
/// `EnumeratorValue`. Strictness rides on the array's attributes rather
/// than on an element.
///
/// This only has an effect on a **registered** type definition; calling it
/// on the loose object that was inserted changes nothing. Every loaded
/// instance of the type is updated.
///
/// # Errors
/// [`Error`] if the COM call fails or the argument is not a live object.
pub fn update_enumerators(&self, enumerators: &Self) -> Result<bool, Error> {
let handle = enumerators
.duplicate_dispatch()
.ok_or(Error::UnexpectedType {
expected: "a live property object",
actual: "a test fake with no COM identity",
})?;
Ok(self
.dispatch
.call(introspect::UPDATE_ENUMERATORS, &[Value::Object(handle)])?
.as_bool()?)
}
/// The display name of a value (`GetValueDisplayName`).
///
/// For an enumeration this is the enumerator's name rather than its number.
///
/// # Errors
/// [`Error`] if the COM call fails or returns an unexpected type.
pub fn get_value_display_name(
&self,
lookup_string: &str,
options: i32,
) -> Result<String, Error> {
Ok(self
.dispatch
.call(
introspect::GET_VALUE_DISPLAY_NAME,
&[Value::Str(lookup_string.to_owned()), Value::I32(options)],
)?
.into_string()?)
}
/// An owned handle to the same object, for passing it back to the engine.
pub(crate) fn duplicate_dispatch(&self) -> Option<Box<dyn Dispatch>> {
self.dispatch.duplicate()
}
/// Evaluates an expression in this object's context (`Evaluate`).
///
/// Superseded by [`evaluate_ex`](Self::evaluate_ex), which adds an options
/// argument. This form is kept because it is the member available on
/// engines from TestStand 2016, which the crate supports — a caller
/// targeting the whole range can use it without a version check.
///
/// # Errors
/// [`Error`] if the expression is invalid or the COM call fails.
pub fn evaluate(&self, expression: &str) -> Result<Self, Error> {
Ok(Self::new(
self.dispatch
.call(introspect::EVALUATE, &[Value::Str(expression.to_owned())])?
.into_object()?,
))
}
/// Evaluates an expression in this object's context (`EvaluateEx`).
///
/// The object is the scope: the expression can name this property's
/// subproperties directly. The result comes back as a `PropertyObject`
/// holding whatever type the expression produced, so read it with the
/// accessor that matches — or with `to_value`.
///
/// `Evaluate` is the obsolete form of this member; use this one.
///
/// # Errors
/// [`Error`] if the expression is invalid or the COM call fails.
pub fn evaluate_ex(&self, expression: &str, options: i32) -> Result<Self, Error> {
Ok(Self::new(
self.dispatch
.call(
introspect::EVALUATE_EX,
&[Value::Str(expression.to_owned()), Value::I32(options)],
)?
.into_object()?,
))
}
/// This property's type object (`PropertyObject.Type`).
///
/// # Errors
/// [`Error`] if the COM call fails or returns an unexpected type.
pub fn property_type(&self) -> Result<crate::PropertyObjectType, Error> {
Ok(crate::PropertyObjectType::new(
self.dispatch.get(introspect::TYPE)?.into_object()?,
))
}
/// A human-readable name for a property's type (`GetTypeDisplayString`).
///
/// Obsolete in the engine; prefer
/// [`property_type`](Self::property_type) then `display_string`.
///
/// This is the in-only route to identifying a type. `GetType` reports the
/// same thing in more detail but returns three of its five arguments by
/// reference, which the dispatch seam does not yet support.
///
/// # Errors
/// [`Error`] if the COM call fails or returns an unexpected type.
pub fn get_type_display_string(
&self,
lookup_string: &str,
options: i32,
) -> Result<String, Error> {
Ok(self
.dispatch
.call(
introspect::GET_TYPE_DISPLAY_STRING,
&[Value::Str(lookup_string.to_owned()), Value::I32(options)],
)?
.into_string()?)
}
/// A property's type as a flag set (`GetTypeFlags`).
///
/// # Errors
/// [`Error`] if the COM call fails or returns an unexpected type.
pub fn get_type_flags(
&self,
lookup_string: &str,
options: i32,
) -> Result<crate::PropertyValueTypeFlags, Error> {
let raw = self
.dispatch
.call(
introspect::GET_TYPE_FLAGS,
&[Value::Str(lookup_string.to_owned()), Value::I32(options)],
)?
.as_i32()?;
Ok(crate::PropertyValueTypeFlags::from_bits_retain(raw))
}
/// An array element by position (`GetPropertyObjectByOffset`).
///
/// # Errors
/// [`Error`] if the COM call fails or returns an unexpected type.
pub fn get_property_object_by_offset(&self, offset: i32, options: i32) -> Result<Self, Error> {
Ok(Self::new(
self.dispatch
.call(
introspect::GET_PROPERTY_OBJECT_BY_OFFSET,
&[Value::I32(offset), Value::I32(options)],
)?
.into_object()?,
))
}
/// Resizes an array property (`SetNumElements`).
///
/// # Errors
/// [`Error`] if the COM call fails.
pub fn set_num_elements(&self, count: i32, options: i32) -> Result<(), Error> {
self.dispatch.call(
introspect::SET_NUM_ELEMENTS,
&[Value::I32(count), Value::I32(options)],
)?;
Ok(())
}
/// The number of elements in an array property (`GetNumElements`).
///
/// # Errors
/// [`Error`] if the COM call fails or returns an unexpected type.
pub fn get_num_elements(&self) -> Result<i32, Error> {
Ok(self
.dispatch
.call(introspect::GET_NUM_ELEMENTS, &[])?
.as_i32()?)
}
/// Retrieves a nested `PropertyObject` by lookup path (`PropertyObject.GetPropertyObject`).
///
/// # Errors
/// [`Error`] if the COM call fails or returns an unexpected type.
pub fn get_property_object(&self, lookup_string: &str, options: i32) -> Result<Self, Error> {
let dispatch = self
.dispatch
.call(
property_object::GET_PROPERTY_OBJECT,
&[Value::Str(lookup_string.to_string()), Value::I32(options)],
)?
.into_object()?;
Ok(Self::new(dispatch))
}
/// Attaches a nested `PropertyObject` by lookup path (`PropertyObject.SetPropertyObject`).
///
/// # Errors
/// [`Error`] if the COM call fails.
pub fn set_property_object(
&self,
lookup_string: &str,
options: i32,
property_object_value: &Self,
) -> Result<(), Error> {
let idispatch =
property_object_value
.as_dispatch()
.as_idispatch()
.ok_or(Error::UnexpectedType {
expected: "live COM dispatch object",
actual: "fake dispatch object",
})?;
let com_dispatch = rs_teststand_sys::ComDispatch::new(idispatch.clone());
self.dispatch.call(
property_object::SET_PROPERTY_OBJECT,
&[
Value::Str(lookup_string.to_string()),
Value::I32(options),
Value::Object(Box::new(com_dispatch)),
],
)?;
Ok(())
}
/// Creates a new sub-property (`PropertyObject.NewSubProperty`).
///
/// # Errors
/// [`Error`] if the COM call fails.
pub fn new_sub_property(
&self,
lookup_string: &str,
value_type: PropValType,
as_array: bool,
type_name: &str,
options: i32,
) -> Result<(), Error> {
self.dispatch.call(
property_object::NEW_SUB_PROPERTY,
&[
Value::Str(lookup_string.to_string()),
Value::I32(value_type as i32),
Value::Bool(as_array),
Value::Str(type_name.to_string()),
Value::I32(options),
],
)?;
Ok(())
}
/// Deletes a sub-property by lookup path (`PropertyObject.DeleteSubProperty`).
///
/// # Errors
/// [`Error`] if the COM call fails.
pub fn delete_sub_property(&self, lookup_string: &str, options: i32) -> Result<(), Error> {
self.dispatch.call(
property_object::DELETE_SUB_PROPERTY,
&[Value::Str(lookup_string.to_string()), Value::I32(options)],
)?;
Ok(())
}
/// Clones a sub-property by lookup path (`PropertyObject.Clone`).
///
/// # Errors
/// [`Error`] if the COM call fails or returns an unexpected type.
pub fn clone_property(&self, lookup_string: &str, options: i32) -> Result<Self, Error> {
let dispatch = self
.dispatch
.call(
property_object::CLONE,
&[Value::Str(lookup_string.to_string()), Value::I32(options)],
)?
.into_object()?;
Ok(Self::new(dispatch))
}
}