async-opcua-types 0.19.0

OPC UA data types
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
// OPCUA for Rust
// SPDX-License-Identifier: MPL-2.0
// Copyright (C) 2017-2024 Adam Lock

//! Contains the `BinaryEncodable` trait and helpers for reading and writing of scalar values and
//! other primitives.

use std::{
    error::Error as StdError,
    fmt::{Debug, Display},
    io::{Cursor, Read, Result, Write},
    num::{ParseFloatError, ParseIntError},
    sync::atomic::{AtomicU64, Ordering},
};

use byteorder::{ByteOrder, LittleEndian, WriteBytesExt};
use chrono::Duration;
use tracing::error;

use crate::{constants, status_code::StatusCode, Context, QualifiedName};

#[derive(Debug, Clone, Default)]
/// Parsed data encoding.
pub enum DataEncoding {
    #[default]
    /// Binary data decoding.
    Binary,
    /// XML data encoding.
    XML,
    /// JSON data encoding.
    JSON,
    /// Some other data encoding.
    Other(QualifiedName),
}
#[derive(Debug, Clone, Default)]
/// Parsed data encoding, only built-in encodings.
pub enum BuiltInDataEncoding {
    #[default]
    /// Binary data decoding.
    Binary,
    /// XML data encoding.
    XML,
    /// JSON data encoding.
    JSON,
}

impl DataEncoding {
    /// Parse data encoding from the browse name in a service call.
    pub fn from_browse_name(name: QualifiedName) -> std::result::Result<Self, StatusCode> {
        match name.name.as_ref() {
            "Default Binary" | "" => Ok(Self::Binary),
            "Default XML" => Ok(Self::XML),
            "Default JSON" => Ok(Self::JSON),
            _ if name.namespace_index != 0 => Ok(Self::Other(name)),
            _ => Err(StatusCode::BadDataEncodingInvalid),
        }
    }
}

/// Result of an encoding or decoding operation.
pub type EncodingResult<T> = std::result::Result<T, Error>;

#[derive(Debug)]
/// General OPC-UA error.
///
/// Contains context about the request this error occured as part of, if that is possible to retrieve,
/// as well as details about the error that caused this, and a status code.
pub struct Error {
    status: StatusCode,
    request_id: Option<u32>,
    request_handle: Option<u32>,
    context: Box<dyn StdError + Send + Sync>,
}

impl Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}: {}", self.status(), self.context)
    }
}

impl StdError for Error {
    fn source(&self) -> Option<&(dyn StdError + 'static)> {
        Some(&*self.context)
    }
}

impl Error {
    /// Create a new error with the specified `status` code and
    /// `context` as a dynamic error source.
    pub fn new(status: StatusCode, context: impl Into<Box<dyn StdError + Send + Sync>>) -> Self {
        Self {
            status,
            request_handle: None,
            request_id: None,
            context: context.into(),
        }
    }

    /// Create a new error with status code `BadDecodingError` and
    /// `context` as a dynamic error source.
    pub fn decoding(context: impl Into<Box<dyn StdError + Send + Sync>>) -> Self {
        Self {
            status: StatusCode::BadDecodingError,
            request_handle: None,
            request_id: None,
            context: context.into(),
        }
    }

    /// Create a new error with status code `BadEncodingError` and
    /// `context` as a dynamic error source.
    pub fn encoding(context: impl Into<Box<dyn StdError + Send + Sync>>) -> Self {
        Self {
            status: StatusCode::BadEncodingError,
            request_handle: None,
            request_id: None,
            context: context.into(),
        }
    }

    /// Add request ID and request handle to this error.
    pub fn with_context(mut self, request_id: Option<u32>, request_handle: Option<u32>) -> Self {
        self.request_id = request_id;
        self.request_handle = request_handle;
        self
    }

    /// Add request ID to this error.
    pub fn with_request_id(mut self, id: u32) -> Self {
        self.request_id = Some(id);
        self
    }

    /// Add request handle to this error.
    pub fn with_request_handle(mut self, handle: u32) -> Self {
        self.request_handle = Some(handle);
        self
    }

    /// Utility for setting request handle when it is _maybe_ initialized.
    pub fn maybe_with_request_handle(mut self, handle: Option<u32>) -> Self {
        if let Some(handle) = handle {
            self.request_handle = Some(handle);
        }
        self
    }

    /// Get the inner status code of this error.
    pub fn status(&self) -> StatusCode {
        self.status
    }

    /// Get the full context of this error, if both fields are present.
    pub fn full_context(&self) -> Option<(u32, u32)> {
        if let (Some(id), Some(handle)) = (self.request_id, self.request_handle) {
            Some((id, handle))
        } else {
            None
        }
    }
}

impl From<Error> for StatusCode {
    fn from(value: Error) -> Self {
        error!("{}", value);
        value.status()
    }
}

impl From<Error> for std::io::Error {
    fn from(value: Error) -> Self {
        value.status().into()
    }
}

impl From<std::io::Error> for Error {
    fn from(value: std::io::Error) -> Self {
        Self::decoding(value)
    }
}

impl From<ParseIntError> for Error {
    fn from(value: ParseIntError) -> Self {
        Self::decoding(value)
    }
}

impl From<ParseFloatError> for Error {
    fn from(value: ParseFloatError) -> Self {
        Self::decoding(value)
    }
}

/// Depth lock holds a reference on the depth gauge. The drop ensures impl that the reference is
/// decremented even if there is a panic unwind.
#[derive(Debug)]
pub struct DepthLock<'a> {
    depth_gauge: &'a DepthGauge,
}

impl Drop for DepthLock<'_> {
    fn drop(&mut self) {
        // This will overflow back if the gauge is somehow at 0. That really should not be possible, if it is only ever
        // incremented from `obtain`
        self.depth_gauge
            .current_depth
            .fetch_sub(1, Ordering::Release);
    }
}

impl<'a> DepthLock<'a> {
    fn new(depth_gauge: &'a DepthGauge) -> (Self, u64) {
        let current = depth_gauge.current_depth.fetch_add(1, Ordering::Acquire);

        (Self { depth_gauge }, current)
    }

    /// The depth lock tests if the depth can increment and then obtains a lock on it.
    /// The lock will decrement the depth when it drops to ensure proper behaviour during unwinding.
    pub fn obtain(depth_gauge: &'a DepthGauge) -> core::result::Result<DepthLock<'a>, Error> {
        let max_depth = depth_gauge.max_depth;
        let (gauge, val) = Self::new(depth_gauge);

        if val >= max_depth {
            Err(Error::decoding(
                "Decoding in stream aborted due maximum recursion depth being reached",
            ))
        } else {
            Ok(gauge)
        }
    }
}

/// Depth gauge is used on potentially recursive structures like Variant & ExtensionObject during
/// decoding to limit the depth the decoder will go before giving up.
#[derive(Debug)]
pub struct DepthGauge {
    /// Maximum decoding depth for recursive elements. Triggers when current depth equals max depth.
    pub(self) max_depth: u64,
    /// Current decoding depth for recursive elements.
    pub(self) current_depth: AtomicU64,
}

// TODO: In general keeping DepthGauge as part of DecodingOptions is suboptimal,
// since this pattern is unintuitive. It should be separated out.
impl Clone for DepthGauge {
    fn clone(&self) -> Self {
        Self {
            max_depth: self.max_depth,
            current_depth: AtomicU64::new(0),
        }
    }
}

impl Default for DepthGauge {
    fn default() -> Self {
        Self::new(constants::MAX_DECODING_DEPTH)
    }
}

impl DepthGauge {
    /// Create a new depth gauge with specified max depth.
    pub fn new(max_depth: u64) -> Self {
        Self {
            max_depth,
            current_depth: AtomicU64::new(0),
        }
    }

    /// Create a minimal depth gauge with max depth of 1.
    pub fn minimal() -> Self {
        Self {
            max_depth: 1,
            ..Default::default()
        }
    }

    /// Get the max depth of the gauge.
    pub fn max_depth(&self) -> u64 {
        self.max_depth
    }
}

#[derive(Clone, Debug)]
/// General decoding options.
pub struct DecodingOptions {
    /// Time offset between the client and the server, only used by the client when it's configured
    /// to ignore time skew.
    pub client_offset: Duration,
    /// Maximum size of a message in bytes. 0 means no limit.
    pub max_message_size: usize,
    /// Maximum number of chunks. 0 means no limit.
    pub max_chunk_count: usize,
    /// Maximum length in bytes (not chars!) of a string. 0 actually means 0, i.e. no string permitted
    pub max_string_length: usize,
    /// Maximum length in bytes of a byte string. 0 actually means 0, i.e. no byte string permitted
    pub max_byte_string_length: usize,
    /// Maximum number of array elements. 0 actually means 0, i.e. no array permitted
    pub max_array_length: usize,
    /// Decoding depth gauge is used to check for recursion
    pub decoding_depth_gauge: DepthGauge,
}

impl Default for DecodingOptions {
    fn default() -> Self {
        DecodingOptions {
            client_offset: Duration::zero(),
            max_message_size: constants::MAX_MESSAGE_SIZE,
            max_chunk_count: constants::MAX_CHUNK_COUNT,
            max_string_length: constants::MAX_STRING_LENGTH,
            max_byte_string_length: constants::MAX_BYTE_STRING_LENGTH,
            max_array_length: constants::MAX_ARRAY_LENGTH,
            decoding_depth_gauge: DepthGauge::default(),
        }
    }
}

impl DecodingOptions {
    /// This can be useful for decoding extension objects where the payload is not expected to contain
    /// a large value.
    pub fn minimal() -> Self {
        DecodingOptions {
            max_string_length: 8192,
            max_byte_string_length: 8192,
            max_array_length: 8192,
            decoding_depth_gauge: DepthGauge::minimal(),
            ..Default::default()
        }
    }

    /// For test only. Having a separate function makes it easier to control calls to DecodingOptions::default().
    pub fn test() -> Self {
        Self::default()
    }

    /// Get a lease on depth, this will fail if max depth is exceeded.
    /// Once the lease is dropped, current depth is decremented.
    pub fn depth_lock(&self) -> core::result::Result<DepthLock<'_>, Error> {
        DepthLock::obtain(&self.decoding_depth_gauge)
    }
}

/// Trait implemented by OPC-UA types, indicating whether
/// they are null or not, for use in encoding.
pub trait UaNullable {
    /// Return true if this value is null, meaning it can be left out when
    /// being encoded in JSON and XML encodings.
    fn is_ua_null(&self) -> bool {
        false
    }
}

impl<T> UaNullable for Option<T>
where
    T: UaNullable,
{
    fn is_ua_null(&self) -> bool {
        match self {
            Some(s) => s.is_ua_null(),
            None => true,
        }
    }
}

impl<T> UaNullable for Vec<T> where T: UaNullable {}
impl<T> UaNullable for Box<T>
where
    T: UaNullable,
{
    fn is_ua_null(&self) -> bool {
        self.as_ref().is_ua_null()
    }
}

macro_rules! is_null_const {
    ($t:ty, $c:expr) => {
        impl UaNullable for $t {
            fn is_ua_null(&self) -> bool {
                *self == $c
            }
        }
    };
}

is_null_const!(bool, false);
is_null_const!(u8, 0);
is_null_const!(u16, 0);
is_null_const!(u32, 0);
is_null_const!(u64, 0);
is_null_const!(i8, 0);
is_null_const!(i16, 0);
is_null_const!(i32, 0);
is_null_const!(i64, 0);
is_null_const!(f32, 0.0);
is_null_const!(f64, 0.0);

impl UaNullable for String {}
impl UaNullable for str {}

/// OPC UA Binary Encoding interface. Anything that encodes to binary must implement this. It provides
/// functions to calculate the size in bytes of the struct (for allocating memory), encoding to a stream
/// and decoding from a stream.
///
/// # Implementing
///
/// The majority of implementers should just use the `derive(BinaryEncodable)` macro,
/// if you need to implement this yourself for some reason, the following _must_ be satisfied:
///
///  - `byte_len` must return a length exactly equal to what `encode` will write, or `encode`
///    must be guaranteed to fail. Since `byte_len` is infallible, you are allowed to
///    return some invalid value there, then fail later when calling `encode`. This should be avoided.
///  - `encode` must use `write_all` on the stream, not just `write`, to ensure that all the data
///    is written, even if the stream is interrupted. Prefer calling `encode` on inner types
///    instead.
pub trait BinaryEncodable {
    /// Returns the exact byte length of the structure as it would be if `encode` were called.
    /// This may be called prior to writing to ensure the correct amount of space is available.
    #[allow(unused)]
    fn byte_len(&self, ctx: &crate::Context<'_>) -> usize;
    /// Encodes the instance to the write stream.
    fn encode<S: Write + ?Sized>(&self, stream: &mut S, ctx: &Context<'_>) -> EncodingResult<()>;

    /// Override the extension object encoding used for this type.
    /// This only makes sense if the type can only ever be encoded using a single
    /// built-in encoding.
    fn override_encoding(&self) -> Option<BuiltInDataEncoding> {
        None
    }

    /// Convenience method for encoding a message straight into an array of bytes. It is preferable to reuse buffers than
    /// to call this so it should be reserved for tests and trivial code.
    fn encode_to_vec(&self, ctx: &Context<'_>) -> Vec<u8> {
        let mut buffer = Cursor::new(Vec::with_capacity(self.byte_len(ctx)));
        let _ = self.encode(&mut buffer, ctx);
        buffer.into_inner()
    }
}

/// Trait for decoding a type from OPC UA binary.
pub trait BinaryDecodable: Sized {
    /// Decodes an instance from the read stream. The decoding options contains restrictions set by
    /// the server / client on the length of strings, arrays etc. If these limits are exceeded the
    /// implementation should return with a `BadDecodingError` as soon as possible.
    fn decode<S: Read + ?Sized>(stream: &mut S, ctx: &Context<'_>) -> EncodingResult<Self>;
}

/// Trait for encoding a type that cannot contain any custom types
/// to OPC UA binary. Used in some core modules to encode raw binary messages.
pub trait SimpleBinaryEncodable {
    #[allow(unused)]
    // Returns the exact byte length of the structure as it would be if `encode` were called.
    /// This may be called prior to writing to ensure the correct amount of space is available.
    fn byte_len(&self) -> usize;

    /// Encodes the instance to the write stream.
    fn encode<S: Write + ?Sized>(&self, stream: &mut S) -> EncodingResult<()>;

    /// Convenience method for encoding a message straight into an array of bytes. It is preferable to reuse buffers than
    /// to call this so it should be reserved for tests and trivial code.
    fn encode_to_vec(&self) -> Vec<u8> {
        let mut buffer = Cursor::new(Vec::with_capacity(self.byte_len()));
        let _ = self.encode(&mut buffer);
        buffer.into_inner()
    }
}

impl<T> BinaryEncodable for T
where
    T: SimpleBinaryEncodable,
{
    fn byte_len(&self, _ctx: &crate::Context<'_>) -> usize {
        SimpleBinaryEncodable::byte_len(self)
    }

    fn encode<S: Write + ?Sized>(&self, stream: &mut S, _ctx: &Context<'_>) -> EncodingResult<()> {
        SimpleBinaryEncodable::encode(self, stream)
    }
}

/// Trait for decoding a type that cannot contain any custom types
/// from OPC UA binary. Used in some core modules to decode raw binary messages.
pub trait SimpleBinaryDecodable: Sized {
    /// Decode Self from the byte stream.
    fn decode<S: Read + ?Sized>(
        stream: &mut S,
        decoding_options: &DecodingOptions,
    ) -> EncodingResult<Self>;
}

impl<T> BinaryDecodable for T
where
    T: SimpleBinaryDecodable,
{
    fn decode<S: Read + ?Sized>(stream: &mut S, ctx: &Context<'_>) -> EncodingResult<Self> {
        SimpleBinaryDecodable::decode(stream, ctx.options())
    }
}

/// Converts an IO encoding error (and logs when in error) into an EncodingResult
pub fn process_encode_io_result(result: Result<()>) -> EncodingResult<()> {
    result.map_err(Error::encoding)
}

/// Converts an IO encoding error (and logs when in error) into an EncodingResult
pub fn process_decode_io_result<T>(result: Result<T>) -> EncodingResult<T>
where
    T: Debug,
{
    result.map_err(Error::decoding)
}

impl<T> BinaryEncodable for Option<Vec<T>>
where
    T: BinaryEncodable,
{
    fn byte_len(&self, ctx: &crate::Context<'_>) -> usize {
        let mut size = 4;
        if let Some(ref values) = self {
            size += values.iter().map(|v| v.byte_len(ctx)).sum::<usize>();
        }
        size
    }

    fn encode<S: Write + ?Sized>(&self, stream: &mut S, ctx: &Context<'_>) -> EncodingResult<()> {
        if let Some(ref values) = self {
            write_i32(stream, values.len() as i32)?;
            for value in values.iter() {
                value.encode(stream, ctx)?;
            }
        } else {
            write_i32(stream, -1)?;
        }
        Ok(())
    }
}

impl<T> BinaryDecodable for Option<Vec<T>>
where
    T: BinaryDecodable,
{
    fn decode<S: Read + ?Sized>(
        stream: &mut S,
        ctx: &Context<'_>,
    ) -> EncodingResult<Option<Vec<T>>> {
        let len = read_i32(stream)?;
        if len == -1 {
            Ok(None)
        } else if len < -1 {
            Err(Error::decoding(
                "Array length is negative value and invalid",
            ))
        } else if len as usize > ctx.options().max_array_length {
            Err(Error::decoding(format!(
                "Array length {} exceeds decoding limit {}",
                len,
                ctx.options().max_array_length
            )))
        } else {
            let mut values: Vec<T> = Vec::with_capacity(len as usize);
            for _ in 0..len {
                values.push(T::decode(stream, ctx)?);
            }
            Ok(Some(values))
        }
    }
}

/// Calculates the length in bytes of an array of encoded type
pub fn byte_len_array<T: BinaryEncodable>(values: &Option<Vec<T>>, ctx: &Context<'_>) -> usize {
    let mut size = 4;
    if let Some(ref values) = values {
        size += values.iter().map(|v| v.byte_len(ctx)).sum::<usize>();
    }
    size
}

/// Writes a series of identical bytes to the stream
pub fn write_bytes<W: Write + ?Sized>(
    stream: &mut W,
    value: u8,
    count: usize,
) -> EncodingResult<usize> {
    for _ in 0..count {
        stream.write_u8(value).map_err(Error::encoding)?;
    }
    Ok(count)
}

/// Writes an unsigned byte to the stream
pub fn write_u8<T, W: Write + ?Sized>(stream: &mut W, value: T) -> EncodingResult<()>
where
    T: Into<u8>,
{
    let buf: [u8; 1] = [value.into()];
    process_encode_io_result(stream.write_all(&buf))
}

/// Writes a signed 16-bit value to the stream
pub fn write_i16<T, W: Write + ?Sized>(stream: &mut W, value: T) -> EncodingResult<()>
where
    T: Into<i16>,
{
    let mut buf = [0u8; 2];
    LittleEndian::write_i16(&mut buf, value.into());
    process_encode_io_result(stream.write_all(&buf))
}

/// Writes an unsigned 16-bit value to the stream
pub fn write_u16<T, W: Write + ?Sized>(stream: &mut W, value: T) -> EncodingResult<()>
where
    T: Into<u16>,
{
    let mut buf = [0u8; 2];
    LittleEndian::write_u16(&mut buf, value.into());
    process_encode_io_result(stream.write_all(&buf))
}

/// Writes a signed 32-bit value to the stream
pub fn write_i32<T, W: Write + ?Sized>(stream: &mut W, value: T) -> EncodingResult<()>
where
    T: Into<i32>,
{
    let mut buf = [0u8; 4];
    LittleEndian::write_i32(&mut buf, value.into());
    process_encode_io_result(stream.write_all(&buf))
}

/// Writes an unsigned 32-bit value to the stream
pub fn write_u32<T, W: Write + ?Sized>(stream: &mut W, value: T) -> EncodingResult<()>
where
    T: Into<u32>,
{
    let mut buf = [0u8; 4];
    LittleEndian::write_u32(&mut buf, value.into());
    process_encode_io_result(stream.write_all(&buf))
}

/// Writes a signed 64-bit value to the stream
pub fn write_i64<T, W: Write + ?Sized>(stream: &mut W, value: T) -> EncodingResult<()>
where
    T: Into<i64>,
{
    let mut buf = [0u8; 8];
    LittleEndian::write_i64(&mut buf, value.into());
    process_encode_io_result(stream.write_all(&buf))
}

/// Writes an unsigned 64-bit value to the stream
pub fn write_u64<T, W: Write + ?Sized>(stream: &mut W, value: T) -> EncodingResult<()>
where
    T: Into<u64>,
{
    let mut buf = [0u8; 8];
    LittleEndian::write_u64(&mut buf, value.into());
    process_encode_io_result(stream.write_all(&buf))
}

/// Writes a 32-bit precision value to the stream
pub fn write_f32<T, W: Write + ?Sized>(stream: &mut W, value: T) -> EncodingResult<()>
where
    T: Into<f32>,
{
    let mut buf = [0u8; 4];
    LittleEndian::write_f32(&mut buf, value.into());
    process_encode_io_result(stream.write_all(&buf))
}

/// Writes a 64-bit precision value to the stream
pub fn write_f64<T, W: Write + ?Sized>(stream: &mut W, value: T) -> EncodingResult<()>
where
    T: Into<f64>,
{
    let mut buf = [0u8; 8];
    LittleEndian::write_f64(&mut buf, value.into());
    process_encode_io_result(stream.write_all(&buf))
}

/// Reads an array of bytes from the stream
pub fn read_bytes<R: Read + ?Sized>(stream: &mut R, buf: &mut [u8]) -> EncodingResult<usize> {
    let result = stream.read_exact(buf);
    process_decode_io_result(result)?;
    Ok(buf.len())
}

/// Read an unsigned byte from the stream
pub fn read_u8<R: Read + ?Sized>(stream: &mut R) -> EncodingResult<u8> {
    let mut buf = [0u8];
    let result = stream.read_exact(&mut buf);
    process_decode_io_result(result)?;
    Ok(buf[0])
}

/// Read an signed 16-bit value from the stream
pub fn read_i16<R: Read + ?Sized>(stream: &mut R) -> EncodingResult<i16> {
    let mut buf = [0u8; 2];
    let result = stream.read_exact(&mut buf);
    process_decode_io_result(result)?;
    Ok(LittleEndian::read_i16(&buf))
}

/// Read an unsigned 16-bit value from the stream
pub fn read_u16<R: Read + ?Sized>(stream: &mut R) -> EncodingResult<u16> {
    let mut buf = [0u8; 2];
    let result = stream.read_exact(&mut buf);
    process_decode_io_result(result)?;
    Ok(LittleEndian::read_u16(&buf))
}

/// Read a signed 32-bit value from the stream
pub fn read_i32<R: Read + ?Sized>(stream: &mut R) -> EncodingResult<i32> {
    let mut buf = [0u8; 4];
    let result = stream.read_exact(&mut buf);
    process_decode_io_result(result)?;
    Ok(LittleEndian::read_i32(&buf))
}

/// Read an unsigned 32-bit value from the stream
pub fn read_u32<R: Read + ?Sized>(stream: &mut R) -> EncodingResult<u32> {
    let mut buf = [0u8; 4];
    let result = stream.read_exact(&mut buf);
    process_decode_io_result(result)?;
    Ok(LittleEndian::read_u32(&buf))
}

/// Read a signed 64-bit value from the stream
pub fn read_i64<R: Read + ?Sized>(stream: &mut R) -> EncodingResult<i64> {
    let mut buf = [0u8; 8];
    let result = stream.read_exact(&mut buf);
    process_decode_io_result(result)?;
    Ok(LittleEndian::read_i64(&buf))
}

/// Read an unsigned 64-bit value from the stream
pub fn read_u64<R: Read + ?Sized>(stream: &mut R) -> EncodingResult<u64> {
    let mut buf = [0u8; 8];
    let result = stream.read_exact(&mut buf);
    process_decode_io_result(result)?;
    Ok(LittleEndian::read_u64(&buf))
}

/// Read a 32-bit precision value from the stream
pub fn read_f32<R: Read + ?Sized>(stream: &mut R) -> EncodingResult<f32> {
    let mut buf = [0u8; 4];
    let result = stream.read_exact(&mut buf);
    process_decode_io_result(result)?;
    Ok(LittleEndian::read_f32(&buf))
}

/// Read a 64-bit precision from the stream
pub fn read_f64<R: Read + ?Sized>(stream: &mut R) -> EncodingResult<f64> {
    let mut buf = [0u8; 8];
    let result = stream.read_exact(&mut buf);
    process_decode_io_result(result)?;
    Ok(LittleEndian::read_f64(&buf))
}

/// Skip `bytes` bytes in the stream.
pub fn skip_bytes<R: Read + ?Sized>(stream: &mut R, bytes: u64) -> EncodingResult<()> {
    std::io::copy(&mut stream.take(bytes), &mut std::io::sink())?;
    Ok(())
}

#[macro_export]
/// Implement the encodable traits for a type, using the provided
/// functions to convert to and from some other type.
///
/// Usage is `impl_encoded_as!(MyType, MyType::from_other, MyType::into_other, |t| t.len())`
macro_rules! impl_encoded_as {
    ($ty:ident, $from:expr, $to:expr, $byte_len:expr) => {
        impl $crate::SimpleBinaryEncodable for $ty {
            fn byte_len(&self) -> usize {
                $byte_len(self)
            }

            fn encode<S: std::io::Write + ?Sized>(
                &self,
                stream: &mut S,
            ) -> $crate::EncodingResult<()> {
                $to(self)?.encode(stream)
            }
        }

        impl $crate::SimpleBinaryDecodable for $ty {
            fn decode<S: std::io::Read + ?Sized>(
                stream: &mut S,
                decoding_options: &$crate::DecodingOptions,
            ) -> $crate::EncodingResult<Self> {
                let inner = $crate::SimpleBinaryDecodable::decode(stream, decoding_options)?;
                $from(inner)
            }
        }

        #[cfg(feature = "json")]
        impl $crate::json::JsonEncodable for $ty {
            fn encode(
                &self,
                stream: &mut $crate::json::JsonStreamWriter<&mut dyn std::io::Write>,
                ctx: &$crate::json::Context<'_>,
            ) -> $crate::EncodingResult<()> {
                $to(self)?.encode(stream, ctx)
            }
        }

        #[cfg(feature = "json")]
        impl $crate::json::JsonDecodable for $ty {
            fn decode(
                stream: &mut $crate::json::JsonStreamReader<&mut dyn std::io::Read>,
                ctx: &$crate::json::Context<'_>,
            ) -> $crate::EncodingResult<Self> {
                let inner = $crate::json::JsonDecodable::decode(stream, ctx)?;
                $from(inner)
            }
        }

        #[cfg(feature = "xml")]
        impl $crate::xml::XmlEncodable for $ty {
            fn encode(
                &self,
                stream: &mut $crate::xml::XmlStreamWriter<&mut dyn std::io::Write>,
                ctx: &$crate::xml::Context<'_>,
            ) -> $crate::EncodingResult<()> {
                $to(self)?.encode(stream, ctx)
            }
        }
        #[cfg(feature = "xml")]
        impl $crate::xml::XmlDecodable for $ty {
            fn decode(
                stream: &mut $crate::xml::XmlStreamReader<&mut dyn std::io::Read>,
                ctx: &$crate::xml::Context<'_>,
            ) -> $crate::EncodingResult<Self> {
                let inner = $crate::xml::XmlDecodable::decode(stream, ctx)?;
                $from(inner)
            }
        }
    };
}

pub use impl_encoded_as;

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use super::{constants, DepthGauge, DepthLock};
    use crate::StatusCode;

    #[test]
    fn depth_gauge() {
        let dg = Arc::new(DepthGauge::default());

        let max_depth = dg.max_depth();
        assert_eq!(max_depth, constants::MAX_DECODING_DEPTH);

        // Iterate the depth
        {
            let mut v = Vec::new();
            for _ in 0..max_depth {
                v.push(DepthLock::obtain(&dg).unwrap());
            }

            // Depth should now be MAX_DECODING_DEPTH
            {
                assert_eq!(
                    dg.current_depth.load(std::sync::atomic::Ordering::Relaxed),
                    max_depth
                );
            }

            // Next obtain should fail
            assert_eq!(
                DepthLock::obtain(&dg).unwrap_err().status,
                StatusCode::BadDecodingError
            );

            // DepthLocks drop here
        }

        // Depth should be zero
        {
            assert_eq!(
                dg.current_depth.load(std::sync::atomic::Ordering::Relaxed),
                0
            );
        }
    }
}