Skip to main content

opcua_types/
add_node_attributes.rs

1use crate::match_extension_object_owned;
2
3use super::{
4    extension_object::ExtensionObject, status_code::StatusCode, DataTypeAttributes,
5    GenericAttributes, MethodAttributes, ObjectAttributes, ObjectTypeAttributes,
6    ReferenceTypeAttributes, VariableAttributes, VariableTypeAttributes, ViewAttributes,
7};
8
9#[derive(Clone, Debug)]
10/// Enum over different attribute collections for AddNodes.
11pub enum AddNodeAttributes {
12    /// Object attributes.
13    Object(ObjectAttributes),
14    /// Variable attributes.
15    Variable(VariableAttributes),
16    /// Method attributes.
17    Method(MethodAttributes),
18    /// ObjectType attributes.
19    ObjectType(ObjectTypeAttributes),
20    /// VariableType attributes.
21    VariableType(VariableTypeAttributes),
22    /// ReferenceType attributes.
23    ReferenceType(ReferenceTypeAttributes),
24    /// DataType attributes.
25    DataType(DataTypeAttributes),
26    /// View attributes.
27    View(ViewAttributes),
28    /// Generic attributes.
29    Generic(GenericAttributes),
30    /// No extra attributes.
31    None,
32}
33
34impl AddNodeAttributes {
35    /// Get Self from an extension object body.
36    pub fn from_extension_object(obj: ExtensionObject) -> Result<Self, StatusCode> {
37        if obj.is_null() {
38            return Ok(Self::None);
39        }
40        match_extension_object_owned!(obj,
41            v: ObjectAttributes => Ok(Self::Object(v)),
42            v: MethodAttributes => Ok(Self::Method(v)),
43            v: VariableAttributes => Ok(Self::Variable(v)),
44            v: ViewAttributes => Ok(Self::View(v)),
45            v: ObjectTypeAttributes => Ok(Self::ObjectType(v)),
46            v: VariableTypeAttributes => Ok(Self::VariableType(v)),
47            v: ReferenceTypeAttributes => Ok(Self::ReferenceType(v)),
48            v: DataTypeAttributes => Ok(Self::DataType(v)),
49            v: GenericAttributes => Ok(Self::Generic(v)),
50            _ => Err(StatusCode::BadNodeAttributesInvalid),
51        )
52    }
53
54    /// Convert this into an extension object.
55    pub fn as_extension_object(&self) -> ExtensionObject {
56        match self.clone() {
57            AddNodeAttributes::Object(o) => ExtensionObject::from_message(o),
58            AddNodeAttributes::Variable(o) => ExtensionObject::from_message(o),
59            AddNodeAttributes::Method(o) => ExtensionObject::from_message(o),
60            AddNodeAttributes::ObjectType(o) => ExtensionObject::from_message(o),
61            AddNodeAttributes::VariableType(o) => ExtensionObject::from_message(o),
62            AddNodeAttributes::ReferenceType(o) => ExtensionObject::from_message(o),
63            AddNodeAttributes::DataType(o) => ExtensionObject::from_message(o),
64            AddNodeAttributes::View(o) => ExtensionObject::from_message(o),
65            AddNodeAttributes::Generic(o) => ExtensionObject::from_message(o),
66            AddNodeAttributes::None => ExtensionObject::null(),
67        }
68    }
69}