Skip to main content

opcua_kit/ua/
mod.rs

1//! UA Binary values and service structures for the protocol roles provided by this crate.
2//!
3//! Use a session to translate numeric namespace indexes before persisting node identifiers.
4//! Use the typed read API for application values or inspect raw Variants for heterogeneous batches.
5//! A raw DateTime preserves the original signed ticks. A nullable UAString preserves
6//! the distinction between missing text and an explicitly empty string.
7//!
8//! ```
9//! use opcua_kit::ua::{BinaryDecodable, BinaryEncodable, ContextOwned, NodeId};
10//!
11//! let context = ContextOwned::default();
12//! let wire_id = NodeId::new(2, "Temperature");
13//! let mut bytes = Vec::new();
14//! wire_id.encode(&mut bytes, &context.context())?;
15//! let received = NodeId::decode(&mut bytes.as_slice(), &context.context())?;
16//! assert_eq!(received, wire_id);
17//! # Ok::<(), opcua_kit::ua::StatusCode>(())
18//! ```
19
20mod binary;
21pub(crate) use binary::BoundedBuffer;
22pub use binary::{BinaryDecodable, BinaryEncodable, Context, ContextOwned, DecodingOptions};
23mod scalar;
24pub use scalar::*;
25mod value;
26pub use value::*;
27
28/// Binary service or structure identifier in the standard namespace.
29pub trait MessageInfo {
30    fn type_id(&self) -> ObjectId;
31}
32
33macro_rules! ua_enum {
34    ($name:ident { $first:ident = $one:expr, $($member:ident = $number:expr,)* }) => {
35        #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
36        #[repr(i32)]
37        pub enum $name {
38            #[default]
39            $first = $one,
40            $($member = $number,)*
41        }
42        impl BinaryEncodable for $name {
43            fn encode<W: std::io::Write + ?Sized>(&self, w: &mut W, c: &Context<'_>) -> Result<(), StatusCode> {
44                (*self as i32).encode(w, c)
45            }
46        }
47        impl BinaryDecodable for $name {
48            fn decode<R: std::io::Read + ?Sized>(r: &mut R, c: &Context<'_>) -> Result<Self, StatusCode> {
49                match i32::decode(r, c)? {
50                    $one => Ok(Self::$first),
51                    $($number => Ok(Self::$member),)*
52                    _ => Err(StatusCode::BadDecodingError),
53                }
54            }
55        }
56        impl std::fmt::Display for $name {
57            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
58                write!(f, "{self:?}")
59            }
60        }
61    };
62}
63macro_rules! ua_struct {
64    ($name:ident { $($field:ident : $ty:ty,)* }) => {
65        #[derive(Clone, Debug, Default, PartialEq)]
66        pub struct $name { $(pub $field: $ty,)* }
67        impl BinaryEncodable for $name {
68            fn encode<W: std::io::Write + ?Sized>(&self,w: &mut W, c: &Context<'_>) -> Result<(), StatusCode> {
69                $(self.$field.encode(w, c)?;)*
70                Ok(())
71            }
72        }
73        impl BinaryDecodable for $name {
74            fn decode<R: std::io::Read + ?Sized>(r: &mut R, c: &Context<'_>) -> Result<Self, StatusCode> {
75                Ok(Self { $($field: <$ty>::decode(r, c)?,)* })
76            }
77        }
78    };
79}
80macro_rules! object_ids {
81    ($($id:ident = $n:expr,)*) => {
82        #[allow(non_camel_case_types)]
83        #[repr(u32)]
84        #[derive(Copy,Clone,Debug,PartialEq,Eq)]
85        pub enum ObjectId { $($id=$n,)* }
86        impl TryFrom<u32> for ObjectId {
87            type Error = StatusCode;
88            fn try_from(id: u32) -> Result<Self, StatusCode> {
89                match id {
90                    $($n => Ok(Self::$id),)*
91                    _ => Err(StatusCode::BadServiceUnsupported),
92                }
93            }
94        }
95    };
96}
97macro_rules! message {
98    ($name:ident, $id:ident) => {
99        impl MessageInfo for $name {
100            fn type_id(&self) -> ObjectId {
101                ObjectId::$id
102            }
103        }
104    };
105}
106mod generated;
107use generated::decode_extension;
108pub use generated::*;
109
110/// Opaque server-issued cursor for BrowseNext and HistoryRead.
111pub type ContinuationPoint = ByteString;
112
113impl ResponseHeader {
114    pub fn new_good(request_handle: u32) -> Self {
115        Self::new_service_result(request_handle, StatusCode::Good)
116    }
117    pub fn new_service_result(request_handle: u32, service_result: StatusCode) -> Self {
118        Self {
119            timestamp: DateTime::now(),
120            request_handle,
121            service_result,
122            ..Default::default()
123        }
124    }
125}