opcua_types/
basic_types.rs

1// OPCUA for Rust
2// SPDX-License-Identifier: MPL-2.0
3// Copyright (C) 2017-2024 Adam Lock
4
5//! Contains definitions of the simple OPC UA scalar types.
6use std::io::{Read, Write};
7
8use crate::encoding::*;
9
10// OPC UA Part 6 - Mappings 1.03 Specification
11
12// Standard UA types onto Rust types:
13
14// Boolean  -> bool
15// SByte    -> i8
16// Byte     -> u8
17// Int16    -> i16
18// UInt16   -> u16
19// Int32    -> i32
20// UInt32   -> u32
21// Int64    -> i64
22// UInt64   -> u64
23// Float    -> f32
24// Double   -> f64
25
26impl SimpleBinaryEncodable for bool {
27    fn byte_len(&self) -> usize {
28        1
29    }
30
31    fn encode<S: Write + ?Sized>(&self, stream: &mut S) -> EncodingResult<()> {
32        // 0, or 1 for true or false, single byte
33        write_u8(stream, if *self { 1 } else { 0 })
34    }
35}
36
37impl SimpleBinaryDecodable for bool {
38    fn decode<S: Read + ?Sized>(
39        stream: &mut S,
40        _decoding_options: &DecodingOptions,
41    ) -> EncodingResult<Self> {
42        Ok(read_u8(stream)? == 1)
43    }
44}
45
46impl SimpleBinaryEncodable for i8 {
47    fn byte_len(&self) -> usize {
48        1
49    }
50
51    fn encode<S: Write + ?Sized>(&self, stream: &mut S) -> EncodingResult<()> {
52        write_u8(stream, *self as u8)
53    }
54}
55
56impl SimpleBinaryDecodable for i8 {
57    fn decode<S: Read + ?Sized>(
58        stream: &mut S,
59        _decoding_options: &DecodingOptions,
60    ) -> EncodingResult<Self> {
61        Ok(read_u8(stream)? as i8)
62    }
63}
64
65/// An unsigned byt integer value between 0 and 255.
66impl SimpleBinaryEncodable for u8 {
67    fn byte_len(&self) -> usize {
68        1
69    }
70
71    fn encode<S: Write + ?Sized>(&self, stream: &mut S) -> EncodingResult<()> {
72        write_u8(stream, *self)
73    }
74}
75
76impl SimpleBinaryDecodable for u8 {
77    fn decode<S: Read + ?Sized>(
78        stream: &mut S,
79        _decoding_options: &DecodingOptions,
80    ) -> EncodingResult<Self> {
81        read_u8(stream)
82    }
83}
84
85/// A signed integer value between −32768 and 32767.
86impl SimpleBinaryEncodable for i16 {
87    fn byte_len(&self) -> usize {
88        2
89    }
90
91    fn encode<S: Write + ?Sized>(&self, stream: &mut S) -> EncodingResult<()> {
92        write_i16(stream, *self)
93    }
94}
95
96impl SimpleBinaryDecodable for i16 {
97    fn decode<S: Read + ?Sized>(
98        stream: &mut S,
99        _decoding_options: &DecodingOptions,
100    ) -> EncodingResult<Self> {
101        read_i16(stream)
102    }
103}
104
105/// An unsigned integer value between 0 and 65535.
106impl SimpleBinaryEncodable for u16 {
107    fn byte_len(&self) -> usize {
108        2
109    }
110
111    fn encode<S: Write + ?Sized>(&self, stream: &mut S) -> EncodingResult<()> {
112        write_u16(stream, *self)
113    }
114}
115
116impl SimpleBinaryDecodable for u16 {
117    fn decode<S: Read + ?Sized>(
118        stream: &mut S,
119        _decoding_options: &DecodingOptions,
120    ) -> EncodingResult<Self> {
121        read_u16(stream)
122    }
123}
124
125/// A signed integer value between −2147483648 and 2147483647.
126impl SimpleBinaryEncodable for i32 {
127    fn byte_len(&self) -> usize {
128        4
129    }
130
131    fn encode<S: Write + ?Sized>(&self, stream: &mut S) -> EncodingResult<()> {
132        write_i32(stream, *self)
133    }
134}
135
136impl SimpleBinaryDecodable for i32 {
137    fn decode<S: Read + ?Sized>(
138        stream: &mut S,
139        _decoding_options: &DecodingOptions,
140    ) -> EncodingResult<Self> {
141        read_i32(stream)
142    }
143}
144
145/// An unsigned integer value between 0 and 4294967295.
146impl SimpleBinaryEncodable for u32 {
147    fn byte_len(&self) -> usize {
148        4
149    }
150
151    fn encode<S: Write + ?Sized>(&self, stream: &mut S) -> EncodingResult<()> {
152        write_u32(stream, *self)
153    }
154}
155impl SimpleBinaryDecodable for u32 {
156    fn decode<S: Read + ?Sized>(
157        stream: &mut S,
158        _decoding_options: &DecodingOptions,
159    ) -> EncodingResult<Self> {
160        read_u32(stream)
161    }
162}
163
164/// A signed integer value between −9223372036854775808 and 9223372036854775807.
165impl SimpleBinaryEncodable for i64 {
166    fn byte_len(&self) -> usize {
167        8
168    }
169
170    fn encode<S: Write + ?Sized>(&self, stream: &mut S) -> EncodingResult<()> {
171        write_i64(stream, *self)
172    }
173}
174
175impl SimpleBinaryDecodable for i64 {
176    fn decode<S: Read + ?Sized>(
177        stream: &mut S,
178        _decoding_options: &DecodingOptions,
179    ) -> EncodingResult<Self> {
180        read_i64(stream)
181    }
182}
183
184/// An unsigned integer value between 0 and 18446744073709551615.
185impl SimpleBinaryEncodable for u64 {
186    fn byte_len(&self) -> usize {
187        8
188    }
189
190    fn encode<S: Write + ?Sized>(&self, stream: &mut S) -> EncodingResult<()> {
191        write_u64(stream, *self)
192    }
193}
194
195impl SimpleBinaryDecodable for u64 {
196    fn decode<S: Read + ?Sized>(
197        stream: &mut S,
198        _decoding_options: &DecodingOptions,
199    ) -> EncodingResult<Self> {
200        read_u64(stream)
201    }
202}
203
204/// An IEEE single precision (32 bit) floating point value.
205impl SimpleBinaryEncodable for f32 {
206    fn byte_len(&self) -> usize {
207        4
208    }
209
210    fn encode<S: Write + ?Sized>(&self, stream: &mut S) -> EncodingResult<()> {
211        write_f32(stream, *self)
212    }
213}
214
215impl SimpleBinaryDecodable for f32 {
216    fn decode<S: Read + ?Sized>(
217        stream: &mut S,
218        _decoding_options: &DecodingOptions,
219    ) -> EncodingResult<Self> {
220        read_f32(stream)
221    }
222}
223
224/// An IEEE double precision (64 bit) floating point value.
225impl SimpleBinaryEncodable for f64 {
226    fn byte_len(&self) -> usize {
227        8
228    }
229
230    fn encode<S: Write + ?Sized>(&self, stream: &mut S) -> EncodingResult<()> {
231        write_f64(stream, *self)
232    }
233}
234
235impl SimpleBinaryDecodable for f64 {
236    fn decode<S: Read + ?Sized>(
237        stream: &mut S,
238        _decoding_options: &DecodingOptions,
239    ) -> EncodingResult<Self> {
240        read_f64(stream)
241    }
242}