distillation_shortcut_unit/
string_parameter.rs

1use crate::shared_unit_data::*;
2use crate::distillation_shortcut_unit::DistillationShortcutUnit;
3use cobia::*;
4
5/// The StringParameter class implements a CAPE-OPEN 1.2 string parameter.
6///
7/// Parameters must implement the ICapeIdentification and ICapeParameter interfaces.
8/// In addition, string parameters must implement the ICapeStringParameter interface.
9///
10/// This implementation can be used for both input and output parameters.
11/// Input parameters can be set by the user, while output parameters are set by the unit operation.
12///
13
14#[cape_object_implementation(
15		interfaces = {
16			cape_open_1_2::ICapeIdentification,
17			cape_open_1_2::ICapeParameter,
18			cape_open_1_2::ICapeStringParameter,
19		},
20		new_arguments = {
21			name,
22			description,
23			is_input,
24			shared_unit_data,
25			default_value,
26			possible_values,
27			exclusive,
28		}
29  )] 
30pub struct StringParameter {
31	/// The name of the parameter
32	pub(crate) name: CapeStringImpl,
33	/// The description of the parameter
34	description: CapeStringImpl,
35	/// Indicates whether this parameter is an input (true) or an output (false)
36	is_input : bool,
37	/// Shared data for the unit, containing unit-specific information
38	shared_unit_data: SharedUnitDataRef, 
39	/// The current value of the parameter
40	pub(crate) value : CapeStringImpl,
41	/// Default value of the parameter
42	default_value : CapeStringImpl,
43	/// List of allowed values for the parameter
44	possible_values : Option<CapeArrayStringVec>,
45	/// Exclusive implies only values in the list are allowed.
46	exclusive : bool,
47	/// Validation status of the parameter
48	validation_status: cape_open_1_2::CapeValidationStatus,
49}
50
51impl StringParameter {
52
53	pub fn new(name: CapeStringImpl, description: CapeStringImpl, is_input: bool, shared_unit_data: SharedUnitDataRef, default_value: CapeStringImpl, possible_values: Option<CapeArrayStringVec>, exclusive: bool) -> Self {
54		Self {
55			cobia_object_data:std::default::Default::default(), //initialization of this generated field is needed; can always be set to Default::default()
56			name,
57			description,
58			is_input,
59			shared_unit_data,
60			value:default_value.clone(),
61			default_value,
62			possible_values,
63			exclusive,
64			validation_status:cape_open_1_2::CapeValidationStatus::CapeNotValidated,
65		}
66	}
67
68	/// Replace the list of possible values with a new list.
69	///
70	/// # Arguments:
71	/// * `possible_values` - An `Option<CapeArrayStringVec>` containing the new list of possible values.
72	/// If `None`, the parameter will not have any possible values.	
73
74	pub fn set_possible_values(&mut self, possible_values: Option<&CapeArrayStringVec>) {
75		//check same
76		match possible_values {
77			Some(new_values) => 
78				{match self.possible_values.as_ref() {
79						Some(current_values) => if current_values == new_values { return; }, //no change, so return
80						None => {} //current is None, continue
81					}
82					let mut new_options=CapeArrayStringVec::new();
83					new_options.set(new_values).unwrap();
84					self.possible_values = Some(new_options);
85				},
86			None => 
87				{if self.possible_values.is_none() { return; } //no change, so return
88				 self.possible_values=None;
89				}
90		}
91		//invalidate the parameter, as the possible values have changed
92		let mut shared=self.shared_unit_data.borrow_mut();
93		shared.dirty=true;
94		shared.validation_status=cape_open_1_2::CapeValidationStatus::CapeNotValidated;
95		self.validation_status=cape_open_1_2::CapeValidationStatus::CapeNotValidated;
96	}
97
98	/// Get value by reference
99	///
100	/// # Returns:
101	/// * A reference to the current value of the parameter as a `CapeStringImpl`.
102	
103	pub fn value(&self) -> &CapeStringImpl {
104		&self.value
105	}
106
107}
108
109impl std::fmt::Display for StringParameter {
110
111	/// Format the StringParameter as a string for display purposes.
112	///
113	/// The std::fmt::Display interface is used when generating the 
114	/// source name of the object that raises an error.
115	///
116	/// # Arguments:
117	/// * `f` - A mutable reference to a `std::fmt::Formatter` where the formatted string will be written.
118	///
119	/// # Returns:
120	/// * A `std::fmt::Result` indicating the success or failure of the formatting operation.
121
122	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
123        write!(f,"Parameter '{}' of {} unit '{}'",self.name, DistillationShortcutUnit::NAME, self.shared_unit_data.borrow().name)
124    }
125
126}
127
128impl cape_open_1_2::ICapeIdentification for StringParameter {
129
130	/// Get the name of the component.
131	///
132	/// # Arguments:
133	/// * `name` - A mutable reference to a `CapeStringOut` where the name will be set.
134	///
135	/// # Returns:
136	/// * A `Result` indicating success or failure. If successful, the name is set in `name`.
137
138	fn get_component_name(&mut self,name:&mut CapeStringOut) -> Result<(), COBIAError> {
139		name.set(&self.name)?;
140		Ok(())
141	}
142
143	/// Get the description of the component.
144	///
145	/// # Arguments:
146	/// * `description` - A mutable reference to a `CapeStringOut` where the description will be set.
147	///
148	/// # Returns:
149	/// * A `Result` indicating success or failure. If successful, the description is set in `description`.
150
151	fn get_component_description(&mut self,description:&mut CapeStringOut) -> Result<(), COBIAError> {
152		description.set(&self.description)?;
153		Ok(())
154	}
155
156	/// Set the name of the component.
157	///
158	/// This method is not allowed for this parameter implementation and will return an error.
159
160	fn set_component_name(&mut self, _name: &CapeStringIn) -> Result<(), COBIAError> {
161		Err(cobia::COBIAError::Code(cobia::COBIAERR_DENIED))
162	}
163
164	/// Set the description of the component.
165	///
166	/// This method is not allowed for this parameter implementation and will return an error.
167
168	fn set_component_description(&mut self, _desc: &CapeStringIn) -> Result<(), COBIAError> {
169		Err(cobia::COBIAError::Code(cobia::COBIAERR_DENIED))
170	}
171}
172
173impl cape_open_1_2::ICapeParameter for StringParameter {
174
175	/// Get the validation status of the parameter.
176	///
177	/// # Returns:
178	/// * A `Result` containing the validation status, which is always `CapeValid` for this implementation.
179
180    fn get_val_status(&mut self) -> Result<cape_open_1_2::CapeValidationStatus,COBIAError> {
181		Ok(self.validation_status)
182    }
183
184	/// Get the mode of the parameter.
185	///
186	/// # Returns:
187	/// * A `Result` containing the mode of the parameter, which is either `CapeInput` or `CapeOutput` based on the `is_input` field.
188
189    fn get_mode(&mut self) -> Result<cape_open_1_2::CapeParamMode,COBIAError> {
190        if self.is_input {
191			Ok(cape_open_1_2::CapeParamMode::CapeInput)
192		} else {
193			Ok(cape_open_1_2::CapeParamMode::CapeOutput)
194		}
195    }
196
197	/// Get the type of the parameter.
198	///
199	/// # Returns:
200	/// * A `Result` containing the type of the parameter, which is always `CapeParameterString` for this implementation.
201
202    fn get_type(&mut self) -> Result<cape_open_1_2::CapeParamType,COBIAError> {
203        Ok(cape_open_1_2::CapeParamType::CapeParameterString)
204    }
205
206	/// Validate the parameter.
207	///
208	/// Call the value validation method to check if the parameter value is valid.
209	/// 
210	/// # Arguments:
211	/// * `message` - A mutable reference to a `CapeStringOut` where any validation error messages will be set.
212	///
213	/// # Returns:
214	/// * A `Result` containing a `CapeBoolean` indicating whether the parameter is valid or not. If the input value is not specified, it returns false and sets an error message.
215
216    fn validate(&mut self,message:&mut CapeStringOut) -> Result<CapeBoolean,COBIAError> {
217		match cape_open_1_2::ICapeStringParameter::validate(self,
218			&CapeStringInFromProvider::from(&self.value).as_cape_string_in(), //this is how any implementation of CapeStringInProvider can be converted to a CapeStringIn
219			message) {
220			Ok(valid) => {
221				self.validation_status=if valid!=0 {cape_open_1_2::CapeValidationStatus::CapeValid} else {cape_open_1_2::CapeValidationStatus::CapeInvalid};
222				Ok(valid)
223			},
224			Err(e) => Err(e),
225		}
226	}
227
228	/// Reset the parameter to its default value.
229	///
230	/// This method sets the value of the parameter back to its default value and marks the unit as dirty.
231	/// It also resets the validation status to `CapeNotValidated`.
232	///
233	/// # Returns:
234	/// * A `Result` indicating success or failure. If successful, the value is reset to the default value.
235
236    fn reset(&mut self) -> Result<(),COBIAError> {
237		if self.default_value.is_empty() {
238			return Err(cobia::COBIAError::Message("Default value is not available".into()));
239		}
240		if self.value!=self.default_value {
241			self.value.set(&self.default_value); 
242			let mut shared=self.shared_unit_data.borrow_mut();
243			shared.dirty=true;
244			shared.validation_status=cape_open_1_2::CapeValidationStatus::CapeNotValidated;
245			self.validation_status=cape_open_1_2::CapeValidationStatus::CapeNotValidated;
246		}
247		Ok(())
248	}
249}
250
251impl cape_open_1_2::ICapeStringParameter for StringParameter {
252
253	/// Get the current value of the string parameter.
254	///
255	/// # Arguments:
256	/// * `value` - A mutable reference to a `CapeStringOut` where the current value will be set.
257	///
258	/// # Returns:
259	/// * A `Result` indicating success or failure. If successful, the current value is set in `value`.
260
261    fn get_value(&mut self,value:&mut CapeStringOut) -> Result<(),COBIAError> {
262        value.set(&self.value)?;
263		Ok(())
264    }
265
266	/// Set the value of the string parameter.
267	///
268	/// The current value cannot be set for an output parameter.
269	///
270	/// # Arguments:
271	/// * `value` - A reference to a `CapeStringIn` containing the new value to be set.
272	///
273	/// # Returns:
274	/// * A `Result` indicating success or failure. If successful, the value is set and the unit is marked as dirty.
275
276    fn set_value(&mut self,value:&CapeStringIn) -> Result<(),COBIAError> {
277        if !self.is_input {
278			return Err(cobia::COBIAError::Code(cobia::COBIAERR_DENIED));
279		}
280		if *value!=self.value {
281			self.value.set(value); //set the value, which is a CapeStringImpl
282			let mut shared=self.shared_unit_data.borrow_mut();
283			shared.dirty=true;
284			shared.validation_status=cape_open_1_2::CapeValidationStatus::CapeNotValidated;
285			self.validation_status=cape_open_1_2::CapeValidationStatus::CapeNotValidated;
286		}
287		Ok(())
288    }
289
290	/// Get the default value of the string parameter.
291	///
292	/// # Arguments:
293	/// * `default_value` - A mutable reference to a `CapeStringOut` where the default value will be set.
294	///
295	/// # Returns:
296	/// * A `Result` indicating success or failure. If successful, the default value is set in `default_value`.
297
298    fn get_default_value(&mut self,default_value:&mut CapeStringOut) -> Result<(),COBIAError> {
299        if self.default_value.is_empty() {
300			return Err(cobia::COBIAError::Message("Default value is not available".into()));
301		}
302		default_value.set(&self.default_value)?; //set the default value
303		Ok(())
304    }
305
306	/// Get the list of possible values for the string parameter.
307	///
308	/// # Arguments:
309	/// * `option_names` - A mutable reference to a `CapeArrayStringOut` where the list of possible values will be set.
310	///
311	/// # Returns:
312	/// * A `Result` indicating success or failure. If successful, the list of possible values is set in `option_names`.
313
314    fn get_option_list(&mut self,option_names:&mut CapeArrayStringOut) -> Result<(),COBIAError> {
315        match self.possible_values {
316			Some(ref values) => {
317				option_names.set(values)?; //set the possible values
318				Ok(())
319			},
320			None => Err(cobia::COBIAError::Message("No options available".into())), //no possible values available
321		}
322    }
323
324	/// Check whether the value is restricted to the list of possible values.
325	///
326	/// # Returns:
327	/// * A `Result` containing a `CapeBoolean` indicating whether the value is restricted to the list of possible values.
328
329    fn get_restricted_to_list(&mut self) -> Result<CapeBoolean,COBIAError> {
330        Ok(self.exclusive as CapeBoolean)
331    }
332
333	/// Validate the value of the string parameter.
334	///
335	/// This method checks whether the specified value is suitable for the parameter.
336	///
337	/// # Arguments:
338	/// * `value` - A reference to a `CapeStringIn` containing the value to be validated.
339	/// * `message` - A mutable reference to a `CapeStringOut` where any validation error messages will be set.
340	///
341	/// # Returns:
342	/// * A `Result` containing a `CapeBoolean` indicating whether the value is valid or not. If the input value is not specified, it returns false and sets an error message.
343
344    fn validate(&mut self,value:&CapeStringIn,message:&mut CapeStringOut) -> Result<CapeBoolean,COBIAError> {
345        if self.is_input && self.value.is_empty() {
346			message.set_string("Value must be specified")?;
347			Ok(false as CapeBoolean)
348		} else if self.exclusive {
349			let mut in_list=false;
350			if let Some(possible_values) = &self.possible_values {
351				for value in possible_values.iter() {
352					if *value == self.value {
353						in_list = true;
354						break;
355					}
356				}
357			}
358			if !in_list {
359				message.set_string(format!("Value \"{}\" is not in the list of allowed values",value))?;
360			}
361			Ok(in_list as CapeBoolean)
362		} else {
363			Ok(true as CapeBoolean)
364		}
365    }
366}