use std::cmp::Ordering;
use std::io::{Read, Write};
use crate::{
encoding::{BinaryDecodable, BinaryEncodable, EncodingResult},
localized_text::LocalizedText,
node_id::NodeId,
string::UAString,
write_u32, Context, DataTypeId, Error, MessageInfo, ObjectId,
};
#[allow(unused)]
mod opcua {
pub(super) use crate as types;
}
#[derive(Clone, Debug, PartialEq, Default, crate::UaNullable)]
#[cfg_attr(feature = "json", derive(crate::JsonEncodable, crate::JsonDecodable))]
#[cfg_attr(
feature = "xml",
derive(crate::XmlEncodable, crate::XmlDecodable, crate::XmlType)
)]
pub struct Argument {
pub name: UAString,
pub data_type: NodeId,
pub value_rank: i32,
pub array_dimensions: Option<Vec<u32>>,
pub description: LocalizedText,
}
impl MessageInfo for Argument {
fn type_id(&self) -> ObjectId {
ObjectId::Argument_Encoding_DefaultBinary
}
fn json_type_id(&self) -> ObjectId {
ObjectId::Argument_Encoding_DefaultJson
}
fn xml_type_id(&self) -> ObjectId {
ObjectId::Argument_Encoding_DefaultXml
}
fn data_type_id(&self) -> crate::DataTypeId {
DataTypeId::Argument
}
}
impl BinaryEncodable for Argument {
fn byte_len(&self, ctx: &crate::Context<'_>) -> usize {
let mut size = 0;
size += self.name.byte_len(ctx);
size += self.data_type.byte_len(ctx);
size += self.value_rank.byte_len(ctx);
size += self.array_dimensions.byte_len(ctx);
size += self.description.byte_len(ctx);
size
}
fn encode<S: Write + ?Sized>(&self, stream: &mut S, ctx: &Context<'_>) -> EncodingResult<()> {
self.name.encode(stream, ctx)?;
self.data_type.encode(stream, ctx)?;
self.value_rank.encode(stream, ctx)?;
if self.value_rank > 0 {
if let Some(ref array_dimensions) = self.array_dimensions {
if self.value_rank as usize != array_dimensions.len() {
return Err(Error::encoding(
format!("The array dimensions {} of the Argument should match value rank {} and they don't", array_dimensions.len(), self.value_rank)));
}
self.array_dimensions.encode(stream, ctx)?;
} else {
return Err(Error::encoding(format!("The array dimensions are expected in the Argument matching value rank {} and they aren't", self.value_rank)));
}
} else {
write_u32(stream, 0u32)?;
}
self.description.encode(stream, ctx)?;
Ok(())
}
}
impl BinaryDecodable for Argument {
fn decode<S: Read + ?Sized>(stream: &mut S, ctx: &Context<'_>) -> EncodingResult<Self> {
let name = UAString::decode(stream, ctx)?;
let data_type = NodeId::decode(stream, ctx)?;
let value_rank = i32::decode(stream, ctx)?;
let mut array_dimensions: Option<Vec<u32>> = BinaryDecodable::decode(stream, ctx)?;
if value_rank > 0 {
if let Some(array_dimensions) = array_dimensions.as_mut() {
let expected_rank = value_rank as usize;
match array_dimensions.len().cmp(&expected_rank) {
Ordering::Less => {
array_dimensions.resize(expected_rank, 0);
}
Ordering::Equal => {}
Ordering::Greater => {
return Err(Error::decoding(format!("The array dimensions {} of the Argument should match value rank {} and they don't", array_dimensions.len(), value_rank)));
}
}
}
}
let description = LocalizedText::decode(stream, ctx)?;
Ok(Argument {
name,
data_type,
value_rank,
array_dimensions,
description,
})
}
}