instruction_serialiser/node_wrapper.rs
1use std::collections::HashMap;
2use crate::calculation_error::CalculationError;
3use crate::parameter::Parameter;
4use crate::serialisation_error::SerialisationError;
5
6pub trait NodeWrapper<T> {
7 /// This method triggers a calculation based on the instructions given by the nodes underlying
8 /// the [NodeWrapper<T>]. If the instruction expects parameters they need to be passed as a
9 /// [std::collections::HashMap] wrapped inside an option. The [std::collections::HashMap]
10 /// expects variable names as keys and related variable values as [std::collections::HashMap]
11 /// values.
12 /// A calculation result of type [T] is returned in case of success, a calculation error of type
13 /// [crate::calculation_error::CalculationError] otherwise.
14 fn calculate(&self, parameters: Option<&HashMap<&str, Parameter>>) -> Result<T, CalculationError> {
15 let new_params;
16 let params = match parameters {
17 None => {
18 new_params = HashMap::new();
19 &new_params
20 },
21 Some(parameters) => parameters,
22 };
23 self.calculate_wrapped_nodes(params)
24 }
25 /// This method triggers a calculation based on the instructions given by the nodes underlying
26 /// the [NodeWrapper<T>]. If the instruction expects parameters they need to be passed as a
27 /// [std::collections::HashMap]. an empty
28 /// [std::collections::HashMap] needs to be passed. The [std::collections::HashMap]
29 /// expects variable names as keys and related variable values as [std::collections::HashMap]
30 /// values. In contrast to [NodeWrapper<T>::calculate], an empty [std::collections::HashMap]
31 /// needs to be passed to this method if the instruction does not expect any parameters.
32 /// A calculation result of type [T] is returned in case of success, a calculation error of type
33 /// [crate::calculation_error::CalculationError] otherwise.
34 fn calculate_wrapped_nodes(&self, parameters: &HashMap<&str, Parameter>) -> Result<T, CalculationError>;
35 /// This method serialises the whole instruction covered by this [NodeWrapper] into a binary
36 /// format that can be deserialised into a [crate::InstructionWrapper].
37 fn serialise(self) -> Result<Vec<u8>, SerialisationError>;
38}