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
//! TestStand `WorkspaceObject` (`IWorkspaceObject`) wrapper.
use crate::Error;
use crate::dispids::workspace_object;
use rs_teststand_sys::{Dispatch, Value};
/// Safe wrapper for TestStand™ `WorkspaceObject` (`IWorkspaceObject`).
#[derive(Debug)]
pub struct WorkspaceObject {
dispatch: Box<dyn Dispatch>,
}
impl WorkspaceObject {
/// Creates a new `WorkspaceObject` wrapper around a COM dispatch seam.
pub(crate) fn new(dispatch: Box<dyn Dispatch>) -> Self {
Self { dispatch }
}
/// Reads display name (`WorkspaceObject.DisplayName`).
///
/// # Errors
/// [`Error`] if the COM call fails or returns an unexpected type.
pub fn display_name(&self) -> Result<String, Error> {
Ok(self
.dispatch
.get(workspace_object::DISPLAY_NAME)?
.into_string()?)
}
/// Writes display name (`WorkspaceObject.DisplayName`).
///
/// # Errors
/// [`Error`] if the COM call fails.
pub fn set_display_name(&self, value: &str) -> Result<(), Error> {
self.dispatch.put(
workspace_object::DISPLAY_NAME,
Value::Str(value.to_string()),
)?;
Ok(())
}
/// Reads relative path (`WorkspaceObject.Path`).
///
/// # Errors
/// [`Error`] if the COM call fails or returns an unexpected type.
pub fn path(&self) -> Result<String, Error> {
Ok(self.dispatch.get(workspace_object::PATH)?.into_string()?)
}
/// Writes relative path (`WorkspaceObject.Path`).
///
/// # Errors
/// [`Error`] if the COM call fails.
pub fn set_path(&self, value: &str) -> Result<(), Error> {
self.dispatch
.put(workspace_object::PATH, Value::Str(value.to_string()))?;
Ok(())
}
/// Reads file exists status (`WorkspaceObject.FileExists`).
///
/// # Errors
/// [`Error`] if the COM call fails or returns an unexpected type.
pub fn file_exists(&self) -> Result<bool, Error> {
Ok(self
.dispatch
.get(workspace_object::FILE_EXISTS)?
.as_bool()?)
}
/// Reads object type discriminant (`WorkspaceObject.ObjectType`).
///
/// # Errors
/// [`Error`] if the COM call fails or returns an unexpected type.
pub fn object_type(&self) -> Result<i32, Error> {
Ok(self.dispatch.get(workspace_object::OBJECT_TYPE)?.as_i32()?)
}
/// Reads number of contained child objects (`WorkspaceObject.NumContainedObjects`).
///
/// # Errors
/// [`Error`] if the COM call fails or returns an unexpected type.
pub fn num_contained_objects(&self) -> Result<i32, Error> {
Ok(self
.dispatch
.get(workspace_object::NUM_CONTAINED_OBJECTS)?
.as_i32()?)
}
/// Retrieves a contained child object by 0-based index (`WorkspaceObject.GetContainedObject`).
///
/// # Errors
/// [`Error`] if the COM call fails or returns an unexpected type.
pub fn get_contained_object(&self, index: i32) -> Result<Self, Error> {
let dispatch = self
.dispatch
.call(workspace_object::GET_CONTAINED_OBJECT, &[Value::I32(index)])?
.into_object()?;
Ok(Self::new(dispatch))
}
/// Reads absolute file path (`WorkspaceObject.GetAbsolutePath`).
///
/// # Errors
/// [`Error`] if the COM call fails or returns an unexpected type.
pub fn get_absolute_path(&self) -> Result<String, Error> {
Ok(self
.dispatch
.call(workspace_object::GET_ABSOLUTE_PATH, &[])?
.into_string()?)
}
/// Creates a new child file object (`WorkspaceObject.NewFile`).
///
/// # Errors
/// [`Error`] if the COM call fails or returns an unexpected type.
pub fn new_file(&self, path_string: &str) -> Result<Self, Error> {
let dispatch = self
.dispatch
.call(
workspace_object::NEW_FILE,
&[Value::Str(path_string.to_string())],
)?
.into_object()?;
Ok(Self::new(dispatch))
}
/// Creates a new child folder object (`WorkspaceObject.NewFolder`).
///
/// # Errors
/// [`Error`] if the COM call fails or returns an unexpected type.
pub fn new_folder(&self, folder_name: &str) -> Result<Self, Error> {
let dispatch = self
.dispatch
.call(
workspace_object::NEW_FOLDER,
&[Value::Str(folder_name.to_string())],
)?
.into_object()?;
Ok(Self::new(dispatch))
}
/// Removes a child object by index (`WorkspaceObject.RemoveObject`).
///
/// # Errors
/// [`Error`] if the COM call fails or returns an unexpected type.
pub fn remove_object(&self, index: i32) -> Result<Self, Error> {
let dispatch = self
.dispatch
.call(workspace_object::REMOVE_OBJECT, &[Value::I32(index)])?
.into_object()?;
Ok(Self::new(dispatch))
}
}
#[cfg(test)]
mod tests {
use super::WorkspaceObject;
use crate::Error;
use crate::dispids::workspace_object;
use rs_teststand_sys::{ComError, Value};
use std::collections::HashMap;
#[derive(Debug)]
struct FakeDispatch {
responses: HashMap<i32, Value>,
}
impl rs_teststand_sys::Dispatch for FakeDispatch {
fn get(&self, dispid: i32) -> Result<Value, ComError> {
self.responses.get(&dispid).map_or_else(
|| Err(ComError::hresult(0, "fake: unscripted dispid")),
|val| match val {
Value::Str(s) => Ok(Value::Str(s.clone())),
Value::Bool(b) => Ok(Value::Bool(*b)),
Value::I32(n) => Ok(Value::I32(*n)),
_ => Err(ComError::hresult(0, "fake")),
},
)
}
fn put(&self, _dispid: i32, _value: Value) -> Result<(), ComError> {
Err(ComError::hresult(0, "fake"))
}
fn call(&self, _dispid: i32, _args: &[Value]) -> Result<Value, ComError> {
Err(ComError::hresult(0, "fake"))
}
}
#[test]
fn display_name_reads_bstr_property() -> Result<(), Error> {
let fake = FakeDispatch {
responses: HashMap::from([(
workspace_object::DISPLAY_NAME,
Value::Str("MyFolder".to_string()),
)]),
};
let obj = WorkspaceObject::new(Box::new(fake));
assert_eq!(obj.display_name()?, "MyFolder");
Ok(())
}
#[test]
fn num_contained_objects_reads_i4_property() -> Result<(), Error> {
let fake = FakeDispatch {
responses: HashMap::from([(workspace_object::NUM_CONTAINED_OBJECTS, Value::I32(5))]),
};
let obj = WorkspaceObject::new(Box::new(fake));
assert_eq!(obj.num_contained_objects()?, 5);
Ok(())
}
#[test]
fn file_exists_reads_bool_property() -> Result<(), Error> {
let fake = FakeDispatch {
responses: HashMap::from([(workspace_object::FILE_EXISTS, Value::Bool(true))]),
};
let obj = WorkspaceObject::new(Box::new(fake));
assert!(obj.file_exists()?);
Ok(())
}
}