use super::errors::GCMockingError;
use super::gateway_instance_ctx::{GCGatewayMockInstanceCtx, GetLastDatapointValueIntercept, LogIntercept, PublishDatapointValueIntercept};
use super::gateway_mock::GCGatewayMock;
use crate::{GCDatapoint, GCPluginInstance};
use serde_json::Value;
pub struct GCGatewayMockBuilder {
pub(super) own_datapoints: Vec<GCDatapoint>,
pub(super) subscribed_datapoints: Vec<GCDatapoint>,
pub(super) core_ctx: Box<GCGatewayMockInstanceCtx>,
pub(super) config: Box<Value>,
pub(super) log_level: gc_abi::GCLogLevel,
}
impl Default for GCGatewayMockBuilder {
fn default() -> Self {
Self::new()
}
}
impl GCGatewayMockBuilder {
pub fn new() -> Self {
Self {
own_datapoints: Vec::new(),
subscribed_datapoints: Vec::new(),
core_ctx: GCGatewayMockInstanceCtx::new(),
config: Box::new(serde_json::Value::Null),
log_level: gc_abi::GCLogLevel::DEBUG,
}
}
pub fn add_own_datapoint(&mut self, datapoint: GCDatapoint) -> &mut Self {
self.own_datapoints.push(datapoint);
self
}
pub fn add_subscribed_datapoint(&mut self, datapoint: GCDatapoint) -> &mut Self {
self.subscribed_datapoints.push(datapoint);
self
}
pub fn set_log_level(&mut self, level: gc_abi::GCLogLevel) -> &mut Self {
self.log_level = level;
self
}
pub fn set_config(&mut self, config: String) -> Result<&mut Self, GCMockingError> {
self.config = Box::new(serde_json::from_str(&config)?);
Ok(self)
}
pub fn set_publish_datapoint_value_callback(&mut self, callback: Box<PublishDatapointValueIntercept>) -> &mut Self {
self.core_ctx.publish_datapoint_callback = callback;
self
}
pub fn set_log_callback(&mut self, callback: Box<LogIntercept>) -> &mut Self {
self.core_ctx.log_callback = callback;
self
}
pub fn set_get_last_datapoint_value_callback(&mut self, callback: Box<GetLastDatapointValueIntercept>) -> &mut Self {
self.core_ctx.get_last_datapoint_callback = callback;
self
}
pub fn start<'a, T: GCPluginInstance<'a>>(self) -> Result<GCGatewayMock<'a, T>, GCMockingError> {
let mut mock = GCGatewayMock::new(self)?;
mock.start()?;
Ok(mock)
}
pub fn build<'a, T: GCPluginInstance<'a>>(self) -> Result<GCGatewayMock<'a, T>, GCMockingError> {
GCGatewayMock::new(self)
}
}