Skip to main content

opcua_types/
attribute.rs

1// OPCUA for Rust
2// SPDX-License-Identifier: MPL-2.0
3// Copyright (C) 2017-2024 Adam Lock
4
5//! The [`AttributeId`] enum, identifying OPC UA node attributes by a numeric value.
6//!
7//! Defined in Part 4, Figure B.7
8
9// Attributes sometimes required and sometimes optional
10
11use std::{error::Error, fmt};
12
13use tracing::debug;
14
15#[derive(Debug)]
16/// Error returned when working with an Attribute ID.
17pub struct AttributeIdError;
18
19impl fmt::Display for AttributeIdError {
20    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21        write!(f, "AttributeIdError")
22    }
23}
24
25impl Error for AttributeIdError {}
26
27#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq)]
28#[repr(u32)]
29/// Node attribute ID, from the OPC UA standard.
30pub enum AttributeId {
31    /// Node ID.
32    NodeId = 1,
33    /// Node class.
34    NodeClass = 2,
35    /// Browse name.
36    BrowseName = 3,
37    /// Display name.
38    DisplayName = 4,
39    /// Description.
40    Description = 5,
41    /// Write mask.
42    WriteMask = 6,
43    /// User write mask.
44    UserWriteMask = 7,
45    /// Is abstract.
46    IsAbstract = 8,
47    /// Is symmetric, applies to reference types.
48    Symmetric = 9,
49    /// Inverse name of reference type.
50    InverseName = 10,
51    /// For views, contains no loops.
52    ContainsNoLoops = 11,
53    /// Whether this object can produce events.
54    EventNotifier = 12,
55    /// Variable value.
56    Value = 13,
57    /// Data type.
58    DataType = 14,
59    /// Variable value rank.
60    ValueRank = 15,
61    /// Variable array dimensions.
62    ArrayDimensions = 16,
63    /// Variable access level.
64    AccessLevel = 17,
65    /// Variable user access level.
66    UserAccessLevel = 18,
67    /// Variable minimum sampling interval.
68    MinimumSamplingInterval = 19,
69    /// Whether a variable stores history.
70    Historizing = 20,
71    /// Whether this method is executable.
72    Executable = 21,
73    /// Whether this method is executable by the current user.
74    UserExecutable = 22,
75    /// Data type definition.
76    DataTypeDefinition = 23,
77    /// Role permissions.
78    RolePermissions = 24,
79    /// User role permissions.
80    UserRolePermissions = 25,
81    /// Access restrictions.
82    AccessRestrictions = 26,
83    /// Access level extension.
84    AccessLevelEx = 27,
85}
86
87impl AttributeId {
88    /// Try to get this attribute ID from a 32 bit integer.
89    pub fn from_u32(attribute_id: u32) -> Result<AttributeId, AttributeIdError> {
90        let attribute_id = match attribute_id {
91            1 => AttributeId::NodeId,
92            2 => AttributeId::NodeClass,
93            3 => AttributeId::BrowseName,
94            4 => AttributeId::DisplayName,
95            5 => AttributeId::Description,
96            6 => AttributeId::WriteMask,
97            7 => AttributeId::UserWriteMask,
98            8 => AttributeId::IsAbstract,
99            9 => AttributeId::Symmetric,
100            10 => AttributeId::InverseName,
101            11 => AttributeId::ContainsNoLoops,
102            12 => AttributeId::EventNotifier,
103            13 => AttributeId::Value,
104            14 => AttributeId::DataType,
105            15 => AttributeId::ValueRank,
106            16 => AttributeId::ArrayDimensions,
107            17 => AttributeId::AccessLevel,
108            18 => AttributeId::UserAccessLevel,
109            19 => AttributeId::MinimumSamplingInterval,
110            20 => AttributeId::Historizing,
111            21 => AttributeId::Executable,
112            22 => AttributeId::UserExecutable,
113            23 => AttributeId::DataTypeDefinition,
114            24 => AttributeId::RolePermissions,
115            25 => AttributeId::UserRolePermissions,
116            26 => AttributeId::AccessRestrictions,
117            27 => AttributeId::AccessLevelEx,
118            _ => {
119                debug!("Invalid attribute id {}", attribute_id);
120                return Err(AttributeIdError);
121            }
122        };
123        Ok(attribute_id)
124    }
125}