distillation_shortcut_unit/
material_port.rs1use crate::shared_unit_data::*;
2use crate::distillation_shortcut_unit::DistillationShortcutUnit;
3use cobia::*;
4
5#[cape_object_implementation(
11 interfaces = {
12 cape_open_1_2::ICapeIdentification,
13 cape_open_1_2::ICapeUnitPort,
14 },
15 create_arguments = {
16 name,
17 description,
18 is_inlet,
19 shared_unit_data
20 }
21 )]
22pub struct MaterialPort {
23 name: CapeStringImpl,
25 description: CapeStringImpl,
27 connected_object: Option<cape_open_1_2::CapeThermoMaterial>,
29 is_inlet : bool,
31 shared_unit_data: SharedUnitDataRef,
33}
34
35impl MaterialPort {
36
37 pub(crate) fn get_connected_material(&self) -> Option<cape_open_1_2::CapeThermoMaterial> {
43 self.connected_object.clone()
44 }
45
46 pub(crate) fn get_name(&self) -> &CapeStringImpl {
52 &self.name
53 }
54
55}
56
57impl std::fmt::Display for MaterialPort {
58
59 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
71 write!(f,"Port '{}' of {} unit '{}'",self.name, DistillationShortcutUnit::NAME, self.shared_unit_data.borrow().name)
72 }
73
74}
75
76impl cape_open_1_2::ICapeIdentification for MaterialPort {
77
78 fn get_component_name(&mut self,name:&mut CapeStringOut) -> Result<(), COBIAError> {
87 name.set(&self.name)?;
88 Ok(())
89 }
90
91 fn get_component_description(&mut self,description:&mut CapeStringOut) -> Result<(), COBIAError> {
100 description.set(&self.description)?;
101 Ok(())
102 }
103
104 fn set_component_name(&mut self, _name: &CapeStringIn) -> Result<(), COBIAError> {
109 Err(cobia::COBIAError::Code(cobia::COBIAERR_DENIED))
110 }
111
112 fn set_component_description(&mut self, _desc: &CapeStringIn) -> Result<(), COBIAError> {
117 Err(cobia::COBIAError::Code(cobia::COBIAERR_DENIED))
118 }
119}
120
121impl cape_open_1_2::ICapeUnitPort for MaterialPort {
122
123 fn get_port_type(&mut self) -> Result<cape_open_1_2::CapePortType,COBIAError> {
129 Ok(cape_open_1_2::CapePortType::CapeMaterial)
130 }
131
132 fn get_direction(&mut self) -> Result<cape_open_1_2::CapePortDirection,COBIAError> {
138 Ok(
139 match self.is_inlet {
140 true => cape_open_1_2::CapePortDirection::CapeInlet,
141 false => cape_open_1_2::CapePortDirection::CapeOutlet,
142 }
143 )
144 }
145
146 fn get_connected_object(&mut self) -> Result<cobia::CapeObject,COBIAError> {
152 match self.connected_object {
153 Some(ref connected_object) => {
154 match cobia::CapeObject::from_object(connected_object) {
155 Ok(object) => {
156 Ok(object)
157 },
158 Err(_) => {
159 Err(COBIAError::Message("Object does not expose ICapeInterface".into()))
160 }
161 }
162 },
163 None => {
164 Err(COBIAError::Code(COBIAERR_NOSUCHITEM))
165 }
166 }
167 }
168
169 fn connect(&mut self,object_to_connect:cobia::CapeObject) -> Result<(),COBIAError> {
180 let material_object = cape_open_1_2::CapeThermoMaterial::from_object(&object_to_connect);
181 match material_object {
182 Ok(material_object) => {
183 self.connected_object=Some(material_object);
184 self.shared_unit_data.borrow_mut().validation_status = cape_open_1_2::CapeValidationStatus::CapeNotValidated;
185 Ok(())
186 },
187 Err(e) => Err(e),
188 }
189 }
190
191 fn disconnect(&mut self) -> Result<(),COBIAError> {
197 self.connected_object=None;
198 self.shared_unit_data.borrow_mut().validation_status = cape_open_1_2::CapeValidationStatus::CapeNotValidated;
199 Ok(())
200 }
201}