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
use byteorder::{BigEndian, ByteOrder, LittleEndian};

pub const ENCAPSULATION_HEADER_SIZE: u64 = 4;

/// Data encapsulation scheme identifiers.
pub trait Encapsulation {
    type E: ByteOrder;

    fn id() -> [u8; 2];
    fn option() -> [u8; 2] {
        [0; 2]
    }
}

/// OMG CDR big-endian encapsulation.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum CdrBe {}

impl Encapsulation for CdrBe {
    type E = BigEndian;

    fn id() -> [u8; 2] {
        [0, 0]
    }
}

/// OMG CDR little-endian encapsulation.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum CdrLe {}

impl Encapsulation for CdrLe {
    type E = LittleEndian;

    fn id() -> [u8; 2] {
        [0, 1]
    }
}

/// ParameterList encapsulated using OMG CDR big-endian encapsulation.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum PlCdrBe {}

impl Encapsulation for PlCdrBe {
    type E = BigEndian;

    fn id() -> [u8; 2] {
        [0, 2]
    }
}

/// ParameterList encapsulated using OMG CDR little-endian encapsulation.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum PlCdrLe {}

impl Encapsulation for PlCdrLe {
    type E = LittleEndian;

    fn id() -> [u8; 2] {
        [0, 3]
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_constant() {
        assert_eq!(
            ENCAPSULATION_HEADER_SIZE,
            (CdrBe::id().len() + CdrBe::option().len()) as u64
        );
        assert_eq!(
            ENCAPSULATION_HEADER_SIZE,
            (CdrLe::id().len() + CdrLe::option().len()) as u64
        );
        assert_eq!(
            ENCAPSULATION_HEADER_SIZE,
            (PlCdrBe::id().len() + PlCdrBe::option().len()) as u64
        );
        assert_eq!(
            ENCAPSULATION_HEADER_SIZE,
            (PlCdrLe::id().len() + PlCdrLe::option().len()) as u64
        );
    }
}