use rs_teststand_sys::{Dispatch, Value};
use crate::Error;
use crate::dispids::expression;
#[derive(Debug)]
pub struct Expression {
dispatch: Box<dyn Dispatch>,
}
impl Expression {
pub(crate) fn new(dispatch: Box<dyn Dispatch>) -> Self {
Self { dispatch }
}
pub fn text(&self) -> Result<String, Error> {
Ok(self.dispatch.get(expression::TEXT)?.into_string()?)
}
pub fn set_text(&self, text: &str) -> Result<(), Error> {
self.dispatch
.put(expression::TEXT, Value::Str(text.to_owned()))?;
Ok(())
}
pub fn num_tokens(&self) -> Result<i32, Error> {
Ok(self.dispatch.get(expression::NUM_TOKENS)?.as_i32()?)
}
pub fn evaluate(
&self,
evaluation_context: &crate::PropertyObject,
options: i32,
) -> Result<crate::PropertyObject, Error> {
let context = evaluation_context
.duplicate_dispatch()
.ok_or(Error::UnexpectedType {
expected: "a live evaluation context",
actual: "a test fake with no COM identity",
})?;
Ok(crate::PropertyObject::new(
self.dispatch
.call(
expression::EVALUATE,
&[Value::Object(context), Value::I32(options)],
)?
.into_object()?,
))
}
}