use rs_teststand_sys::{Dispatch, Value};
use crate::Error;
use crate::dispids::step;
use crate::property::PropertyObject;
#[derive(Debug)]
pub struct Step {
dispatch: Box<dyn Dispatch>,
}
impl Step {
pub(crate) fn new(dispatch: Box<dyn Dispatch>) -> Self {
Self { dispatch }
}
pub fn name(&self) -> Result<String, Error> {
Ok(self.dispatch.get(step::NAME)?.into_string()?)
}
pub fn set_name(&self, name: &str) -> Result<(), Error> {
self.dispatch.put(step::NAME, Value::Str(name.to_owned()))?;
Ok(())
}
pub fn precondition(&self) -> Result<String, Error> {
Ok(self.dispatch.get(step::PRECONDITION)?.into_string()?)
}
pub fn set_precondition(&self, expression: &str) -> Result<(), Error> {
self.dispatch
.put(step::PRECONDITION, Value::Str(expression.to_owned()))?;
Ok(())
}
pub fn run_mode(&self) -> Result<Option<crate::RunMode>, Error> {
let raw = self.dispatch.get(step::RUN_MODE)?.into_string()?;
Ok(crate::RunMode::from_value(&raw))
}
pub fn set_run_mode(&self, mode: crate::RunMode) -> Result<(), Error> {
self.dispatch
.put(step::RUN_MODE, Value::Str(mode.as_str().to_owned()))?;
Ok(())
}
pub fn adapter_key_name(&self) -> Result<Option<crate::AdapterKeyName>, Error> {
let raw = self.dispatch.get(step::ADAPTER_KEY_NAME)?.into_string()?;
Ok(crate::AdapterKeyName::from_key(&raw))
}
pub fn post_expression(&self) -> Result<String, Error> {
Ok(self.dispatch.get(step::POST_EXPRESSION)?.into_string()?)
}
pub fn set_post_expression(&self, expression: &str) -> Result<(), Error> {
self.dispatch
.put(step::POST_EXPRESSION, Value::Str(expression.to_owned()))?;
Ok(())
}
pub fn create_new_unique_step_id(&self) -> Result<(), Error> {
self.dispatch.call(step::CREATE_NEW_UNIQUE_STEP_ID, &[])?;
Ok(())
}
pub fn result_recording_option(&self) -> Result<crate::ResultRecordingOption, Error> {
crate::ResultRecordingOption::from_bits(
self.dispatch.get(step::RESULT_RECORDING_OPTION)?.as_i32()?,
)
}
pub fn set_result_recording_option(
&self,
option: crate::ResultRecordingOption,
) -> Result<(), Error> {
self.dispatch
.put(step::RESULT_RECORDING_OPTION, Value::I32(option as i32))?;
Ok(())
}
pub fn record_result(&self) -> Result<bool, Error> {
Ok(self.dispatch.get(step::RECORD_RESULT)?.as_bool()?)
}
pub fn set_record_result(&self, record: bool) -> Result<(), Error> {
self.dispatch
.put(step::RECORD_RESULT, Value::Bool(record))?;
Ok(())
}
pub fn as_property_object(&self) -> Result<PropertyObject, Error> {
Ok(PropertyObject::new(
self.dispatch
.call(step::AS_PROPERTY_OBJECT, &[])?
.into_object()?,
))
}
pub fn step_type(&self) -> Result<PropertyObject, Error> {
Ok(PropertyObject::new(
self.dispatch.get(step::STEP_TYPE)?.into_object()?,
))
}
pub(crate) fn duplicate_dispatch(&self) -> Option<Box<dyn Dispatch>> {
self.dispatch.duplicate()
}
}